# Histogram logarithmic with logarithmic bins

Hi,
I have a code to simulate data of astroparticles, i´m using a logarithmic scale without troubles but i need to implement a logarithmic bin, because a need to see the improbable data, i appreciate your help in this case.
this is the code:

``````#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <math.h>
#include <sstream>
#include<time.h>

using namespace std;

int potencial()
{

//declaracion de variables

long double N=0;
cout << "Introduzca el numero de variables aleatorias a generar: " << "\n";
cin  >> N;

long double E_0=0;
cout << "Introduzca el valor inicial de energia (MeV): " << "\n";
cin >> E_0;

long double E_m=0;
cout << "Introduzca el valor maximo de energia(TeV): " << "\n";
cin >> E_m;

long double alpha=0;
cout << "Introduzca el valor de alpha: " << "\n";
cin >> alpha;

E_0= E_0 * 1e6; // MeV
E_m= E_m * 1e12; // TeV

TH1D* potencial = new TH1D("potencial", "Histograma Potencial de Energias", 100000.,100000.,1000000000.);//(”nombre",”Titutlo”,xBins,lowxbin, upxbin);

potencial->GetXaxis()->SetTitle("MeV");
potencial->GetYaxis()->SetTitle("frecuencia");

long double  num=0 ;
long int  i=0, j=0;

srand(time(NULL)); //semilla que usa la hora del ordenador para generar numeros aleatorios

for( i = 0; i < N; i++){

num =  rand() % (10000);//numeros aleatorios entre 0 y 1 con 4 decimales
num = num / 10000;

num = exp(log(( pow(E_m,(1-alpha))-pow(E_0,(1-alpha)) ) * num  + pow(E_0,(1-alpha)))/(1-alpha));

potencial->Fill( (num) );

}
potencial->Draw();
// gStyle->SetOptLogx();
return 0;

}
``````

Luis Hernández
University Central of Venezuela
Science Faculty
Hernandez.y.luis.a@gmail.com
luis.hernandez.y@ucv.ve

Search for “variable bin size” in the TH1 class description.

Better use this version of the class description:
root.cern.ch/doc/master/classTH1.html

thanks, but this can not solve my problem yet, may you help me with an example?

The constructor we pointed to you allows to defined the bins limits as you wish using an array. You simply have to fill the array with limits distributed according to a log scale.

so, can i put the TH1 into a loop ?

for()
{
TH1D* potencial = new TH1D(“potencial”, “Histograma Potencial de Energias”, 100000.,100000.,1000000000.)

}

something like that but using a variable in the number of bins?

``TH1D* potencial = new TH1D("potencial", "Histograma Potencial de Energias", 100000.,100000.,1000000000.)``

If you create the histogram this way it will not be a variable bin size one.(also note that there is a type error for the number of bins, it should be an integer).

The variable bin size constructor is:

root.cern.ch/doc/master/classTH … 75b6c7aa5a