Happy New year to everyone.
I have problem, trying to loop through a list of .root file, get histogram B, do calculation with histogram A, den put histograms A and B on same canvas, den save each individual canvas. Trouble is saving each individual histogram A and B for each file.
A sample of the code:
void contam_study()
{
char const *infile = "Bgoodndst.list";
TFile *file = TFile::Open("Atrigger_goodndst.root");
TTree *tree = (TTree*)file->Get("tree");
Int_t nevents = tree->GetEntries();
cout << "Nevents = " << nevents << endl;
Int_t s_stvar;
Float_t stvar[50];
TBranch *s_stv_branch = tree->GetBranch("s_stvar");
s_stv_branch->SetAddress(&s_stvar);
TBranch *ntv_branch = tree->GetBranch("stvar");
ntv_branch->SetAddress(&stvar);
//histogram A abd B
TH1F *hsq = new TH1F("hsq","bcq",100,0.,2500);
// hsq->SetBit(TH1::kCanRebin);
TH1F *h_sq = new TH1F("h_sq","bnsq",100,0,2500);
// get variables from fileA
for (Int_t ievt=0; ievt<n events; ievt++)
{
tree->GetEvent(ievt);
float bsum = stvar[2] + stvar[3];
hsq->Fill(bsum);
}//end of for loop
//file B files one at time, den,calculate
ifstream runlist(infile);//file stream
char filepath [900];
TFile *tfile[10000];
int nfile = 0;
if(runlist)
{
while( runlist.good() ) // continue reading file
{
runlist.getline(filepath,900);
if ( runlist.eof() ) break;
tfile[nfile] = new TFile(filepath, "read");
if ( tfile[nfile] )
{
tfile[nfile]->GetObject("h_sum",h_sq);
}
TCanvas *bb = new TCanvas("bb","A/B_sum",550,425);
TH1 *histo = hsq->Clone("histo");
histo->Divide(h_sq);
TF1 *poly0 = new TF1("poly0","pol0",1800,2000);
histo->Fit(poly0,"R");
gPad->SetLogy(1);
Double_t scale = poly0->GetParameter(0);
cout << "scale = " << scale << endl;
THStack *hs = new THStack("hs","Stacked histogram");
hs->Add(hsq);
hsq->SetLineColor(4);
hs->Add(h_sq);
h_sq->SetLineColor(2);
h_sq->Scale(scale);
hs->Draw();
tfile[nfile]->Close(); //lock di file
nfile++;//stepwise
}
}
else
{
cout << "file " << infile << " does not exist" << endl;
}
//fi save indiv plots
TFile *h = new TFile("outfile.root", "RECREATE");
h->cd();
hs->Write();//fi di individual files out.
runlist.close();//close list file.
}//end of void
Awaits your kind assistance.
Cheers
Prya13