Rebinning with variable bin size

Dear experts,

I have a simple question. I’m using root inside CMSSW " CMSSW_9_4_10". “root version is 6.10/09”
I was using variable bin size for long time without errors in my macro. Now I get error that the new histogram name not defined.

The steps I’m doing:

int n = 10;  
Double_t xbins[11]={5,7,10,20,30,40,50,80,90,100,150}; //n=10

TFile *f0 = TFile::Open("All_data.root")
TH1F * h1 = (TH1F*)f0->Get("ZplusM_Pt_DEN_Barrel");
h1->Rebin(n,"histoMudenBarrelnew",xbins); //Rebining
histoMudenBarrelnew->Draw()

I got this error:

**error:** **use of undeclared identifier 'histoMudenBarrelnew'**

histoMudenBarrelnew->Draw();

Error in <ACLiC>: Dictionary generation failed!
  • When I tried the same steps interactively in root without macro, it works fine but not inside my macro.

any idea what I have to do ?

Regards
Reham

TH1 *histoMudenBarrelnew = h1->Rebin(n, "histoMudenBarrelnew", xbins); //Rebining

or:

TH1F *histoMudenBarrelnew = ((TH1F*)(h1->Rebin(n, "histoMudenBarrelnew", xbins))); //Rebining

Thanks a lot Wile,

I used the first solution and It works fine now.

I have another question: I defined TGraphAsymmErrors from 2 rebind histograms but when I use this graph later I get error the is not defined (although I included the header file in the beginning of my macro #include “TGraphAsymmErrors.h”),

this what I’m doing

int n = 10;   
Double_t xbins[11]={5,7,10,20,30,40,50,80,90,100,150}; 
TFile *f0 = TFile::Open("All_data.root");

TH1F * h1 = (TH1F*)f0->Get("ZplusM_Pt_DEN_Barrel"); 
TH1 *histo_Mu_den_Barrel_new = h1->Rebin(n, "histo_Mu_den_Barrel_new", xbins); 

TH1F * h2 = (TH1F*)f0->Get("ZplusM_Pt_NUM_ID_ISO_Barrel");
TH1 *histo_Mu_num_Barrel_new = h2->Rebin(n,"histo_Mu_num_Barrel_new",xbins);
   
 TGraphAsymmErrors * Trg_Pt2Er_data_Barrel = new TGraphAsymmErrors(histo_Mu_num_Barrel_new,histo_Mu_den_Barrel_new); 

Trg_Pt2Er_data_Barrel->SetMarkerStyle(20);
Trg_Pt2Er_data_Barrel->SetMarkerColor(9);
Trg_Pt2Er_data_Barrel->SetLineColor(9);
Trg_Pt2Er_data_Barrel->Draw("p");

I got this errors

**error:** **use of undeclared identifier 'Trg_Pt2Er_data_Barrel'
Trg_Pt2Er_data_Barrel->SetMarkerStyle(20);

**error:** **use of undeclared identifier 'Trg_Pt2Er_data_Barrel'
Trg_Pt2Er_data_Barrel->SetMarkerColor(9);

**error:** **use of undeclared identifier 'Trg_Pt2Er_data_Barrel'
Trg_Pt2Er_data_Barrel->SetLineColor(9);

**error:** **use of undeclared identifier 'Trg_Pt2Er_data_Barrel'
Trg_Pt2Er_data_Barrel->Draw("p");

any Idea what I have to do ?

Thanks in advance.

Regards
Reham

Can it be that you define the “Trg_Pt2Er_data_Barrel” inside of a {...} block but then you try to access it outside if this block?

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Thanks a lot Wile,

Yes I defined the “Trg_Pt2Er_data_Barrel” inside of a {...} block but I defined a bool which is true for this block

Here all what I’m doing:

void calc() {

TCanvas *c_Ratio = new TCanvas("c_Ratio","c_Ratio",600,600);

bool Mu= true;
bool Ele= false;

int n = 10;   
Double_t xbins[11]={5,7,10,20,30,40,50,80,90,100,150}; 

TFile *f0 = TFile::Open("All_data.root");

 if(Mu== true){
TH1F * h1 = (TH1F*)f0->Get("ZplusM_Pt_DEN_Barrel"); 
TH1 *histo_Mu_den_Barrel_new = h1->Rebin(n, "histo_Mu_den_Barrel_new", xbins); 

TH1F * h2 = (TH1F*)f0->Get("ZplusM_Pt_NUM_ID_ISO_Barrel");
TH1 *histo_Mu_num_Barrel_new = h2->Rebin(n,"histo_Mu_num_Barrel_new",xbins);
   
 TGraphAsymmErrors * Trg_Pt2Er_data_Barrel = new TGraphAsymmErrors(histo_Mu_num_Barrel_new,histo_Mu_den_Barrel_new); 
}

else if (Ele == true ){

 TH1F * h3 = (TH1F*)f0->Get("ZplusE_Pt_DEN_Barrel"); 
 TH1 *histo_Ele_den_Barrel_new = h3->Rebin(n,"histo_Ele_den_Barrel_new",xbins); 
    
TH1F * h4 = (TH1F*)f0->Get("ZplusE_Pt_NUM_ID_ISO_Barrel"); 
 TH1 *histo_Ele_num_Barrel_new = h4->Rebin(n,"histo_Ele_num_Barrel_new",xbins);

 TGraphAsymmErrors * Trg_Pt2Er_data_Barrel = new TGraphAsymmErrors(histo_Ele_num_Barrel_new,histo_Ele_den_Barrel_new); 
}

c_Ratio->cd();

Trg_Pt2Er_data_Barrel->SetMarkerStyle(20);
Trg_Pt2Er_data_Barrel->SetMarkerColor(9);
Trg_Pt2Er_data_Barrel->SetLineColor(9);
Trg_Pt2Er_data_Barrel->Draw("p");

  if (Mu == true){c_Ratio->SaveAs("Mu.png");}
   else if (Ele == true){c_Ratio->SaveAs("Ele.png");}

 if (Mu == true) {TFile* f = TFile::Open("Mu.root","RECREATE");}
 else if (Ele == true){ TFile* f = TFile::Open("Ele.root","RECREATE");}

 f->cd();
 c_Ratio->Write();
 f->Close();

}

Before the “if”:

TGraphAsymmErrors *Trg_Pt2Er_data_Barrel = 0;

Inside of the “if / else if”:

Trg_Pt2Er_data_Barrel = new TGraphAsymmErrors(..);

After the “if / else if”:

if (Trg_Pt2Er_data_Barrel) {
  Trg_Pt2Er_data_Barrel->...
} else { std::cout << "No Trg_Pt2Er_data_Barrel!" << std::endl; }

Thanks a lot Wile… It works fine now. :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.