Cannot access a histogram

I am creating an ntuple where I have saved some histograms. I am trying to open a specific histogram in order to apply some functions. The weird thing is that some functions work(i.e. Smooth() and Rebin()) while some others don’t.

Look for instance the following dialog

thanos@thanos-laptop:~/Desktop/offlineAnalysis/Temp$ root F_p_1000_Calibration.root root [0] Attaching file F_p_1000_Calibration.root as _file0... root [1] .ls TFile** F_p_1000_Calibration.root TFile* F_p_1000_Calibration.root KEY: TH1F histo_de1;1 #Delta E_{1} KEY: TH1F histo_e1;1 E_{1} KEY: TH1F histo_de2;1 #Delta E_{2} KEY: TH1F histo_e2;1 E_{2} KEY: TH1F histo_de3;1 #Delta E_{3} KEY: TH1F histo_e3;1 E_{3} KEY: TH2F dee1;1 #Delta E_{1} vs E_{1} KEY: TH2F dee2;1 #Delta E_{2} vs E_{2} KEY: TH2F dee3;1 #Delta E_{3} vs E_{3} KEY: TNtuple ntuple;1 DE/E analysis KEY: TH1D proj_e1;1 #Delta E_{1} vs E_{1} KEY: TH1D proj_e2;1 #Delta E_{2} vs E_{2} KEY: TH1D proj_e3;1 #Delta E_{3} vs E_{3} root [2] histo_e1 (class TH1F*)0x9d35370 root [3] histo_e1->Draw() Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1 root [4] histo_e1->Smooth(100) root [5] histo_e1->SetBins(histo_e1->GetNbinsX(),0,1024) Error: Symbol histo_e1 is not defined in current scope (tmpfile):1: Error: Failed to evaluate histo_e1->GetNbinsX() *** Interpreter error recovered ***

Somehow the histogram is not recognized! Why is this happening? The weird thing is that I have already applied some functions on that particular histogram.

Just for reference the code that I create the ntuple is the following

[code]#include "Riostream.h"
void ntuple(char * file_c) {

//*****************************************************
//* First executes the script “evnt2dat”.
//* The script accepts as an input a .evnt file
//* and creates a .dat file.
//* Then root creates the ntuple, histogram from each detector
//* and the DE-E scatter plot
//*
//* Execute it using
//* root -l ‘ntuple.C(“filename”)’
//* Note that the extension .evnt MUST NOT be used
//* and evnt2dat script must be on the same directory as
//* this macro.
//*****************************************************

TString file(file_c);
gSystem->Exec(TString::Format("./evnt2dat %s",file.Data()));//Executes the script evnt2dat
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“ntuple.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(TString::Format("%s%s_Processed.dat",dir.Data(),file.Data()));

gROOT->SetStyle(“Plain”);

Float_t de1,e1,de2,e2,de3,e3;
Int_t events = 0;
Int_t channels=4096;//Change it according to the acquisition channels
TFile *f = new TFile(TString::Format("%s.root",file.Data()),“RECREATE”);
//Create the histograms
TH1F *histo_de1 = new TH1F(“histo_de1”,"#Delta E_{1}",channels,1,channels);
TH1F *histo_e1 = new TH1F(“histo_e1”,“E_{1}”,channels,1,channels);
TH1F *histo_de2 = new TH1F(“histo_de2”,"#Delta E_{2}",channels,1,channels);
TH1F *histo_e2 = new TH1F(“histo_e2”,“E_{2}”,channels,1,channels);
TH1F *histo_de3 = new TH1F(“histo_de3”,"#Delta E_{3}",channels,1,channels);
TH1F *histo_e3 = new TH1F(“histo_e3”,“E_{3}”,channels,1,channels);
TH2F *dee1 = new TH2F(“dee1”, “#Delta E_{1} vs E_{1}”,channels,1,channels,channels,1,channels);
TH2F *dee2 = new TH2F(“dee2”, “#Delta E_{2} vs E_{2}”,channels,1,channels,channels,1,channels);
TH2F *dee3 = new TH2F(“dee3”, “#Delta E_{3} vs E_{3}”,channels,1,channels,channels,1,channels);

//Create the ntuple
TNtuple *ntuple = new TNtuple(“ntuple”,“DE/E analysis”,“de1:e1:de2:e2:de3:e3”);

//Fill the histos and the ntuple
while (1) {
in >> de1 >> e1 >> de2 >> e2 >> de3 >> e3;
if (!in.good()) break;
histo_de1->Fill(de1);
histo_e1->Fill(e1);
histo_de2->Fill(de2);
histo_e2->Fill(e2);
histo_de3->Fill(de3);
histo_e3->Fill(e3);
dee1->Fill(e1, de1);
dee2->Fill(e2, de2);
dee3->Fill(e3, de3);
ntuple->Fill(de1,e1,de2,e2,de3,e3);
events++;
}
printf(“Found %d events\n”,events);
//in.close();
//f->Write();
//TCanvas c1 = new TCanvas(“c1”, “c1”,225,219,700,530);
//c1->SetFillColor(5);
//c1->SetFrameFillColor(10);
//histo_de1->Rebin(4);
/**histo_de1->SetBins( histo_de1->GetNbinsX(), 0, 1024);
/
//--------------------------->histo_de1->Draw();
/**histo_de1->Rebin(4);*/
//c1->Mofified();
//c1->Update();

//TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
//c2->SetFillColor(5);
//c2->SetFrameFillColor(10);
//dee3->Draw(“COLZ”);
//c2->Modified();
//c2->Update();

TCanvas *c1 = new TCanvas(“c1”, “c1”,225,219,700,530);
TCutG *cut120 = new TCutG(“cut120”,6);
cut120->SetVarX("");
cut120->SetVarY("");
cut120->SetTitle(“Graph120”);
cut120->SetFillColor(1);
cut120->SetPoint(0,310,1800);
cut120->SetPoint(1,3880,840);
cut120->SetPoint(2,3920,330);
cut120->SetPoint(3,2050,600);
cut120->SetPoint(4,320,1330);
cut120->SetPoint(5,310,1800);

TH1D *proj_e1 = dee1->ProjectionX(“proj_e1”,1,4096,"[cut120]");
proj_e1->Draw();
proj_e1->Smooth(1000);
proj_e1->SetBins(proj_e1->GetNbinsX(), 0, 1024);
c1->Update();
proj_e1->Draw();
proj_e1->Rebin(4);
TString histfilename120 =TString::Format("%s_120.dat",file.Data());
SingleExportAscii(proj_e1,histfilename120);
TCanvas *c11 = new TCanvas(“c11”, “c11”,225,219,700,530);
//dee1->Draw("[cut120]");
dee1->Draw(“COLZ”);
cut120->Draw(“L”);
c11->Modify();
c11->Update();

TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
TCutG *cut170 = new TCutG(“cut170”,6);
cut170->SetVarX("");
cut170->SetVarY("");
cut170->SetTitle(“Graph170”);
cut170->SetFillColor(1);
cut170->SetPoint(0,170,2230);
cut170->SetPoint(1,3940,830);
cut170->SetPoint(2,3830,150);
cut170->SetPoint(3,1400,400);
cut170->SetPoint(4,160,840);
cut170->SetPoint(5,170,2230);

TH1D *proj_e2 = dee2->ProjectionX(“proj_e2”,1,4096,"[cut170]");
proj_e2->Draw();
proj_e2->Smooth(1000);
proj_e2->SetBins(proj_e2->GetNbinsX(), 0, 1024);
c2->Update();
proj_e2->Draw();
proj_e2->Rebin(4);
TString histfilename170 =TString::Format("%s_170.dat",file.Data());
SingleExportAscii(proj_e2,histfilename170);
TCanvas *c22 = new TCanvas(“c22”, “c22”,225,219,700,530);
//dee2->Draw("[cut170]");
dee2->Draw(“COLZ”);
cut170->Draw(“L”);
c22->Modify();
c22->Update();

TCanvas *c3 = new TCanvas(“c3”, “c3”,225,219,700,530);
TCutG *cut150 = new TCutG(“cut150”,6);
cut150->SetVarX("");
cut150->SetVarY("");
cut150->SetTitle(“Graph150”);
cut150->SetFillColor(1);
cut150->SetPoint(0,390,1100);
cut150->SetPoint(1,4030,750);
cut150->SetPoint(2,4040,30);
cut150->SetPoint(3,1650,160);
cut150->SetPoint(4,370,450);
cut150->SetPoint(5,390,1100);
TH1D *proj_e3 = dee3->ProjectionX(“proj_e3”,1,4096,"[cut150]");
proj_e3->Draw();
proj_e3->Smooth(1000);
proj_e3->SetBins(proj_e3->GetNbinsX(), 0, 1024);
c3->Update();
proj_e3->Draw();
proj_e3->Rebin(4);
TString histfilename150 =TString::Format("%s_150.dat",file.Data());
SingleExportAscii(proj_e3,histfilename150);
TCanvas *c33 = new TCanvas(“c33”, “c33”,225,219,700,530);
//dee3->Draw("[cut150]");
dee3->Draw(“COLZ”);
cut150->Draw(“L”);
c33->Modify();
c33->Update();
in.close();
f->Write();

//myhist.Print(“all”); > myhist.dat
//c3->SaveAs(TString::Format("%s.pdf",file.Data()));
//TCanvas *c4 = new TCanvas(“c4”, “c4”,225,219,700,530);
//dee3->Draw("[cut3]");
//dee3->Draw(“COLZ”);

//TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
//dee1->Draw(“COLZ”);

}

/**

  • \brief Export Single Histogram into ASCII file
    /
    Bool_t SingleExportAscii(TH1
    hist, TString &filename, TString folder="", TString separator="\t")
    {
    Int_t i,j;
    Double_t xcenter, xwidth;
    Bool_t success=kFALSE;
    //filename = folder + hist->GetName() + “.dat”;
    ofstream file_out(filename);
    file_out << "# Output " << hist->ClassName() << “: " << hist->GetName() << " (” << hist->GetTitle() << “)\n”;
    if (hist->GetDimension()==1)
    {
    file_out << “# BinCenter” << separator << “Content” << separator << “BinHalfWidth” << separator << “Error\n”;
    for (i=1; i<=hist->GetNbinsX(); i++)
    file_out << hist->GetBinCenter(i)-0.5 << separator << hist->GetBinContent(i) << separator << hist->GetBinWidth(i)/2 << separator << hist->GetBinError(i) << endl;
    if (i>1)
    success=kTRUE;
    }
    else if (hist->GetDimension()==2)
    {
    file_out << “# xBinCenter” << separator << “yBinCenter” << separator << “Content” << separator << “xBinHalfWidth” << separator << “yBinHalfWidth” << separator << “Error” << endl;
    for (i=1; i <= hist->GetNbinsX(); i++)
    {
    xcenter = hist->GetXaxis()->GetBinCenter(i);
    xwidth = hist->GetXaxis()->GetBinWidth(i)/2;
    for (j=1; j <= hist->GetNbinsY(); j++)
    file_out << xcenter << separator << hist->GetYaxis()->GetBinCenter(j) << separator << hist->GetBinContent(i,j) << separator << xwidth << separator << hist->GetYaxis()->GetBinWidth(j)/2 << separator << hist->GetBinError(i,j) << endl;
    if (j>1)
    file_out << endl; // produce a blank line after each set of Ybins for a certain Xbin. Gnuplot likes this.
    }
    if (i>1)
    success=kTRUE;
    }
    file_out.close();
    if (success == kTRUE)
    cout << "*** TRemiHistExport: Histogram " << hist->GetName() << " written to " << filename << endl;
    return success;
    }
    [/code]

I remember some problems in cases in which an “automatically created pointer” was used more than once in a single statement. Try: TH1F *h; gFile->GetObject("histo_e1", h); if (h) h->SetBins(h->GetNbinsX(), 0, 1024);

This did work really! Thank you very much!
I didn’t know that something like that could happen!
Do you have any idea why is this happening?