Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:25

0001 #ifndef ConfigurableAnalysis_Plotter_H
0002 #define ConfigurableAnalysis_Plotter_H
0003 
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 
0006 #include "TH1.h"
0007 #include "TH1F.h"
0008 #include "TH2F.h"
0009 #include "TProfile.h"
0010 
0011 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
0012 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
0013 #include "PhysicsTools/UtilAlgos/interface/ConfigurableHisto.h"
0014 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0015 #include "CommonTools/Utils/interface/TFileDirectory.h"
0016 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0017 
0018 class Plotter {
0019 public:
0020   Plotter() {}
0021   Plotter(const edm::ParameterSet& iConfig) {}
0022   virtual ~Plotter() {}
0023 
0024   virtual void setDir(std::string dir) = 0;
0025   virtual void fill(std::string subDir, const edm::Event& iEvent) = 0;
0026   virtual void complete() = 0;
0027 };
0028 
0029 class VariablePlotter : public Plotter {
0030 public:
0031   VariablePlotter(const edm::ParameterSet& iConfig) : currentDir_("youDidNotSetDirectoryFirst") {
0032     //create the master copy, never filled, just to make copies
0033 
0034     //    make TH1
0035     edm::ParameterSet th1 = iConfig.getParameter<edm::ParameterSet>("TH1s");
0036     std::vector<std::string> th1Names;
0037     th1.getParameterSetNames(th1Names);
0038     for (unsigned int iH = 0; iH != th1Names.size(); ++iH) {
0039       std::string hname = th1Names[iH];
0040       edm::ParameterSet hPset = th1.getParameter<edm::ParameterSet>(hname);
0041       if (hPset.exists("name"))
0042         hname = hPset.getParameter<std::string>("name");
0043       bool split = hPset.exists("splitter") || hPset.exists("splitters");
0044       if (split)
0045         master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
0046       else
0047         master_[hname] = new ConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
0048     }
0049 
0050     //    make profiles
0051     edm::ParameterSet tprof = iConfig.getParameter<edm::ParameterSet>("TProfiles");
0052     std::vector<std::string> tprofNames;
0053     tprof.getParameterSetNames(tprofNames);
0054     for (unsigned int iH = 0; iH != tprofNames.size(); ++iH) {
0055       std::string hname = tprofNames[iH];
0056       edm::ParameterSet hPset = tprof.getParameter<edm::ParameterSet>(hname);
0057       bool split = hPset.exists("splitter") || hPset.exists("splitters");
0058       if (hPset.exists("name"))
0059         hname = hPset.getParameter<std::string>("name");
0060       if (split)
0061         master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
0062       else
0063         master_[hname] = new ConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
0064     }
0065 
0066     //    make TH2
0067     edm::ParameterSet th2 = iConfig.getParameter<edm::ParameterSet>("TH2s");
0068     std::vector<std::string> th2Names;
0069     th2.getParameterSetNames(th2Names);
0070     for (unsigned int iH = 0; iH != th2Names.size(); ++iH) {
0071       std::string hname = th2Names[iH];
0072       edm::ParameterSet hPset = th2.getParameter<edm::ParameterSet>(hname);
0073       if (hPset.exists("name"))
0074         hname = hPset.getParameter<std::string>("name");
0075       bool split = hPset.exists("splitter") || hPset.exists("splitters");
0076       if (split)
0077         master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
0078       else
0079         master_[hname] = new ConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
0080     }
0081   }
0082 
0083   void setDir(std::string dir) override {
0084     //insert a new one
0085     Directory& insertedDirectory = directories_[dir];
0086 
0087     //create the actual directory in TFile: name is <dir>
0088     if (!insertedDirectory.dir) {
0089       insertedDirectory.dir = new TFileDirectory(edm::Service<TFileService>()->mkdir(dir));
0090       insertedDirectory.dirName = dir;
0091     }
0092 
0093     //remember which directory name this is
0094     currentDir_ = dir;
0095   }
0096 
0097   void fill(std::string subDir, const edm::Event& iEvent) override {
0098     //what is the current directory
0099     Directory& currentDirectory = directories_[currentDir_];
0100 
0101     //what is the current set of sub directories for this
0102     SubDirectories& currentSetOfSubDirectories = currentDirectory.subDir;
0103 
0104     //find the subDirectory requested:
0105     SubDirectory* subDirectoryToUse = nullptr;
0106     SubDirectories::iterator subDirectoryFindIterator = currentSetOfSubDirectories.find(subDir);
0107 
0108     //not found? insert a new directory with this name
0109     if (subDirectoryFindIterator == currentSetOfSubDirectories.end()) {
0110       edm::LogInfo("VariablePlotter") << " gonna clone histograms for :" << subDir << " in " << currentDir_;
0111       SubDirectory& insertedDir = currentSetOfSubDirectories[subDir];
0112       subDirectoryToUse = &insertedDir;
0113       if (!insertedDir.dir) {
0114         insertedDir.dir = new TFileDirectory(currentDirectory.dir->mkdir(subDir));
0115         insertedDir.dirName = subDir;
0116       }
0117 
0118       //create a copy from the master copy
0119       DirectoryHistos::iterator masterHistogramIterator = master_.begin();
0120       DirectoryHistos::iterator masterHistogramIterator_end = master_.end();
0121       for (; masterHistogramIterator != masterHistogramIterator_end; ++masterHistogramIterator) {
0122         //clone does not book histogram
0123         insertedDir.histos[masterHistogramIterator->first] = masterHistogramIterator->second->clone();
0124       }
0125 
0126       //book all copies of the histos
0127       DirectoryHistos::iterator clonedHistogramIterator = insertedDir.histos.begin();
0128       DirectoryHistos::iterator clonedHistogramIterator_end = insertedDir.histos.end();
0129       for (; clonedHistogramIterator != clonedHistogramIterator_end; ++clonedHistogramIterator) {
0130         clonedHistogramIterator->second->book(insertedDir.dir);
0131       }
0132     } else {
0133       subDirectoryToUse = &subDirectoryFindIterator->second;
0134     }
0135 
0136     //now that you have the subdirectory: fill histograms for this sub directory
0137     DirectoryHistos::iterator histogramIterator = subDirectoryToUse->histos.begin();
0138     DirectoryHistos::iterator histogramIterator_end = subDirectoryToUse->histos.end();
0139     for (; histogramIterator != histogramIterator_end; ++histogramIterator) {
0140       histogramIterator->second->fill(iEvent);
0141     }
0142   }
0143 
0144   ~VariablePlotter() override {
0145     // CANNOT DO THAT because of TFileService holding the histograms
0146     /*    //loop over all subdirectories and delete all ConfigurableHistograms
0147       Directories::iterator dir_It = directories_.begin();
0148       Directories::iterator dir_It_end = directories_.end();
0149       // loop directories
0150       for (;dir_It!=dir_It_end;++dir_It){
0151       Directory & currentDirectory=dir_It->second;
0152       SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
0153       SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
0154       SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
0155       //loop subdirectories
0156       for (;subDir_It!=subDir_It_end;++subDir_It){
0157       DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
0158       DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
0159       //loop configurable histograms
0160       for(; histogramIterator!=histogramIterator_end;++histogramIterator){
0161       // by doing that you are removing the histogram from the TFileService too. and this will crash
0162       //      delete histogramIterator->second;
0163       }
0164       }
0165       }
0166     */
0167   }
0168   void complete() override {
0169     //loop over all subdirectories and call complete() on all ConfigurableHistograms
0170 
0171     Directories::iterator dir_It = directories_.begin();
0172     Directories::iterator dir_It_end = directories_.end();
0173     // loop directories
0174     for (; dir_It != dir_It_end; ++dir_It) {
0175       Directory& currentDirectory = dir_It->second;
0176       SubDirectories& currentSetOfSubDirectories = currentDirectory.subDir;
0177       SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
0178       SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
0179       //loop subdirectories
0180       for (; subDir_It != subDir_It_end; ++subDir_It) {
0181         DirectoryHistos::iterator histogramIterator = subDir_It->second.histos.begin();
0182         DirectoryHistos::iterator histogramIterator_end = subDir_It->second.histos.end();
0183         //loop configurable histograms
0184         for (; histogramIterator != histogramIterator_end; ++histogramIterator) {
0185           histogramIterator->second->complete();
0186         }
0187       }
0188     }
0189   }
0190 
0191 private:
0192   typedef std::map<std::string, ConfigurableHisto*> DirectoryHistos;
0193   DirectoryHistos master_;
0194 
0195   class SubDirectory {
0196   public:
0197     SubDirectory() : dirName(""), dir(nullptr) {}
0198     std::string dirName;
0199     DirectoryHistos histos;
0200     TFileDirectory* dir;
0201   };
0202   typedef std::map<std::string, SubDirectory> SubDirectories;
0203 
0204   class Directory {
0205   public:
0206     Directory() : dirName(""), dir(nullptr) {}
0207     std::string dirName;
0208     SubDirectories subDir;
0209     TFileDirectory* dir;
0210   };
0211   typedef std::map<std::string, Directory> Directories;
0212 
0213   std::string currentDir_;
0214   Directories directories_;
0215 };
0216 
0217 #include "FWCore/PluginManager/interface/PluginFactory.h"
0218 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0219 
0220 typedef edmplugin::PluginFactory<Plotter*(const edm::ParameterSet&)> PlotterFactory;
0221 
0222 #endif