Re: [ROOT] update on histograms

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Jun 04 2002 - 08:29:35 MEST


Hi Frederic,

In your example, you are opening the output file in read only mode.
You should receive a waring message in this case when executing f->Write.
I suggest modidifications to your code in the following way:

//--- first step I define a root file and some histograms
TFile *f = new TFile("Results.root","recreate");
TH1F *hist = new TH1F("h","h",100,-2,2);

//-- then I loop on several data files (ROOT Tree)
while (nextDataFile()) {

  //--- I load the ROOT data file
  TFile * f2 = TFile::Open(CurrentDataFile);
  TTree * tree = (TTree *) f2->Get("Global");

  //--- I analyze each data files with a class created with
  //---  a MakeClass applied on one of the data files
  Analyze ana(tree);
  //-- then I loop on events and give my histogram in input
  //--- in order to fill it and "update"/"increment" it
  ana.Loop(*hist);
}

//--- then I write the histo in the file and close it
f->cd(); //important
f->Write();
f->Close()

Rene Brun

Frederic Villeneuve-Seguier wrote:
> 
> Hi again,
> 
>  Ok, I see my english is worse than I thought.
> What you proposed me to do is, if I well understood, to replace existing
> histograms that already exist (overwrite).
> This is not what I want to do. I may be confused you using the verb
> "update". When I said "update an histogram" and didn't mean I wanted to
> overwrite them, I want to "update"="increment" the number of entries.
> 
> Let me give you a short description of the procedure I want to use :
> 
> //--- first step I define a root file and some histograms
> TFile f("Results.root");
> TH1F hist("h","h",100,-2,2);
> 
> //-- then I loop on several data files (ROOT Tree)
> while (nextDataFile()) {
> 
>   //--- I load the ROOT data file
>   TFile * f2 = TFile::Open(CurrentDataFile);
>   TTree * tree = (TTree *) f2->Get("Global");
> 
>   //--- I analyze each data files with a class created with
>   //---  a MakeClass applied on one of the data files
>   Analyze ana(tree);
>   //-- then I loop on events and give my histogram in input
>   //--- in order to fill it and "update"/"increment" it
>   ana.Loop(hist);
> }
> 
> //--- then I write the histo in the file and close it
> f->Write();
> f->Close()
> 
> Other example : Imagine I want to plot the number of jets in one Run.
> This Run is composed by 10 files, I want my histos to keep memory and just
> increment data file from data file.
> 
> This doesn't work, my histogram is not updated ! I don't understand why !
> I tried to use the Write() method inside the Loop() but it creates me
> several cycles of my histos.
> Do I miss something ?
> 
> I tried to do that another way, and I prefer this way because it allows to
> stay more object oriented (better for C++ analysis)
> This way work but create cycles of the same histo, and I would have to
> merge them after, but I'm pretty sure you have a magical trick here
> to cure that.
> In this preocedure The main loop looks like that :
> 
> //-- then I loop on several data files (ROOT Tree)
> while (nextDataFile()) {
> 
>   //--- I load the ROOT data file
>   TFile * f2 = TFile::Open(CurrentDataFile);
>   TTree * tree = (TTree *) f2->Get("Global");
> 
>   //--- I analyze each data files with a class created with
>   //---  a MakeClass applied on one of the data files
>   Analyze ana(tree);
>   //-- then I loop on events and give my histogram in input
>   //--- in order to fill it and "update"/"increment" it
>   ana.Loop();
> }
> 
> I don't declare the histogram out of the loop.
> In my analysis class, I have a pointer to an object of type MyHistoMgr (a
> class of mine).
> This hstogram manager has a data member which is the histogram I want to
> fill. More exactly, the data member is a TH1F* .
> 
> In the Analyze::Loop() method, I initialize the pointer to the histogram
> manager (out of the loop on event) :
>   during this  initialization, I open "Results.root" (the output file
> with my histogram), then I check if this file is empty (just check if
> there are already the histogram I want) => if the file is empty, I create
> histograms, if not, I initialize the pointer from the histogram manager
> to the TH1F which has been previously defined.
> Then I fill this histos event by event, and write to Results.root and
> close the file.
> And I follow this procedure data files after data files.
> This works but create several cycles of the same histogram.
> 
> Is there a way to fill an histogram, write it into a root file, then close
> the file, open it again, add entries to the histograms, write it to the
> file, then close the file again .. and so on and so on without having
> several cycles of the histogram  ?
> 
> Please, if you need more explanation, I can try again to explain you
> what I want, so don't hesitate and tell me if I gave bad explanation ..
> 
> many thanks for the help,
> take care,
>                         frederic.
> 
> On Sat, 1 Jun 2002, Rene Brun wrote:
> 
> > Hi Frederic,
> >
> > Suppose the following session:
> > TFile f("junk.root","recreate");
> > TH1F h("h","h",100,-2,2);
> > h.Write();
> > h.Fill(1);
> > h.Write("",TObject::kOverwrite)
> > f.ls(); //this will produce
> > TFile**         junk.root
> >  TFile*         junk.root
> >   OBJ: TH1F     h       h : 0
> >   KEY: TH1F     h;1     h
> >
> > As you can see, using the kOverwite flag will force a replacement
> > of the previous object with the same name.
> >
> > Rene Brun
> >
> > On Wed, 29 May 2002, Frederic Villeneuve-Seguier wrote:
> >
> > >
> > > Hi,
> > > I probably gave bad explanation of what I want to do :
> > > I run on several data files. On each data files I apply a class that
> > > open/update a root files with some histograms.
> > > In order to update histograms, I check if they are defined, if not I
> > > create them, but if they are already define I declare pointers to them.
> > >
> > > This is working but in the root files where I store my histograms,
> > > I have duplicated histos (different cycles of the same histos)
> > >
> > > Is there a way to avoid duplication, and to update an histogram ?
> > >
> > > thanks,
> > > frederic.
> > >
> > >
> > >
> > > On Wed, 29 May 2002, Rene Brun wrote:
> > >
> > > > Hi Frederic,
> > > >
> > > > You must write the histograms to the file once you have filled them.
> > > > In your case,  add the statement
> > > >     f->Write();
> > > > just after the loop filling your histograms
> > > >
> > > > Rene Brun
> > > >
> > > >
> > > > On Tue, 28 May 2002, Frederic Villeneuve-Seguier wrote:
> > > >
> > > > >
> > > > >
> > > > > Hi all,
> > > > >
> > > > >
> > > > >  I want to study a large number of root files and I can't chain them.
> > > > > So I study files one by one.
> > > > > FOr somme coding reason, I need to apply a class inherited from a
> > > > > MakeClass() on each of the files.
> > > > > What I'm trying to do is :
> > > > > I open in the "update" mode, the file where I store the results of my
> > > > > studies. Then I declare (if it's the first time) or I "point at"
> > > > > the histograms and fill/update them.
> > > > > I use this in a C++ standalone mode.
> > > > >
> > > > > At the moment the file with the results is empty, and it seems that
> > > > > the histograms aren't detected. I coded it this way :
> > > > >
> > > > >   //--- Check if histos exists
> > > > >   TFile * f = TFile::Open("TriggStudies.root");
> > > > >   f->ls();
> > > > >   TH1F * check = NULL;
> > > > >   check = (TH1F * ) f->Get("sJ_nev_mu");
> > > > >   cout << "check = " << check << endl;
> > > > >   if (check == NULL) {
> > > > >     //--- Histograms Creation
> > > > >     cout << "| [#] Create Histograms for Trigger Studies" << endl;
> > > > >     diJets_Nevts_mu = new TH1F("sJ_nev_mu","N evt vs Pt of Jet - L1 mu",75,0,150);
> > > > >   } else {
> > > > >     diJets_Nevts_mu = (TH1F * ) f->Get("sJ_nev_mu");
> > > > >   }
> > > > >
> > > > > the problem I have is that data files after data files, the "check"
> > > > > pointer is everytime empty.
> > > > >
> > > > > Does someone has an idea on what could be the source of this problem ?
> > > > >
> > > > > In an other way of saying that, Is it possible to fill histograms in a
> > > > > root file, close it, then reopen this file, update the histograms
> > > > > and re-close it again, and so on ... ?
> > > > >
> > > > >
> > > > > many thanks,
> > > > > take care,
> > > > >                 frederic.
> > > > >



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:54 MET