Saving Tlist in root file

Dear All,
I am having problem to save my list of histograms into a root file, actually if I do something like:

file->Write()

All the histograms objects are saved into the root file except the Collection list object.
Is there any specific way to save the Tlist objects ?
I would apreciate if someone could help.
Thanks in advance.
–Diallo


_ROOT Version:6.12/06

Hi Diallo,

When you saving TList into the file, you should specify extra option:

file->cd()
lst->Write("name", 1)

Regards,
Sergey

Hi Sergey,
Thanks for replying yeah that works :wink: .
Actually I did that because I was suspecting that the histograms saved in my Tlists (and later merged) are different from the histograms I am expecting according to my code. I don’t know how is it possible but that’s what I find out. For instance here the part of my code which is doing the job (from line 278 ):

#define abcd_method_cxx
#include "abcd_method.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCollection.h>


void abcd_method::Begin(TTree * /*tree*/)
{

  
    TString file = GetOption();
   if (file.IsNull())
     file="summary.root";
   else {
     Ssiz_t pos = file.Last('.');
     if (pos<0) pos=file.Length();
     file.Replace(pos,20,".root");
   }
   fOut = TFile::Open(file, "RECREATE");


Int_t i, j;
    const char *i_name[] = {"4e", "2e2m", "4m"};
    const int j_number[] = {341488,341947,341964,344973,344974,345046,345047,345048,345060,345066,345708,345709,361601,364243,364245,364247,364248,364250,364251,364252,364364,410142,410472};

   
for (int i = 0; i < 3; i++) for (int j = 0; j < 23; j++) {
    hist[i][j] =
      new TH1F(TString::Format("hist_%s_%d", i_name[i], j_number[j]),
               "all channel;cut;events", 5, 0.5, 5.5);
    TAxis *a = hist[i][j]->GetXaxis();
    a->SetBinLabel(1, "all");
    a->SetBinLabel(2, "A");
    a->SetBinLabel(3, "B");
    a->SetBinLabel(4, "C");
    a->SetBinLabel(5, "D");
  }


}
void abcd_method::SlaveBegin(TTree * /*tree*/)
{
   // The SlaveBegin() function is called after the Begin() function.
   // When running with PROOF SlaveBegin() is called on each slave server.
   // The tree argument is deprecated (on PROOF 0 is passed).

  

  }

Bool_t abcd_method::Process(Long64_t entry)
{
   fChain->GetEntry(entry);
     int Ncut = 1;

TLorentzVector v4l = *lep1234;
    double M4l = lep1234->M() * 1e-3; // GeV
    TLorentzVector v12 = *lep12;
    double M12 = lep12->M() * 1e-3; // GeV
    TLorentzVector v34 = *lep34;
    double M34 = lep34->M() * 1e-3; // GeV

     TLorentzVector v32 = *lep32;
     TLorentzVector v1 = *lep1;
     TLorentzVector v2 = *lep2;
     TLorentzVector v3 = *lep3;
     TLorentzVector v4 = *lep4;

     TLorentzVector Pair_12;
     TLorentzVector Pair_34;
     TLorentzVector Pair_13;
     TLorentzVector Pair_24;
     TLorentzVector Pair_14;
     TLorentzVector Pair_23;

     double M_Pair_12; 
     double M_Pair_34; 
     double M_Pair_13; 
     double M_Pair_24;
     double M_Pair_14;
     double M_Pair_23;
	   
     double Pt1 = lep1->Pt() * 1e-3; // GeV
     double Pt2 = lep2->Pt() * 1e-3; // GeV
     double Pt3 = lep3->Pt() * 1e-3; // GeV
     double Pt4 = lep4->Pt() * 1e-3; // GeV
     double MZ = 91.18;
     double d0SigLepA;
     double d0SigLepB;
     double d0SigLepC;
     double d0SigLepD;

     double IdLepA;
     double IdLepB;
     double IdLepC;
     double IdLepD;

     //Defining pairs which make sense
     
     if (IdLep1 + IdLep2 == 0)
       {
	Pair_12 =  *lep1 + *lep2; M_Pair_12 = Pair_12.M() * 1e-3;
	Pair_34 =  *lep3 + *lep4; M_Pair_34 = Pair_34.M() * 1e-3;
	
	double Comp12 = fabs(M_Pair_12-MZ);
	double Comp34 = fabs(M_Pair_34-MZ);
	if (Comp12 > Comp34)
	  {
	    d0SigLepC = d0SigLep1; d0SigLepD = d0SigLep2;
	    d0SigLepA = d0SigLep3; d0SigLepB = d0SigLep4;

	    IdLepC = IdLep1; IdLepD = IdLep2;
	    IdLepA = IdLep3; IdLepB = IdLep4;
	    
	  }
	else if (Comp12 < Comp34)
	    {
	      d0SigLepC = d0SigLep3; d0SigLepD = d0SigLep4;
	      d0SigLepA = d0SigLep1; d0SigLepB = d0SigLep2;

	      IdLepC = IdLep3; IdLepD = IdLep4;
	      IdLepA = IdLep1; IdLepB = IdLep2;

	    }
	
       }
     
     else if (IdLep1 + IdLep3 == 0)
       {
	 Pair_13 =  *lep1 + *lep3; M_Pair_13 = Pair_13.M() * 1e-3; 
	 Pair_24 =  *lep2 + *lep4;M_Pair_24  = Pair_24.M() * 1e-3;
	  double Comp13 = fabs(M_Pair_13-MZ);
	 double Comp24 = fabs(M_Pair_24-MZ);
	 if (Comp13 > Comp24)
	   {
	     d0SigLepC = d0SigLep1; d0SigLepD = d0SigLep3;
	     d0SigLepA = d0SigLep2; d0SigLepB = d0SigLep4;

	     IdLepC = IdLep1; IdLepD = IdLep3;
	     IdLepA = IdLep2; IdLepB = IdLep4;
	     
	   }
	 else if (Comp13 < Comp24)
	   {
	     d0SigLepC = d0SigLep2; d0SigLepD = d0SigLep4;
	     d0SigLepA = d0SigLep1; d0SigLepB = d0SigLep3;
	   }
	}
	if (IdLep1 + IdLep4 == 0)
	{
	Pair_14 =  *lep1 + *lep4; M_Pair_14 = Pair_14.M() * 1e-3; 
	Pair_23 =  *lep2 + *lep3; M_Pair_23 = Pair_23.M() * 1e-3;

	 double Comp14 = fabs(M_Pair_14-MZ);
	 double Comp23 = fabs(M_Pair_23-MZ);
	 if (Comp14 > Comp23)
	   {
	     d0SigLepC = d0SigLep1; d0SigLepD = d0SigLep4;
	     d0SigLepA = d0SigLep2; d0SigLepB = d0SigLep3;

	     IdLepC = IdLep1; IdLepD = IdLep4;
	     IdLepA = IdLep2; IdLepB = IdLep3;
	   }
	 else if (Comp14 < Comp23)
	   {
	     d0SigLepC = d0SigLep2; d0SigLepD = d0SigLep3;
	     d0SigLepA = d0SigLep1; d0SigLepB = d0SigLep4;
	     IdLepC = IdLep2; IdLepD = IdLep3;
	     IdLepA = IdLep1; IdLepB = IdLep4;
	   }

	
	}
 double M32 = lep32->M() * 1e-3; // GeV
     TLorentzVector v14 = *lep14;
     double M14 = lep14->M() * 1e-3; // GeV
     double avMll = (M12+M34)/2;
     if ((fabs(IdLepA)==13 && d0SigLepA < 3.) || (fabs(IdLepA)==11 && d0SigLepA < 5.)) lepton_d0Sig[0] = true; 
     else lepton_d0Sig[0] = false;
     //  if (lepton_d0Sig[0] == 1) {std::cout << "fabs(IdLep1) = " <<fabs(IdLep1) << std::endl; std::cout << "d0SigLep1 = " << d0SigLep1 << std::endl;}
     // if (fabs(IdLep1)==11) std::cout << "d0SigLep1_electron = " << d0SigLep1 << std::endl;
     if ((fabs(IdLepB)==13 && d0SigLepB < 3.) || (fabs(IdLepB)==11 && d0SigLepB < 5.)) lepton_d0Sig[1] = true;
     else lepton_d0Sig[1] = false;
     if ((fabs(IdLepC)==13 && d0SigLepC < 3.) || (fabs(IdLepC)==11 && d0SigLepC < 5.)) lepton_d0Sig[2] = true;
     else lepton_d0Sig[2] = false;
     if ((fabs(IdLepD)==13 && d0SigLepD < 3.) || (fabs(IdLepD)==11 && d0SigLepD < 5.)) lepton_d0Sig[3] = true;
     else lepton_d0Sig[3] = false;
     d0SigB_allpass      =    (lepton_d0Sig[0])  && (lepton_d0Sig[1])  && (lepton_d0Sig[2])  && (lepton_d0Sig[3]);
     d0SigB_34Npass      =    (lepton_d0Sig[0])  && (lepton_d0Sig[1])  && (!lepton_d0Sig[2]) && (!lepton_d0Sig[3]) ;
     d0SigB_3pass_4Npass =    (lepton_d0Sig[0])  && (lepton_d0Sig[1])  && (lepton_d0Sig[2])  && (!lepton_d0Sig[3]);
     d0SigB_3Npass_4pass =    (lepton_d0Sig[0])  && (lepton_d0Sig[1])  && (!lepton_d0Sig[2]) && (lepton_d0Sig[3]);
     //  std::cout << lepton_d0Sig[0]<< std::endl;

   //applying alectron ID cut

     // if (!(elIDLep1 < 3 && elIDLep2 < 3 && elIDLep3 < 3 && elIDLep4 < 3))return kTRUE;   
    

if (!Q_Veto)return kTRUE;
Int_t i, j;

if (llll_pdgIdSum == 44) i = 0; // 44 = "4e"
else if (llll_pdgIdSum == 48) i = 1; // ... = "2e2m"
else if (llll_pdgIdSum == 52) i = 2; // ... = "4m"
else return kFALSE; // unexpected llll_pdgIdSum

      if (MC_channel_number == 341488) j = 0;
 else if (MC_channel_number == 341947) j = 1;	 
 else if (MC_channel_number == 341964) j = 2;	
 else if (MC_channel_number == 344973) j = 3;	
 else if (MC_channel_number == 344974) j = 4;	
 else if (MC_channel_number == 345046) j = 5;	
 else if (MC_channel_number == 345047) j = 6;	
 else if (MC_channel_number == 345048) j = 7;	
 else if (MC_channel_number == 345060) j = 8;	
 else if (MC_channel_number == 345066) j = 9;	
 else if (MC_channel_number == 345708) j = 10; 
 else if (MC_channel_number == 345709) j = 11; 
 else if (MC_channel_number == 361601) j = 12; 
 else if (MC_channel_number == 364243) j = 13;
 else if (MC_channel_number == 364245) j = 14;
 else if (MC_channel_number == 364247) j = 15;
 else if (MC_channel_number == 364248) j = 16;
 else if (MC_channel_number == 364250) j = 17;
 else if (MC_channel_number == 364251) j = 18;
 else if (MC_channel_number == 364252) j = 19;
 else if (MC_channel_number == 364364) j = 20;
 else if (MC_channel_number == 410142) j = 21;
 else if (MC_channel_number == 410472) j = 22;
 else return kFALSE; // unexpected MC_channel_number

      if (llll_pdgIdSum !=52){

hist[i][j]->Fill(1, EvtWeight);
if (l_isIsolFixedCutLoose == 15 && d0SigB_allpass) hist[i][j]->Fill(2, EvtWeight);
if(l_isIsolFixedCutLoose==15 && (d0SigB_34Npass || d0SigB_3pass_4Npass ||d0SigB_3Npass_4pass))hist[i][j]->Fill(3, EvtWeight);
if(d0SigB_allpass && (l_isIsolFixedCutLoose==12 ||l_isIsolFixedCutLoose==14 || l_isIsolFixedCutLoose==13))hist[i][j]->Fill(4, EvtWeight);
 if((d0SigB_34Npass || d0SigB_3pass_4Npass ||d0SigB_3Npass_4pass) && (l_isIsolFixedCutLoose==12 ||l_isIsolFixedCutLoose==14 || l_isIsolFixedCutLoose==13)) hist[i][j]->Fill(5, EvtWeight);}

      if (llll_pdgIdSum ==52)
	{
	 if (d0SigLep2 >= 3 || d0SigLep3 >= 3)return kTRUE;
	 if(d0SigLep2 < 3 && d0SigLep3 < 3) hist[i][j]->Fill(1, EvtWeight);
	 if(d0SigB_allpass) hist[i][j]->Fill(2, EvtWeight);
	 if((d0SigLep1 < 3) && (d0SigLep4 >= 3)) hist[i][j]->Fill(3, EvtWeight);
	 if (d0SigLep1 >= 3 && d0SigLep4 >= 3) hist[i][j]->Fill(5, EvtWeight);
      }


return kTRUE;
}

void abcd_method::SlaveTerminate()
{
   // The SlaveTerminate() function is called after all entries or objects
   // have been processed. When running with PROOF SlaveTerminate() is called
   // on each slave server.
  
   

}

void abcd_method::Terminate()
{
 TString file_input[]={"./input_file2.dat"};
 TString tmps;
 ifstream fin(file_input[0]);
 const unsigned int  Npoints = 23;
 double DatasetID[Npoints];
 double Cross_sect[Npoints];
 double kfactor[Npoints];
 double genFiltEff[Npoints];
 double events_all[Npoints];
 double Normar[Npoints];
 // fin>>tmps;fin>>tmps;fin>>tmps;fin>>tmps;fin>>tmps;//read first line.

 for (Int_t j=0; j<Npoints; j++)
   {
   
   fin>>tmps;DatasetID[j]=tmps.IsFloat()?tmps.Atof():0;
   fin>>tmps; Cross_sect[j]=tmps.IsFloat()?tmps.Atof():0;
   fin>>tmps; kfactor[j]=tmps.IsFloat()?tmps.Atof():0;
   fin>>tmps; genFiltEff[j]=tmps.IsFloat()?tmps.Atof():0;
   fin>>tmps; events_all[j]=tmps.IsFloat()?tmps.Atof():0;
   }
 // std::cout << "CrossSect = " << Cross_sect[0] << std::endl;
 
 TList *list_4e = new TList;
 TList *list_2e2m = new TList;
 TList *list_4m = new TList;
 TH1F *hist_all_bkg_4e = (TH1F*)hist[0][0]->Clone();
 TH1F *hist_all_bkg_2e2m = (TH1F*)hist[0][0]->Clone();
 TH1F *hist_all_bkg_4m = (TH1F*)hist[0][0]->Clone();
 hist_all_bkg_4e->SetName("hist_all_bkg_4e");
 hist_all_bkg_2e2m->SetName("hist_all_bkg_2e2m");
 hist_all_bkg_4m->SetName("hist_all_bkg_4m");
 hist_all_bkg_4e->Reset();
 hist_all_bkg_2e2m->Reset();
 hist_all_bkg_4m->Reset();


 
 
 
 for (int i = 0; i < 3; i++) for (int j = 0; j < Npoints; j++)
			       {
      
				 Normar[j]= (Cross_sect[j]*kfactor[j]*genFiltEff[j]*lumi)/events_all[j];
				 hist[i][j]->Scale(Normar[j]);
				 if (i == 0) list_4e->Add(hist[i][j]);
				 else if (i == 1) list_2e2m->Add(hist[i][j]);
				 else if (i == 2) list_4m->Add(hist[i][j]);
			       }

 /* for (int j = 0; j < Npoints; j++)
   {
list_4m->Add(hist[2][j]);
   }*/

 
 
      hist_all_bkg_4e->Merge(list_4e);
      hist_all_bkg_2e2m->Merge(list_2e2m);
      hist_all_bkg_4m->Merge(list_4m);
        hist_all_bkg_4m->Print("All");
      // TList *list = (TList*)f->Get("chist");
       list_4m->Print("");
      // hist_all_bkg_4e->Draw();
       TFile* ftest = new TFile("list4muon.root","recreate");
	 // list_4m->Write();
	 list_4m->Write("list_4m", 1);
	 ftest->Write();
	 ftest->Close();
       
      

     
     TString summary = GetOption();
   if (summary.IsNull()) {
     if (fOut && fOut->IsOpen()) {
       // list_4m->Write("list_4m");
       fOut->Write();
       fOut->Close();
     }
   } else {
     Ssiz_t pos = summary.Last('.');
     if (pos<0) pos=summary.Length();
     summary.Replace(pos,20,".pdf");
   }
   
    if (fOut && fOut->IsOpen()) {
      // list_4m->Write("list_4m");
     fOut->Write();
     fOut->Close();
     }
  //  hCuts_4e_345709->Draw();
}

And this 2 root file are my outputs, one the Tlist (list4muon.root) and the other one containes all the histograms (summary.root). And you can see that the same histograms contained in the Tlist are different from the ones in the summary.root which is strange for me since they are from the same code.
Can someone tell me what happen please ?
if you want to run the code I can send you .h file and the root input file.
Thanks in advance.
Cheers,
Diallo
summary.root (43.4 KB)
list4muon.root (6.5 KB)

Hi Diallo,

Why you say that histograms are different?
If I just take first from the TList - it is same in both files.
Look:

http://jsroot.gsi.de/latest/?file=../files/tmp/list4muon.root&item=list_4m/hist_4m_341488
http://jsroot.gsi.de/latest/?file=../files/tmp/summary.root&item=hist_4m_341488

Regards,
Sergey

Hi Sergy,
I am sorry but I think I did a mistake by running on different input files that’s why I had the 2 outputs files different, but after checking (and be sure that I run on the same input file)I can see that they are the same as expected.
Sorry again and thanks for looking into this.
Cheers,
Diallo.

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