Helper.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id:  cranmer $
00002 // Author: Kyle Cranmer, Akira Shibata
00003 /*************************************************************************
00004  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 //_________________________________________________
00012 /*
00013 BEGIN_HTML
00014 <p>
00015 </p>
00016 END_HTML
00017 */
00018 //
00019 
00020 
00021 
00022 //#define DEBUG
00023 #include "Helper.h"
00024 
00025 namespace RooStats{
00026 namespace HistFactory{
00027   vector<pair<string, string> > get_comb(vector<string> names){
00028     vector<pair<string, string> > list;
00029     for(vector<string>::iterator itr=names.begin(); itr!=names.end(); ++itr){
00030       vector<string>::iterator itr2=itr; 
00031       for(itr2++; itr2!=names.end(); ++itr2){
00032         list.push_back(pair<string, string>(*itr, *itr2));
00033       }
00034     }
00035     return list;
00036   }
00037 
00038   vector<EstimateSummary>*  loadSavedInputs(TFile* outFile, string channel ){
00039     outFile->ShowStreamerInfo();
00040 
00041     vector<EstimateSummary>* summaries = new  vector<EstimateSummary>;
00042     outFile->cd(channel.c_str());
00043 
00044     // loop through estimate summaries
00045     TIter next(gDirectory->GetListOfKeys()); 
00046     EstimateSummary* summary; 
00047     while ((summary=(EstimateSummary*) next())) { 
00048 //      if(summary){
00049         summary->Print();
00050         cout << "was able to read summary with name " << summary->name << endl;
00051         cout << " nominal hist = " << summary->nominal << endl;
00052         if(summary->nominal)
00053            cout << " hist name = " << summary->nominal->GetName() <<endl;
00054         cout << "still ok" << endl;
00055        
00056         summaries->push_back(*summary);
00057 
00058 //L.M. This code cannot be reached- remove it 
00059 //       }
00060 //       else{
00061 //         cout << "was not able to read summary" << endl;
00062 //       }
00063     } 
00064     return summaries;
00065   }
00066 
00067 
00068   void saveInputs(TFile* outFile, string channel, vector<EstimateSummary> summaries){
00069     vector<EstimateSummary>::iterator it = summaries.begin();
00070     vector<EstimateSummary>::iterator end = summaries.end();
00071     vector<TH1F*>::iterator histIt;
00072     vector<TH1F*>::iterator histEnd;
00073     outFile->mkdir(channel.c_str());
00074 
00075     for(; it!=end; ++it){
00076       if(channel != it->channel){
00077         cout << "channel mismatch" << endl;
00078         return;
00079       }
00080       outFile->cd(channel.c_str());
00081       
00082       // write the EstimateSummary object
00083       it->Write();
00084 
00085       gDirectory->mkdir(it->name.c_str());
00086       gDirectory->cd(it->name.c_str());
00087 
00088       it->nominal->Write();
00089 
00090       histIt = it->lowHists.begin();
00091       histEnd = it->lowHists.end();
00092       for(; histIt!=histEnd; ++histIt)
00093         (*histIt)->Write();
00094 
00095       histIt = it->highHists.begin();
00096       histEnd = it->highHists.end();
00097       for(; histIt!=histEnd; ++histIt)
00098         (*histIt)->Write();
00099       
00100     }
00101   }
00102 
00103 
00104   TH1F * GetHisto( TFile * inFile, const string name ){
00105 
00106   if(!inFile || name.empty()){
00107     cerr << "Not all necessary info are set to access the input file. Check your config" << endl;
00108     cerr << "fileptr: " << inFile
00109          << "path/obj: " << name << endl;
00110     return 0;
00111   }
00112   #ifdef DEBUG
00113     cout << "Retrieving " << name ;
00114   #endif
00115     TH1F * ptr = (TH1F *) (inFile->Get( name.c_str() )->Clone());  
00116   #ifdef DEBUG
00117     cout << " found at " << ptr << " with integral " << ptr->Integral() << " and mean " << ptr->GetMean() << endl;
00118   #endif
00119     if (ptr) ptr->SetDirectory(0); //         for the current histogram h
00120     //TH1::AddDirectory(kFALSE);
00121     return ptr;
00122 
00123   }
00124 
00125   TH1 * GetHisto( const string file, const string path, const string obj){
00126 
00127   #ifdef DEBUG
00128     cout << "Retrieving " << file << ":" << path << obj ;
00129   #endif
00130     TFile inFile(file.c_str());
00131     TH1 * ptr = (TH1 *) (inFile.Get( (path+obj).c_str() )->Clone());
00132   #ifdef DEBUG
00133     cout << " found at " << ptr << " with integral " << ptr->Integral() << " and mean " << ptr->GetMean() << endl;
00134   #endif
00135     //    if(file.empty() || path.empty() || obj.empty()){
00136     if(!ptr){
00137       cerr << "Not all necessary info are set to access the input file. Check your config" << endl;
00138       cerr << "filename: " << file
00139            << "path: " << path
00140            << "obj: " << obj << endl;
00141     }
00142     else 
00143        ptr->SetDirectory(0); //         for the current histogram h
00144 
00145     return ptr;
00146 
00147   }
00148 
00149   void AddSubStrings( vector<string> & vs, string s){
00150     const string delims("\\ ");
00151     string::size_type begIdx, endIdx;
00152     begIdx=s.find_first_not_of(delims);
00153     while(begIdx!=string::npos){
00154       endIdx=s.find_first_of(delims, begIdx);
00155       if(endIdx==string::npos) endIdx=s.length();
00156       vs.push_back(s.substr(begIdx,endIdx-begIdx));
00157       begIdx=s.find_first_not_of(delims, endIdx);
00158     }
00159   }
00160 
00161   /*
00162   bool AddSummaries( vector<EstimateSummary> & channel, vector<vector<EstimateSummary> > &master){
00163     string channel_str=channel[0].channel;
00164     for( unsigned int proc=1;  proc < channel.size(); proc++){
00165       if(channel[proc].channel != channel_str){
00166         std::cout << "Illegal channel description, should be " << channel_str << " but found " << channel.at(proc).channel << std::endl;
00167         std::cout << "name " << channel.at(proc).name << std::endl;
00168         exit(1);
00169       }
00170       master.push_back(channel); 
00171     } 
00172     return true;
00173   }*/
00174 }
00175 }

Generated on Tue Jul 5 14:52:32 2011 for ROOT_528-00b_version by  doxygen 1.5.1