Re: Merging histograms

From: Ida Häggström <ida.haggstrom_at_radfys.umu.se>
Date: Wed, 9 Nov 2011 15:58:08 +0100


Hello Philippe and thanks for your help!

Detaching the histogram seems to have done the trick! =) I don't get any errors so everything seems correct so far... Thanks! Below is the code snippet for anyone who's interested.
Cheers,
Ida



 // Add all ROOT-files in given directory  TSystemDirectory* dataDir = new
TSystemDirectory("",MyDirectoryWithRootFiles);
 TList*       contents = dataDir->GetListOfFiles();
 TIter        next(contents);

 TSystemFile* file;
 TList *listOfHistograms = new TList;
 Int_t i = 0;

 // Loop over all files in directory
 while (( file = (TSystemFile*)next() )) {

    if ( file->IsDirectory() ) { continue; }     // Add if root-file...
    if ( (TString(file->GetName())).EndsWith(".root") ) {

        TFile f( file->GetName() );
        if ( i==0 ){
             TH1F *AllHistograms= (TH1F*)Ion_decay_time_s->
Clone("AllHistograms");
             AllHistograms->Reset();

    }
    Ion_decay_time_s->SetDirectory(0);
    AllHistograms->Add( Ion_decay_time_s );     }
 }
AllHistograms->Draw();

On Wed, Nov 9, 2011 at 2:47 PM, Philippe Canal <pcanal_at_fnal.gov> wrote:

> Hi Ida,
>
> One thing missing from the code snippet is the way the TFile are opened
> and close.
> For the case where the Merge function is used, either the TFile must be
> kept open
> or the the histogram must be detached from the file (
> Ion_decay_time_s->SetDirectory(0) ).
>
> Either way the alternative:
>
> AllHistograms->Add( Ion_decay_time_s ); <--- Alternative I tried with
> no success either....
>
> should apriori have worked (unless the range/binning are not always the
> same).
>
> Cheers,
> Philippe.
>
> On 11/9/11 6:17 AM, Ida Häggström wrote:
> > Hello! (please disregard my previous post as it was sent by accident!)
> >
> > I have a bunch of ROOT-files, each containing several histograms and
> some trees. I'm trying to merge one of the histograms (same
> > in all ROOT-files, called "Ion_decay_time_s", all of equal size) but
> can't really get it to work. The code below is what I've
> > tried with no success... I end up with "*** Break *** segmentation
> violation" when running (root version 5.28/00a). Any ideas?
> > Thanks!
> > Ida
> >
> > Here's my code:
> > ---------------------------------
> > // Add all ROOT-files in given directory
> > TSystemDirectory* dataDir = new
> TSystemDirectory("",MyDirectoryWithRootFiles);
> > TList* contents = dataDir->GetListOfFiles();
> > TIter next(contents);
> > TSystemFile* file;
> > TList *listOfHistograms = new TList;
> > Int_t i = 0;
> >
> > // Loop over all files in directory
> > while (( file = (TSystemFile*)next() )) {
> > if ( file->IsDirectory() ) { continue; }
> > // Add if root-file...
> > if ( (TString(file->GetName())).EndsWith(".root") ) {
> > if ( i==0 ){
> > TH1F *AllHistograms=
> (TH1F*)Ion_decay_time_s->Clone("AllHistograms");
> > AllHistograms->Reset();
> > }
> > listOfHistograms->Add( Ion_decay_time_s );
> > //AllHistograms->Add( Ion_decay_time_s ); <--- Alternative I tried
> with no success either....
> > }
> > }
> >
> > AllHistograms->Merge(listOfHistograms);
> > AllHistograms->Draw();
> > ---------------------------------
>
Received on Wed Nov 09 2011 - 15:58:21 CET

This archive was generated by hypermail 2.2.0 : Wed Nov 09 2011 - 17:50:01 CET