Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:39

0001 /**
0002  * This macro takes a file and the name of a histogram and looks for
0003  * a histogram of the same name in all the subdirectories.
0004  * If it finds a match, it returns a pointer to it (but a TObject pointer
0005  * needs a cast to use it outside), if not returns
0006  * a null pointer and also does a std::cout saying it did not find it.
0007  * ATTENTION: if the same name matches more than one histogram in different
0008  * subdirectories, only the last match will be returned and a warning will be
0009  * given. In this case, the directory name can be passed to select the correct match.
0010  */
0011 
0012 // If you need to compile this macro, uncomment the following includes:
0013 // --------------------------------------------------------------------
0014 // Needed to use gROOT in a compiled macro
0015 #include "TROOT.h"
0016 #include "TH1.h"
0017 #include "TChain.h"
0018 #include "TFile.h"
0019 #include "TTree.h"
0020 #include "TKey.h"
0021 #include "Riostream.h"
0022 #include "TCanvas.h"
0023 
0024 class HistoFinder {
0025  public:
0026   HistoFinder() { histoFound_ = 0; }
0027   TObject * operator()( const TString & name, TDirectory * input, const TString & dirName = "" ) {
0028     histoFound_ = 0;
0029     histoFoundNum_ = 0;
0030     findHisto_( name, input, dirName );
0031     if( histoFound_ == 0 ) std::cout << "WARNING: histogram " << name << " not found" << std::endl;
0032     if( histoFoundNum_ > 1 ) std::cout << "WARNING: more than one match found. Please specify the directory" << std::endl;
0033     return histoFound_;
0034   }
0035  protected:
0036   void findHisto_( const TString & name, TDirectory * input, const TString & dirName = "" ) {
0037 
0038     TIter nextkey( input->GetListOfKeys() );
0039     TKey *key, *oldkey=0;
0040     while ( (key = (TKey*)nextkey())) {
0041 
0042       //keep only the highest cycle number for each key
0043       if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
0044 
0045       // read object from first source file
0046       TObject *obj = key->ReadObj();
0047 
0048       if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
0049         // If it matches the name, return it
0050         if( dirName == "" || dirName == input->GetName() ) {
0051           if( name == obj->GetName() ) {
0052             histoFound_ = obj;
0053             ++histoFoundNum_;
0054           }
0055         }
0056       }
0057       else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
0058         // it's a subdirectory
0059         // std::cout << "Found subdirectory " << obj->GetName() << std::endl;
0060         findHisto_( name, (TDirectory*)obj, dirName );
0061       }
0062       else {
0063         // object is of no type that we know or can handle
0064         std::cout << "Unknown object type, name: "
0065              << obj->GetName() << " title: " << obj->GetTitle() << std::endl;
0066       }
0067     }
0068   }
0069   TObject * histoFound_;
0070   int histoFoundNum_;
0071 };