I am using the following script to loop over a set of files and draw histograms:
plot(TString& formu){
int counter = 0;
TCanvas* c = new TCanvas(formu,formu);
c->Divide(6,4);
TString cut("sum>8000 && sum<14000");
for (int y=2;y<6;y++){
for (int x=2;x<8;x++){
TString xs = TString::Format("%d",x);
TString ys = TString::Format("%d",y);
TString name = "./x_0"+xs+"_y_0"+ys+".root";
MyTree t(name);
c->cd(counter+1);
gStyle->SetHistLineColor(counter);
t.fChain->Draw(formu,cut);
counter += 1;
}
}
}
It works fine, but when i try to set the range of the histograms via calling the script
plot("myvariable >> (200,0,2)")
Then only the last histogram is shown.
What am I doing wrong?
Also, I would like to know how to change the color of the histogram (in case I want to put the histograms from all files in the same plot), gStyle->SetHistLineColor seems to have no effect.
EDIT: I tried to fix it by drawing a copy of the temporary histogram htemp:
//...
t.fChain->Draw(formu,cut,opt);
TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp"); // 1D
htemp->DrawCopy();
std::cout << htemp->GetName() << htemp->GetEntries() << std::endl;
//...
The behavior of this script is quite strange. When I remove the line “htemp->DrawCopy()” it works and I get the expected output. However, with the line I get a segmentation violation.
EDIT2: It seems like only when i call the script without a range (e.g. plot(“myvariable”)) the Draw uses the temporary TH1F called htemp. When I put a range (e.g. plot(“myvariable >> (200,0,2)”)) htemp is undefined.