# Loop of opening several histogram

Bonjour,

I want to calculate the relative error between two histogram for each bin( 9 bins ).
I need to get the content of each bin(i) from several histograms.
For that I want to use the “TH1F(Form(“h%s”,pdf[1]))->GetContentBin(i);” instead "hstw->GetContentBin(i); " in order to access to all the histogram.
But I get these error.
Amine

``````[...]
TH1D *hct10 = (TH1D*)fCT10->Get("Cs_NLO");
TH1D *hmstw = (TH1D*)fMSTW->Get("Cs_NLO");
TH1D *hnnpdf = (TH1D*)fNNPDF->Get("Cs_NLO");
TH1D *hcteq = (TH1D*)fCTEQ->Get("Cs_NLO");

char *pdf[1]= "mstw";
char *pdf[2]= "nnpdf";

for (int i=1 ; i<10 ; i++) {
Content1 = TH1F(Form("h%s",pdf[1]))->GetBinContent(i);
Content2 = TH1F(Form("h%s",pdf[2]))->GetBinContent(i);

diff = Content1-Content2 ;
Double relative_error = (diff/Content1)*100;

}``````
``````Error: No matching constructor for explicit conversion TH1F(Form("h%s",pdf[1])) comp_jetphjox_plot.C:90:
Error: non class,struct,union object TH1F(Form("h%s",pdf[1])) used with . or -> comp_jetphjox_plot.C:90:

*** Break *** segmentation violation``````
``      Content1 = TH1F(Form("h%s",pdf[1]))->GetBinContent(i);``

Of course this cannot work. Form(“h%s”,pdf[1]) returns a text string …
how do you expect casting it to an histogram ?

You should make an array of histograms.

Thank you couet,

The execution crash, it’s come from :

``````herrorpos[k]->SetBinContent(i,er_pos);
herrorneg[k]->SetBinContent(i,er_neg);``````

we can not use SetBinContent() function for array histogram ?

Thank you
Amine,

``````for (int k=1 ; k<7 ; k++) {
for (int i=1 ; i<10 ; i++) {
Content1 = h1[k]->GetBinContent(i);
Content2 = h2[k]->GetBinContent(i);

double diff = Content1-Content2 ;
double Erreur_relatif = (diff/Content1)*100;
cout << " fraction est  " << Erreur_relatif << "%" << endl;
double er_pos= +Erreur_relatif;
double er_neg= -Erreur_relatif;
cout << " + error  = " <<  er_pos << "%" <<endl;
cout << " - error  = " <<  er_neg << "%" <<endl;
herrorpos[k]->SetBinContent(i,er_pos);
herrorneg[k]->SetBinContent(i,er_neg);
}
herrorpos[k]->Draw("");
herrorneg[k]->Draw("same");
}``````

Your example does not show how you booked the histograms.

Thank you
I hope that it’s clear now.
Script:

``````    TH1F *hct10;
TH1F *hmstw;
TH1F *hnnpdf;
TH1F *  hcteq;
..
..
..
..

TH1F *herrorpos[7];
TH1F *herrorneg[7];
TH1F *h1[7];
TH1F *h2[7];

h1[1]= (TH1F*) hct10->Clone();
h1[2]= (TH1F*) hct10->Clone();
h1[3]= (TH1F*) hct10->Clone();
h1[4]= (TH1F*) hmstw->Clone();
h1[5]= (TH1F*) hmstw->Clone();
h1[6]= (TH1F*) hnnpdf->Clone();

h2[1]= (TH1F*) hmstw->Clone();
h2[2]= (TH1F*) hnnpdf->Clone();
h2[3]= (TH1F*) hcteq->Clone();
h2[4]= (TH1F*) hnnpdf->Clone();
h2[5]= (TH1F*) hcteq->Clone();
h2[6]= (TH1F*) hcteq->Clone();

for (int k=1 ; k<7 ; k++) // loop of array histogram
{
double er_pos[10];
double er_neg[10];
for (int i=1 ; i<10 ; i++) // loop of bin
{

double Content1 = h1[k]->GetBinContent(i);
double Content2 = h2[k]->GetBinContent(i);
double diff = Content1-Content2 ;
double Erreur_relatif = (diff/Content1)*100;

er_pos= +Erreur_relatif;
er_neg= - Erreur_relatif;
cout << " + error  = " <<  er_pos << "%" <<endl;
cout << " - error = " <<  er_neg << "%" <<endl;
// i want to fill and to plot the new histogram of the erreur relatif ....
herrorpos[k]->SetBinContent(i,er_pos);
herrorneg[k]->SetBinContent(i,er_neg);
}

herrorpos[k]->Draw("");
herrorneg[k]->Draw("same");

}``````

Yes, it is clear it cannot work… you have only pointers to histograms.

Where are the “new TH1F(…)”… ?

May be you should look at some little examples and play a little bit with them before writing straight away the full program …

Thank you very much couet!!!

It’s resolved now .
As you said, I forget create a new histogram…

Thank you!!
Amine

``for (int k=1 ; k<7 ; k++) // loop of array histogram``

and in the loop you access h1[k]…

If you have created a vector h1[n], the first “h1” is h1[0] and the last one h1[n-1].

Hi all,

I’m new here and face the same problem as Amelkhalii. I want to calculate the relative error between two histogram for each bin( 100 bins ). Here is the code I used for plotting the two histogram:

[code]int macro2(){
auto c=new TCanvas();c->SetGrid();

``````TGraphErrors graph_expected("./test_TError03.txt",
"%lg %lg %lg");
graph_expected.SetTitle(
"Measurement XYZ and Expectation;"
"lenght [cm];"
"Arb.Units");
graph_expected.SetFillColor(kRed);
graph_expected.DrawClone("E3AL"); // E3 draws the band

TGraphErrors graph("./test_TError04.txt","%lg %lg %lg");
graph.SetMarkerStyle(2);
graph.SetFillColor(kBlue);
graph.DrawClone("PESame");

// Draw the Legend
TLegend leg(.1,.7,.3,.9,"Lab. Lesson 2");
leg.SetFillColor(0);
leg.DrawClone("Same");

graph.Print();
return 0;
``````

}[/code]
I understand about the loop for bin but I don’t understand the loop of array histogram. How should I start doing this?

Warm regards,
Nur

The macro you sent is working with TGraphs, not with histograms. Are you sure that’s what you want to do ?

Hi Couet,

Yes. I forgot to mention that. Instead of histogram, I plot two graph because in HAWK, it also gave the y-error so I read the data files and plot two graph on top of each other. But what I’m interested is to plot the relative difference between these two graph as a function of mass ZH. So the idea is to get the array of number given as points to TGrapherror from each bin and then loop over the number of bin to calculate the relative difference. Lastly plot the graph of relative difference vs mass of ZH. That’s the only idea I can think about.

Feel free if you have any other suggestion.

Warm regards,
Nur

Sounds fine.

Hi Couet,

But the problem is, I don’t know how to do it… Do you know any reference I can refer to? About how to get the array of number given as points to TGrapherror from each bin? Is there any special commands or something?

Best regards,
Nur

TGraph::GetX()
TGraph::GetY()

Hi Couet,

Ok. Will try now. Thanks!

Best regards,
Nuha

Hi Couet,

Here is my code using GetX( ) but it breaks when I execute it. Where did I made the mistake?

Thanks!

``````//Reads the points from file, get the point x,y, calculate relative diff and lastly plot relative diff vs x03.

void testErr04(){
auto c=new TCanvas();

TGraphErrors graph03("./complete03.xsec","%lg %lg %lg");
Int_t n = graph03.GetN();
Double_t *x03 = graph03.GetX();
Double_t *y03 = graph03.GetY();

TGraphErrors graph04("./complete04.xsec","%lg %lg %lg");
Double_t *y04 = graph04.GetY();

//Calculate the relative difference
Double_t diff[100], rel_err[100];

for (Int_t i=0; i<=n; i++) {
diff[i] = y03[i] - y04[i];
rel_err[i] = (diff[i] / y03[i]);

}
TGraph *gr = new TGraph(n,rel_err,x03);
gr->SetMarkerStyle(20);
gr->SetLineColor(kRed);
gr->SetMarkerColor(kRed);
gr->Draw("APL");
}``````

Hi Couet,

I managed to get the graph…(finally…). I must say it was stupid mistakes…So problem solved!

Thanks!