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)
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;
194 
195  class SubDirectory {
196  public:
197  SubDirectory() : dirName(""), dir(nullptr) {}
201  };
202  typedef std::map<std::string, SubDirectory> SubDirectories;
203 
204  class Directory {
205  public:
206  Directory() : dirName(""), dir(nullptr) {}
210  };
211  typedef std::map<std::string, Directory> Directories;
212 
215 };
216 
219 
221 
222 #endif
TFileDirectory::mkdir
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileDirectory.cc:112
PluginFactory.h
ConfigurableHisto::h2
Definition: ConfigurableHisto.h:72
VariablePlotter::Directory::dir
TFileDirectory * dir
Definition: Plotter.h:209
VariablePlotter::currentDir_
std::string currentDir_
Definition: Plotter.h:213
VariablePlotter::SubDirectory
Definition: Plotter.h:195
CachingVariable.h
VariablePlotter::directories_
Directories directories_
Definition: Plotter.h:214
Plotter
Definition: Plotter.h:18
VariablePlotter::SubDirectory::histos
DirectoryHistos histos
Definition: Plotter.h:199
edm::LogInfo
Definition: MessageLogger.h:254
VariablePlotter::Directory
Definition: Plotter.h:204
ConfigurableHisto.h
TFileDirectory
Definition: TFileDirectory.h:24
VariablePlotter::~VariablePlotter
~VariablePlotter() override
Definition: Plotter.h:144
cms::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
VariablePlotter
Definition: Plotter.h:29
Plotter::Plotter
Plotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:21
ConfigurableHisto::prof
Definition: ConfigurableHisto.h:72
ConfigurableHisto::h1
Definition: ConfigurableHisto.h:72
TFileDirectory.h
VariablePlotter::complete
void complete() override
Definition: Plotter.h:168
PostProcessorHcalIsoTrack_cfi.subDir
subDir
Definition: PostProcessorHcalIsoTrack_cfi.py:5
VariablePlotter::master_
DirectoryHistos master_
Definition: Plotter.h:193
Plotter::fill
virtual void fill(std::string subDir, const edm::Event &iEvent)=0
VariablePlotter::DirectoryHistos
std::map< std::string, ConfigurableHisto * > DirectoryHistos
Definition: Plotter.h:192
VariablePlotter::Directory::subDir
SubDirectories subDir
Definition: Plotter.h:208
Plotter::complete
virtual void complete()=0
Plotter::setDir
virtual void setDir(std::string dir)=0
eostools.mkdir
def mkdir(path)
Definition: eostools.py:251
Plotter::~Plotter
virtual ~Plotter()
Definition: Plotter.h:22
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TFileService.h
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:674
ConfigurableHisto
Definition: ConfigurableHisto.h:70
edm::ParameterSet
Definition: ParameterSet.h:36
VariablePlotter::SubDirectory::dir
TFileDirectory * dir
Definition: Plotter.h:200
edmplugin::PluginFactory
Definition: PluginFactory.h:34
VariablePlotter::SubDirectory::dirName
std::string dirName
Definition: Plotter.h:198
edm::Service< TFileService >
iEvent
int iEvent
Definition: GenABIO.cc:224
VariablePlotter::setDir
void setDir(std::string dir) override
Definition: Plotter.h:83
VariablePlotter::Directory::dirName
std::string dirName
Definition: Plotter.h:207
VariablePlotter::Directory::Directory
Directory()
Definition: Plotter.h:206
VariablePlotter::fill
void fill(std::string subDir, const edm::Event &iEvent) override
Definition: Plotter.h:97
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
VariablePlotter::SubDirectories
std::map< std::string, SubDirectory > SubDirectories
Definition: Plotter.h:202
SplittingConfigurableHisto
Definition: ConfigurableHisto.h:241
VariablePlotter::SubDirectory::SubDirectory
SubDirectory()
Definition: Plotter.h:197
VariableHelper.h
Plotter::Plotter
Plotter()
Definition: Plotter.h:20
ParameterSet.h
edm::Event
Definition: Event.h:73
VariablePlotter::Directories
std::map< std::string, Directory > Directories
Definition: Plotter.h:211
VariablePlotter::VariablePlotter
VariablePlotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:31
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
edm::ParameterSet::getParameterSetNames
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
Definition: ParameterSet.cc:717