Root macro draw problem

Dear all,

I created two loops of histos, HIS3[i] and H3[i], from different root files. then I tried to draw each one [i] in one pad by using the Draw option “same”. like:

TFile *f=new TFile("comparison.root","update");   
 sprintf(HFUNC2,"Avv","Zvv==%d");
         printf("HFUNC2=%s\n",HFUNC2);

     for (Int_t i=0;i<=30;i++){
        sprintf(TITLE2,"A{Z=%d}",i);
	sprintf(HNAME2,"mass%d",i);
        sprintf(SELECT2,"Zvv==%d",i);
	printf("SELECT2=%s\n",SELECT2);
	sprintf(HNAME2,"mass%d",i);
        HIS3[i] = new TH1F(HNAME2,TITLE2,60,0,60);
        HIS3[i]->GetXaxis()->SetTitle("Avv");
        h101->Project(HNAME2,HFUNC2,SELECT2);
HIS3->Draw();
HIS3->Write();

sprintf(HFUNC3,"A","Z==%d");
         printf("HFUNC3=%s\n",HFUNC3);
        sprintf(TITLE3,"A{Z=%d}",i);
	sprintf(Hist3,"mass%d",i);
        sprintf(SELECT3,"Z==%d",i);
	printf("SELECT3=%s\n",SELECT3);
	sprintf(Hist3,"mass%d",i);
        H3[i] = new TH1F(Hist3,TITLE3,60,0,60);
        H3[i]->GetXaxis()->SetTitle("A");
        tree->Project(Hist3,HFUNC3,SELECT3);
H3->Draw("SAME");
H3->Write();

and it doesn’t work.
please any help,
Thank you…

thats a bit vague… can you post a small example showing your problem ?

hi Couet,
thanks for trying help me,

I mean of “it doesn’t work” it’s keep drawing it in separate canvas,
I tried every thing, maybe there is something missing.

p.s,
“h101” and “tree” are tree file of root1 and root2 respectively, and Z, A, Zvv and Avv are leaves.

Basically your macro is this:

{
   TFile *f=new TFile("comparison.root","update");   
 
     for (Int_t i=0;i<=30;i++){
        HIS3[i] = new TH1F(HNAME2,TITLE2,60,0,60);
        HIS3[i]->GetXaxis()->SetTitle("Avv");
        h101->Project(HNAME2,HFUNC2,SELECT2);
        HIS3->Draw();
        HIS3->Write();
        H3[i] = new TH1F(Hist3,TITLE3,60,0,60);
        H3[i]->GetXaxis()->SetTitle("A");
        tree->Project(Hist3,HFUNC3,SELECT3);
        H3->Draw("SAME");
        H3->Write();
}

But it cannot work like that … for instance H3 is an array and then you acces it as a simple variable.
Can you provide a working version of this small script ?

hi couet,

my root files are too big I couldn’t attach it.

I have input and output data and i would to compare them,
my input root file include (h101) tree file with leaves (Avv and Zvv), and my output root file include (tree) tree file with leaves (A and Z).
i attached my macro, I hope you understand my point.

thank you.

comparison6.C (1.36 KB)
comparison6.root (25.6 KB)

hey,
I kept trying, and i need your help dears experts,

I made a simple macro that able me to draw two histograms in the same pad by getting the histograms one by one, as shown:

[code]{
gROOT->Reset();

// comparison6.root has the histos of input and output data

TFile f1(“comparison6.root”);

// i got the isotopes of z=0
// input
TH1D *h1 = f1.Get(“mass0”);
//output
TH1D *h2 = f1.Get(“MASS0”);

// draw it in the same canvas with logarithmic scale for y to be able to see the differences.

h1->Write();
h1->Draw();
h2->Write();
h2->Draw(“same”);

gPad->SetLogy();

return;

}[/code]

is there any idea to make the same for the rest of array or method to make a loop to z = 28? I would makes it in one macro.

I’m waiting for any help.
thanks.

Yes you can make a “for” loop to display all your histogram using the option SAME.

You can also use a THStack.
Sometimes it is more convenient to compute automatically the global scale. In that case you do the loop on the THStack::Add method.

Thank you couet,

Please could you help to make a loop to this one, i couldn’t do it, and the time runs up.
My histograms from mass0 to mass28,

 TFile f1("comparison6.root");   

TH1D *h1 = f1.Get("mass0");

[code]#include “TFile.h”
#include “TH1.h”
#include “THStack.h”
#include “TString.h”
#include “TROOT.h”
#include “TMath.h”
#include “TCanvas.h”
#include “TPad.h”

void comparison(const char *fname = “comparison6.root”, // input file name
const int n = 29) // number of histograms to process
{
if (!fname || !(*fname)) return; // just a precaution
if (n < 1) return; // just a precaution

TFile *f = TFile::Open(fname);
if (!f) return; // just a precaution

TH1F *h[n];
TH1F *H[n];

THStack *s = new THStack(“s”, “mass and MASS”);

for(int i = 0; i < n; i++) {
f->GetObject(TString::Format(“mass%d”, i), h[i]);
if (h[i]) {
h[i]->SetDirectory(gROOT); // gROOT or 0
s->Add(h[i]);
}
f->GetObject(TString::Format(“MASS%d”, i), H[i]);
if (H[i]) {
H[i]->SetDirectory(gROOT); // gROOT or 0
s->Add(H[i]);
}
}

delete f; // close the input file (no longer needed)

int m = TMath::Ceil(TMath::Sqrt(n));
TCanvas *c = new TCanvas(“c”, “c”);
c->Divide(m, ((m * (m - 1) < n) ? m : m - 1));
for(int i = 0; i < n; i++) {
if (!h[i] && !H[i]) continue; // just a precaution
c->cd(i + 1);
gPad->SetLogy();
s->Draw(“NOSTACK AXIS”);
if (h[i]) h[i]->Draw(“same”);
if (H[i]) H[i]->Draw(“same”);
gPad->RedrawAxis(); // re-draw “axes”
// gPad->RedrawAxis(“g”); // re-draw “grid”
}
c->cd(0);

TCanvas *chH = new TCanvas(“chH”, “chH”);
chH->SetLogy();
s->Draw(“NOSTACK”);
}[/code]

yes yes it’s done,
thank you so so much Wile E. Coyote.