CMS 3D CMS Logo

Plotter.h
Go to the documentation of this file.
1 #ifndef ConfigurableAnalysis_Plotter_H
2 #define ConfigurableAnalysis_Plotter_H
3 
5 
6 #include "TH1.h"
7 #include "TH1F.h"
8 #include "TH2F.h"
9 #include "TProfile.h"
10 
17 
18 class Plotter {
19 public:
20  Plotter() {}
21  Plotter(const edm::ParameterSet& iConfig) {}
22  virtual ~Plotter() {}
23 
24  virtual void setDir(std::string dir) = 0;
25  virtual void fill(std::string subDir, const edm::Event& iEvent) = 0;
26  virtual void complete() = 0;
27 };
28 
29 class VariablePlotter : public Plotter {
30 public:
31  VariablePlotter(const edm::ParameterSet& iConfig) : currentDir_("youDidNotSetDirectoryFirst") {
32  //create the master copy, never filled, just to make copies
33 
34  // make TH1
35  edm::ParameterSet th1 = iConfig.getParameter<edm::ParameterSet>("TH1s");
36  std::vector<std::string> th1Names;
37  th1.getParameterSetNames(th1Names);
38  for (unsigned int iH = 0; iH != th1Names.size(); ++iH) {
39  std::string hname = th1Names[iH];
41  if (hPset.exists("name"))
42  hname = hPset.getParameter<std::string>("name");
43  bool split = hPset.exists("splitter") || hPset.exists("splitters");
44  if (split)
45  master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
46  else
47  master_[hname] = new ConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
48  }
49 
50  // make profiles
51  edm::ParameterSet tprof = iConfig.getParameter<edm::ParameterSet>("TProfiles");
52  std::vector<std::string> tprofNames;
53  tprof.getParameterSetNames(tprofNames);
54  for (unsigned int iH = 0; iH != tprofNames.size(); ++iH) {
55  std::string hname = tprofNames[iH];
56  edm::ParameterSet hPset = tprof.getParameter<edm::ParameterSet>(hname);
57  bool split = hPset.exists("splitter") || hPset.exists("splitters");
58  if (hPset.exists("name"))
59  hname = hPset.getParameter<std::string>("name");
60  if (split)
61  master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
62  else
63  master_[hname] = new ConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
64  }
65 
66  // make TH2
67  edm::ParameterSet th2 = iConfig.getParameter<edm::ParameterSet>("TH2s");
68  std::vector<std::string> th2Names;
69  th2.getParameterSetNames(th2Names);
70  for (unsigned int iH = 0; iH != th2Names.size(); ++iH) {
71  std::string hname = th2Names[iH];
73  if (hPset.exists("name"))
74  hname = hPset.getParameter<std::string>("name");
75  bool split = hPset.exists("splitter") || hPset.exists("splitters");
76  if (split)
77  master_[hname] = new SplittingConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
78  else
79  master_[hname] = new ConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
80  }
81  }
82 
83  void setDir(std::string dir) override {
84  //insert a new one
85  Directory& insertedDirectory = directories_[dir];
86 
87  //create the actual directory in TFile: name is <dir>
88  if (!insertedDirectory.dir) {
89  insertedDirectory.dir = new TFileDirectory(edm::Service<TFileService>()->mkdir(dir));
90  insertedDirectory.dirName = dir;
91  }
92 
93  //remember which directory name this is
94  currentDir_ = dir;
95  }
96 
97  void fill(std::string subDir, const edm::Event& iEvent) override {
98  //what is the current directory
99  Directory& currentDirectory = directories_[currentDir_];
100 
101  //what is the current set of sub directories for this
102  SubDirectories& currentSetOfSubDirectories = currentDirectory.subDir;
103 
104  //find the subDirectory requested:
105  SubDirectory* subDirectoryToUse = nullptr;
106  SubDirectories::iterator subDirectoryFindIterator = currentSetOfSubDirectories.find(subDir);
107 
108  //not found? insert a new directory with this name
109  if (subDirectoryFindIterator == currentSetOfSubDirectories.end()) {
110  edm::LogInfo("VariablePlotter") << " gonna clone histograms for :" << subDir << " in " << currentDir_;
111  SubDirectory& insertedDir = currentSetOfSubDirectories[subDir];
112  subDirectoryToUse = &insertedDir;
113  if (!insertedDir.dir) {
114  insertedDir.dir = new TFileDirectory(currentDirectory.dir->mkdir(subDir));
115  insertedDir.dirName = subDir;
116  }
117 
118  //create a copy from the master copy
119  DirectoryHistos::iterator masterHistogramIterator = master_.begin();
120  DirectoryHistos::iterator masterHistogramIterator_end = master_.end();
121  for (; masterHistogramIterator != masterHistogramIterator_end; ++masterHistogramIterator) {
122  //clone does not book histogram
123  insertedDir.histos[masterHistogramIterator->first] = masterHistogramIterator->second->clone();
124  }
125 
126  //book all copies of the histos
127  DirectoryHistos::iterator clonedHistogramIterator = insertedDir.histos.begin();
128  DirectoryHistos::iterator clonedHistogramIterator_end = insertedDir.histos.end();
129  for (; clonedHistogramIterator != clonedHistogramIterator_end; ++clonedHistogramIterator) {
130  clonedHistogramIterator->second->book(insertedDir.dir);
131  }
132  } else {
133  subDirectoryToUse = &subDirectoryFindIterator->second;
134  }
135 
136  //now that you have the subdirectory: fill histograms for this sub directory
137  DirectoryHistos::iterator histogramIterator = subDirectoryToUse->histos.begin();
138  DirectoryHistos::iterator histogramIterator_end = subDirectoryToUse->histos.end();
139  for (; histogramIterator != histogramIterator_end; ++histogramIterator) {
140  histogramIterator->second->fill(iEvent);
141  }
142  }
143 
144  ~VariablePlotter() override {
145  // CANNOT DO THAT because of TFileService holding the histograms
146  /* //loop over all subdirectories and delete all ConfigurableHistograms
147  Directories::iterator dir_It = directories_.begin();
148  Directories::iterator dir_It_end = directories_.end();
149  // loop directories
150  for (;dir_It!=dir_It_end;++dir_It){
151  Directory & currentDirectory=dir_It->second;
152  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
153  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
154  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
155  //loop subdirectories
156  for (;subDir_It!=subDir_It_end;++subDir_It){
157  DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
158  DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
159  //loop configurable histograms
160  for(; histogramIterator!=histogramIterator_end;++histogramIterator){
161  // by doing that you are removing the histogram from the TFileService too. and this will crash
162  // delete histogramIterator->second;
163  }
164  }
165  }
166  */
167  }
168  void complete() override {
169  //loop over all subdirectories and call complete() on all ConfigurableHistograms
170 
171  Directories::iterator dir_It = directories_.begin();
172  Directories::iterator dir_It_end = directories_.end();
173  // loop directories
174  for (; dir_It != dir_It_end; ++dir_It) {
175  Directory& currentDirectory = dir_It->second;
176  SubDirectories& currentSetOfSubDirectories = currentDirectory.subDir;
177  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
178  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
179  //loop subdirectories
180  for (; subDir_It != subDir_It_end; ++subDir_It) {
181  DirectoryHistos::iterator histogramIterator = subDir_It->second.histos.begin();
182  DirectoryHistos::iterator histogramIterator_end = subDir_It->second.histos.end();
183  //loop configurable histograms
184  for (; histogramIterator != histogramIterator_end; ++histogramIterator) {
185  histogramIterator->second->complete();
186  }
187  }
188  }
189  }
190 
191 private:
192  typedef std::map<std::string, ConfigurableHisto*> DirectoryHistos;
193  DirectoryHistos master_;
194 
195  class SubDirectory {
196  public:
199  DirectoryHistos histos;
201  };
202  typedef std::map<std::string, SubDirectory> SubDirectories;
203 
204  class Directory {
205  public:
208  SubDirectories subDir;
210  };
211  typedef std::map<std::string, Directory> Directories;
212 
214  Directories directories_;
215 };
216 
219 
221 
222 #endif
std::vector< std::string_view > split(std::string_view, const char *)
T getParameter(std::string const &) const
TFileDirectory * dir
Definition: Plotter.h:209
Directories directories_
Definition: Plotter.h:214
virtual ~Plotter()
Definition: Plotter.h:22
#define nullptr
bool exists(std::string const &parameterName) const
checks if a parameter exists
void complete() override
Definition: Plotter.h:168
std::map< std::string, Directory > Directories
Definition: Plotter.h:211
std::map< std::string, ConfigurableHisto * > DirectoryHistos
Definition: Plotter.h:192
Plotter()
Definition: Plotter.h:20
int iEvent
Definition: GenABIO.cc:224
VariablePlotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:31
edmplugin::PluginFactory< Plotter *(const edm::ParameterSet &)> PlotterFactory
Definition: Plotter.h:220
TFileDirectory * dir
Definition: Plotter.h:200
void setDir(std::string dir) override
Definition: Plotter.h:83
~VariablePlotter() override
Definition: Plotter.h:144
DirectoryHistos master_
Definition: Plotter.h:193
void fill(std::string subDir, const edm::Event &iEvent) override
Definition: Plotter.h:97
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
def mkdir(path)
Definition: eostools.py:251
Plotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:21
SubDirectories subDir
Definition: Plotter.h:208
std::string currentDir_
Definition: Plotter.h:213
std::map< std::string, SubDirectory > SubDirectories
Definition: Plotter.h:202
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
virtual void fill(std::string subDir, const edm::Event &iEvent)=0
virtual void complete()=0
virtual void setDir(std::string dir)=0
DirectoryHistos histos
Definition: Plotter.h:199