File indexing completed on 2024-04-06 12:22:44
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0043 if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
0044
0045
0046 TObject *obj = key->ReadObj();
0047
0048 if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
0049
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
0059
0060 findHisto_( name, (TDirectory*)obj, dirName );
0061 }
0062 else {
0063
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 };