CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
19 class Plotter {
20  public:
21  Plotter(){}
22  Plotter(const edm::ParameterSet& iConfig){}
23  virtual ~Plotter(){}
24 
25  virtual void setDir(std::string dir) =0;
26  virtual void fill(std::string subDir,const edm::Event& iEvent) =0;
27  virtual void complete() =0;
28 };
29 
30 
31 class VariablePlotter : public Plotter {
32  public:
33  VariablePlotter(const edm::ParameterSet& iConfig) : currentDir_("youDidNotSetDirectoryFirst") {
34  //create the master copy, never filled, just to make copies
35 
36  // make TH1
38  std::vector<std::string> th1Names;
39  th1.getParameterSetNames(th1Names);
40  for (unsigned int iH=0;iH!=th1Names.size();++iH){
41  std::string hname = th1Names[iH];
43  bool split=hPset.exists("splitter") || hPset.exists("splitters");
44  if (split)
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];
57  bool split=hPset.exists("splitter") || hPset.exists("splitters");
58  if (split)
60  else
61  master_[hname]=new ConfigurableHisto(ConfigurableHisto::prof, hname, hPset);
62  }
63 
64  // make TH2
66  std::vector<std::string> th2Names;
67  th2.getParameterSetNames(th2Names);
68  for (unsigned int iH=0;iH!=th2Names.size();++iH){
69  std::string hname = th2Names[iH];
71  bool split=hPset.exists("splitter") || hPset.exists("splitters");
72  if (split)
74  else
75  master_[hname]=new ConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
76  }
77  }
78 
80  //insert a new one
81  Directory & insertedDirectory = directories_[dir];
82 
83  //create the actual directory in TFile: name is <dir>
84  if (!insertedDirectory.dir){
85  insertedDirectory.dir=new TFileDirectory(edm::Service<TFileService>()->mkdir(dir));
86  insertedDirectory.dirName=dir;
87  }
88 
89  //remember which directory name this is
91  }
92 
93  void fill(std::string subDir,const edm::Event& iEvent){
94  //what is the current directory
95  Directory & currentDirectory= directories_[currentDir_];
96 
97  //what is the current set of sub directories for this
98  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
99 
100  //find the subDirectory requested:
101  SubDirectory * subDirectoryToUse=0;
102  SubDirectories::iterator subDirectoryFindIterator=currentSetOfSubDirectories.find(subDir);
103 
104  //not found? insert a new directory with this name
105  if (subDirectoryFindIterator==currentSetOfSubDirectories.end()){
106  edm::LogInfo("VariablePlotter")<<" gonna clone histograms for :"<<subDir<<" in "<<currentDir_;
107  SubDirectory & insertedDir = currentSetOfSubDirectories[subDir];
108  subDirectoryToUse = &insertedDir;
109  if (!insertedDir.dir){
110  insertedDir.dir=new TFileDirectory(currentDirectory.dir->mkdir(subDir));
111  insertedDir.dirName=subDir;
112  }
113 
114  //create a copy from the master copy
115  DirectoryHistos::iterator masterHistogramIterator=master_.begin();
116  DirectoryHistos::iterator masterHistogramIterator_end=master_.end();
117  for (; masterHistogramIterator!=masterHistogramIterator_end;++masterHistogramIterator)
118  {
119  //clone does not book histogram
120  insertedDir.histos[masterHistogramIterator->first]=masterHistogramIterator->second->clone();
121  }
122 
123  //book all copies of the histos
124  DirectoryHistos::iterator clonedHistogramIterator=insertedDir.histos.begin();
125  DirectoryHistos::iterator clonedHistogramIterator_end=insertedDir.histos.end();
126  for (; clonedHistogramIterator!=clonedHistogramIterator_end;++clonedHistogramIterator)
127  {
128  clonedHistogramIterator->second->book(insertedDir.dir);
129  }
130  }
131  else{
132  subDirectoryToUse=&subDirectoryFindIterator->second;
133  }
134 
135  //now that you have the subdirectory: fill histograms for this sub directory
136  DirectoryHistos::iterator histogramIterator=subDirectoryToUse->histos.begin();
137  DirectoryHistos::iterator histogramIterator_end=subDirectoryToUse->histos.end();
138  for(; histogramIterator!=histogramIterator_end;++histogramIterator)
139  { histogramIterator->second->fill(iEvent); }
140  }
141 
143  // CANNOT DO THAT because of TFileService holding the histograms
144  /* //loop over all subdirectories and delete all ConfigurableHistograms
145  Directories::iterator dir_It = directories_.begin();
146  Directories::iterator dir_It_end = directories_.end();
147  // loop directories
148  for (;dir_It!=dir_It_end;++dir_It){
149  Directory & currentDirectory=dir_It->second;
150  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
151  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
152  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
153  //loop subdirectories
154  for (;subDir_It!=subDir_It_end;++subDir_It){
155  DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
156  DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
157  //loop configurable histograms
158  for(; histogramIterator!=histogramIterator_end;++histogramIterator){
159  // by doing that you are removing the histogram from the TFileService too. and this will crash
160  // delete histogramIterator->second;
161  }
162  }
163  }
164  */
165  }
166  void complete(){
167 
168  //loop over all subdirectories and call complete() on all ConfigurableHistograms
169 
170  Directories::iterator dir_It = directories_.begin();
171  Directories::iterator dir_It_end = directories_.end();
172  // loop directories
173  for (;dir_It!=dir_It_end;++dir_It){
174  Directory & currentDirectory=dir_It->second;
175  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
176  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
177  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
178  //loop subdirectories
179  for (;subDir_It!=subDir_It_end;++subDir_It){
180  DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
181  DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
182  //loop configurable histograms
183  for(; histogramIterator!=histogramIterator_end;++histogramIterator)
184  { histogramIterator->second->complete(); }
185  }
186  }
187  }
188 
189  private:
190  typedef std::map<std::string, ConfigurableHisto *> DirectoryHistos;
192 
193  class SubDirectory {
194  public:
195  SubDirectory() : dirName(""),dir(0){}
199  };
200  typedef std::map<std::string, SubDirectory> SubDirectories;
201 
202  class Directory {
203  public:
204  Directory() : dirName(""),dir(0){}
208  };
209  typedef std::map<std::string, Directory> Directories;
210 
213 };
214 
215 
218 
220 
221 #endif
T getParameter(std::string const &) const
TFileDirectory * dir
Definition: Plotter.h:207
Directories directories_
Definition: Plotter.h:212
void fill(std::string subDir, const edm::Event &iEvent)
Definition: Plotter.h:93
virtual ~Plotter()
Definition: Plotter.h:23
bool exists(std::string const &parameterName) const
checks if a parameter exists
void complete()
Definition: Plotter.h:166
std::map< std::string, Directory > Directories
Definition: Plotter.h:209
std::map< std::string, ConfigurableHisto * > DirectoryHistos
Definition: Plotter.h:190
Plotter()
Definition: Plotter.h:21
int iEvent
Definition: GenABIO.cc:243
edmplugin::PluginFactory< Plotter *(const edm::ParameterSet &) > PlotterFactory
Definition: Plotter.h:219
VariablePlotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:33
TFileDirectory * dir
Definition: Plotter.h:198
DirectoryHistos master_
Definition: Plotter.h:191
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
void setDir(std::string dir)
Definition: Plotter.h:79
Plotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:22
SubDirectories subDir
Definition: Plotter.h:206
std::string currentDir_
Definition: Plotter.h:211
std::map< std::string, SubDirectory > SubDirectories
Definition: Plotter.h:200
dbl *** dir
Definition: mlp_gen.cc:35
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:197
double split
Definition: MVATrainer.cc:139