CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/PhysicsTools/UtilAlgos/interface/Plotter.h

Go to the documentation of this file.
00001 #ifndef ConfigurableAnalysis_Plotter_H
00002 #define ConfigurableAnalysis_Plotter_H
00003 
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 
00006 #include "TH1.h"
00007 #include "TH1F.h"
00008 #include "TH2F.h"
00009 #include "TProfile.h"
00010 
00011 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
00012 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
00013 #include "PhysicsTools/UtilAlgos/interface/ConfigurableHisto.h"
00014 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00015 #include "CommonTools/Utils/interface/TFileDirectory.h"
00016 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00017 
00018 
00019 class Plotter {
00020  public:
00021   Plotter(){}
00022   Plotter(edm::ParameterSet iConfig){}
00023   virtual ~Plotter(){}
00024 
00025   virtual void setDir(std::string dir) =0;
00026   virtual void fill(std::string subDir,const edm::Event& iEvent) =0;
00027   virtual void complete() =0;
00028 };
00029 
00030 
00031 class VariablePlotter : public Plotter {
00032  public:
00033   VariablePlotter(edm::ParameterSet iConfig) : currentDir_("youDidNotSetDirectoryFirst") {
00034     //create the master copy, never filled, just to make copies
00035 
00036     //    make TH1
00037     edm::ParameterSet th1=iConfig.getParameter<edm::ParameterSet>("TH1s");
00038     std::vector<std::string> th1Names;
00039     th1.getParameterSetNames(th1Names);
00040     for (unsigned int iH=0;iH!=th1Names.size();++iH){
00041       std::string hname = th1Names[iH];
00042       edm::ParameterSet hPset=th1.getParameter<edm::ParameterSet>(hname);
00043       bool split=hPset.exists("splitter") || hPset.exists("splitters");
00044       if (split)
00045         master_[hname]=new SplittingConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
00046       else
00047         master_[hname]=new ConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
00048     }
00049 
00050     //    make profiles
00051     edm::ParameterSet tprof=iConfig.getParameter<edm::ParameterSet>("TProfiles");
00052     std::vector<std::string> tprofNames;
00053     tprof.getParameterSetNames(tprofNames);
00054     for (unsigned int iH=0;iH!=tprofNames.size();++iH){
00055       std::string hname = tprofNames[iH];
00056       edm::ParameterSet hPset=tprof.getParameter<edm::ParameterSet>(hname);
00057       bool split=hPset.exists("splitter") || hPset.exists("splitters");
00058       if (split)
00059         master_[hname]=new SplittingConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
00060       else
00061         master_[hname]=new ConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
00062     }
00063     
00064     //    make TH2
00065     edm::ParameterSet th2=iConfig.getParameter<edm::ParameterSet>("TH2s");
00066     std::vector<std::string> th2Names;
00067     th2.getParameterSetNames(th2Names);
00068     for (unsigned int iH=0;iH!=th2Names.size();++iH){
00069       std::string hname = th2Names[iH];
00070       edm::ParameterSet hPset=th2.getParameter<edm::ParameterSet>(hname);
00071       bool split=hPset.exists("splitter") || hPset.exists("splitters");
00072       if (split)
00073         master_[hname]=new SplittingConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
00074       else
00075         master_[hname]=new ConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
00076     }
00077   }
00078 
00079   void setDir(std::string dir){
00080     //insert a new one
00081     Directory & insertedDirectory = directories_[dir];
00082 
00083     //create the actual directory in TFile: name is <dir>
00084     if (!insertedDirectory.dir){
00085       insertedDirectory.dir=new TFileDirectory(edm::Service<TFileService>()->mkdir(dir));
00086       insertedDirectory.dirName=dir;
00087     }
00088 
00089     //remember which directory name this is
00090     currentDir_=dir;
00091   }
00092   
00093   void fill(std::string subDir,const edm::Event& iEvent){
00094     //what is the current directory
00095     Directory & currentDirectory= directories_[currentDir_];
00096 
00097     //what is the current set of sub directories for this
00098     SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
00099     
00100     //find the subDirectory requested:
00101     SubDirectory * subDirectoryToUse=0;
00102     SubDirectories::iterator subDirectoryFindIterator=currentSetOfSubDirectories.find(subDir);
00103 
00104     //not found? insert a new directory with this name
00105     if (subDirectoryFindIterator==currentSetOfSubDirectories.end()){
00106       edm::LogInfo("VariablePlotter")<<" gonna clone histograms for :"<<subDir<<" in "<<currentDir_;
00107       SubDirectory & insertedDir = currentSetOfSubDirectories[subDir];
00108       subDirectoryToUse = &insertedDir;
00109       if (!insertedDir.dir){
00110         insertedDir.dir=new TFileDirectory(currentDirectory.dir->mkdir(subDir));
00111         insertedDir.dirName=subDir;
00112       }
00113 
00114       //create a copy from the master copy
00115       DirectoryHistos::iterator masterHistogramIterator=master_.begin();
00116       DirectoryHistos::iterator masterHistogramIterator_end=master_.end();
00117       for (; masterHistogramIterator!=masterHistogramIterator_end;++masterHistogramIterator)
00118         {
00119           //clone does not book histogram
00120           insertedDir.histos[masterHistogramIterator->first]=masterHistogramIterator->second->clone();
00121         }
00122       
00123       //book all copies of the histos
00124       DirectoryHistos::iterator clonedHistogramIterator=insertedDir.histos.begin();
00125       DirectoryHistos::iterator clonedHistogramIterator_end=insertedDir.histos.end();
00126       for (; clonedHistogramIterator!=clonedHistogramIterator_end;++clonedHistogramIterator)
00127         {
00128           clonedHistogramIterator->second->book(insertedDir.dir);
00129         }
00130     }
00131     else{
00132       subDirectoryToUse=&subDirectoryFindIterator->second;
00133     }
00134     
00135     //now that you have the subdirectory: fill histograms for this sub directory
00136     DirectoryHistos::iterator histogramIterator=subDirectoryToUse->histos.begin();
00137     DirectoryHistos::iterator histogramIterator_end=subDirectoryToUse->histos.end();
00138     for(; histogramIterator!=histogramIterator_end;++histogramIterator)
00139       { histogramIterator->second->fill(iEvent); }
00140   }
00141 
00142   ~VariablePlotter(){
00143     // CANNOT DO THAT because of TFileService holding the histograms
00144     /*    //loop over all subdirectories and delete all ConfigurableHistograms
00145           Directories::iterator dir_It = directories_.begin();
00146           Directories::iterator dir_It_end = directories_.end();
00147           // loop directories
00148           for (;dir_It!=dir_It_end;++dir_It){
00149           Directory & currentDirectory=dir_It->second;
00150           SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
00151           SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
00152           SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
00153           //loop subdirectories
00154           for (;subDir_It!=subDir_It_end;++subDir_It){
00155           DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
00156           DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
00157           //loop configurable histograms
00158           for(; histogramIterator!=histogramIterator_end;++histogramIterator){
00159           // by doing that you are removing the histogram from the TFileService too. and this will crash
00160           //      delete histogramIterator->second;
00161           }
00162           }
00163           }
00164     */
00165   }
00166   void complete(){
00167     
00168     //loop over all subdirectories and call complete() on all ConfigurableHistograms
00169     
00170     Directories::iterator dir_It = directories_.begin();
00171     Directories::iterator dir_It_end = directories_.end();
00172     // loop directories
00173     for (;dir_It!=dir_It_end;++dir_It){
00174       Directory & currentDirectory=dir_It->second;
00175       SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
00176       SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
00177       SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
00178       //loop subdirectories
00179       for (;subDir_It!=subDir_It_end;++subDir_It){
00180         DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
00181         DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
00182         //loop configurable histograms
00183         for(; histogramIterator!=histogramIterator_end;++histogramIterator)
00184           { histogramIterator->second->complete(); }
00185       }
00186     }
00187   }
00188 
00189  private:
00190   typedef std::map<std::string, ConfigurableHisto *> DirectoryHistos;
00191   DirectoryHistos master_;
00192 
00193   class SubDirectory {
00194   public:
00195     SubDirectory() : dirName(""),dir(0){}
00196     std::string dirName;
00197     DirectoryHistos histos;
00198     TFileDirectory * dir;
00199   };
00200   typedef std::map<std::string, SubDirectory> SubDirectories;
00201 
00202   class Directory {
00203   public:
00204     Directory() : dirName(""),dir(0){}
00205     std::string dirName;
00206     SubDirectories subDir;
00207     TFileDirectory * dir;
00208   };
00209   typedef std::map<std::string, Directory> Directories;
00210 
00211   std::string currentDir_;
00212   Directories directories_;
00213 };
00214 
00215 
00216 #include "FWCore/PluginManager/interface/PluginFactory.h"
00217 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00218 
00219 typedef edmplugin::PluginFactory< Plotter* (const edm::ParameterSet&) > PlotterFactory;
00220 
00221 #endif