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  if (hPset.exists("name")) hname = hPset.getParameter<std::string>("name");
44  bool split=hPset.exists("splitter") || hPset.exists("splitters");
45  if (split)
47  else
48  master_[hname]=new ConfigurableHisto(ConfigurableHisto::h1, hname, hPset);
49  }
50 
51  // make profiles
52  edm::ParameterSet tprof=iConfig.getParameter<edm::ParameterSet>("TProfiles");
53  std::vector<std::string> tprofNames;
54  tprof.getParameterSetNames(tprofNames);
55  for (unsigned int iH=0;iH!=tprofNames.size();++iH){
56  std::string hname = tprofNames[iH];
58  bool split=hPset.exists("splitter") || hPset.exists("splitters");
59  if (hPset.exists("name")) 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
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")) hname = hPset.getParameter<std::string>("name");
74  bool split=hPset.exists("splitter") || hPset.exists("splitters");
75  if (split)
77  else
78  master_[hname]=new ConfigurableHisto(ConfigurableHisto::h2, hname, hPset);
79  }
80  }
81 
83  //insert a new one
84  Directory & insertedDirectory = directories_[dir];
85 
86  //create the actual directory in TFile: name is <dir>
87  if (!insertedDirectory.dir){
88  insertedDirectory.dir=new TFileDirectory(edm::Service<TFileService>()->mkdir(dir));
89  insertedDirectory.dirName=dir;
90  }
91 
92  //remember which directory name this is
94  }
95 
96  void fill(std::string subDir,const edm::Event& iEvent){
97  //what is the current directory
98  Directory & currentDirectory= directories_[currentDir_];
99 
100  //what is the current set of sub directories for this
101  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
102 
103  //find the subDirectory requested:
104  SubDirectory * subDirectoryToUse=0;
105  SubDirectories::iterator subDirectoryFindIterator=currentSetOfSubDirectories.find(subDir);
106 
107  //not found? insert a new directory with this name
108  if (subDirectoryFindIterator==currentSetOfSubDirectories.end()){
109  edm::LogInfo("VariablePlotter")<<" gonna clone histograms for :"<<subDir<<" in "<<currentDir_;
110  SubDirectory & insertedDir = currentSetOfSubDirectories[subDir];
111  subDirectoryToUse = &insertedDir;
112  if (!insertedDir.dir){
113  insertedDir.dir=new TFileDirectory(currentDirectory.dir->mkdir(subDir));
114  insertedDir.dirName=subDir;
115  }
116 
117  //create a copy from the master copy
118  DirectoryHistos::iterator masterHistogramIterator=master_.begin();
119  DirectoryHistos::iterator masterHistogramIterator_end=master_.end();
120  for (; masterHistogramIterator!=masterHistogramIterator_end;++masterHistogramIterator)
121  {
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  {
131  clonedHistogramIterator->second->book(insertedDir.dir);
132  }
133  }
134  else{
135  subDirectoryToUse=&subDirectoryFindIterator->second;
136  }
137 
138  //now that you have the subdirectory: fill histograms for this sub directory
139  DirectoryHistos::iterator histogramIterator=subDirectoryToUse->histos.begin();
140  DirectoryHistos::iterator histogramIterator_end=subDirectoryToUse->histos.end();
141  for(; histogramIterator!=histogramIterator_end;++histogramIterator)
142  { histogramIterator->second->fill(iEvent); }
143  }
144 
146  // CANNOT DO THAT because of TFileService holding the histograms
147  /* //loop over all subdirectories and delete all ConfigurableHistograms
148  Directories::iterator dir_It = directories_.begin();
149  Directories::iterator dir_It_end = directories_.end();
150  // loop directories
151  for (;dir_It!=dir_It_end;++dir_It){
152  Directory & currentDirectory=dir_It->second;
153  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
154  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
155  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
156  //loop subdirectories
157  for (;subDir_It!=subDir_It_end;++subDir_It){
158  DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
159  DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
160  //loop configurable histograms
161  for(; histogramIterator!=histogramIterator_end;++histogramIterator){
162  // by doing that you are removing the histogram from the TFileService too. and this will crash
163  // delete histogramIterator->second;
164  }
165  }
166  }
167  */
168  }
169  void complete(){
170 
171  //loop over all subdirectories and call complete() on all ConfigurableHistograms
172 
173  Directories::iterator dir_It = directories_.begin();
174  Directories::iterator dir_It_end = directories_.end();
175  // loop directories
176  for (;dir_It!=dir_It_end;++dir_It){
177  Directory & currentDirectory=dir_It->second;
178  SubDirectories & currentSetOfSubDirectories=currentDirectory.subDir;
179  SubDirectories::iterator subDir_It = currentSetOfSubDirectories.begin();
180  SubDirectories::iterator subDir_It_end = currentSetOfSubDirectories.end();
181  //loop subdirectories
182  for (;subDir_It!=subDir_It_end;++subDir_It){
183  DirectoryHistos::iterator histogramIterator=subDir_It->second.histos.begin();
184  DirectoryHistos::iterator histogramIterator_end=subDir_It->second.histos.end();
185  //loop configurable histograms
186  for(; histogramIterator!=histogramIterator_end;++histogramIterator)
187  { histogramIterator->second->complete(); }
188  }
189  }
190  }
191 
192  private:
193  typedef std::map<std::string, ConfigurableHisto *> DirectoryHistos;
195 
196  class SubDirectory {
197  public:
198  SubDirectory() : dirName(""),dir(0){}
202  };
203  typedef std::map<std::string, SubDirectory> SubDirectories;
204 
205  class Directory {
206  public:
207  Directory() : dirName(""),dir(0){}
211  };
212  typedef std::map<std::string, Directory> Directories;
213 
216 };
217 
218 
221 
223 
224 #endif
T getParameter(std::string const &) const
TFileDirectory * dir
Definition: Plotter.h:210
Directories directories_
Definition: Plotter.h:215
void fill(std::string subDir, const edm::Event &iEvent)
Definition: Plotter.h:96
virtual ~Plotter()
Definition: Plotter.h:23
bool exists(std::string const &parameterName) const
checks if a parameter exists
void complete()
Definition: Plotter.h:169
std::map< std::string, Directory > Directories
Definition: Plotter.h:212
std::map< std::string, ConfigurableHisto * > DirectoryHistos
Definition: Plotter.h:193
Plotter()
Definition: Plotter.h:21
int iEvent
Definition: GenABIO.cc:230
edmplugin::PluginFactory< Plotter *(const edm::ParameterSet &) > PlotterFactory
Definition: Plotter.h:222
VariablePlotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:33
TFileDirectory * dir
Definition: Plotter.h:201
DirectoryHistos master_
Definition: Plotter.h:194
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
void setDir(std::string dir)
Definition: Plotter.h:82
Plotter(const edm::ParameterSet &iConfig)
Definition: Plotter.h:22
SubDirectories subDir
Definition: Plotter.h:209
def mkdir
Definition: eostools.py:248
std::string currentDir_
Definition: Plotter.h:214
std::map< std::string, SubDirectory > SubDirectories
Definition: Plotter.h:203
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:200
double split
Definition: MVATrainer.cc:139