CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ElectronDqmAnalyzerBase.cc
Go to the documentation of this file.
1 
8 #include "TMath.h"
9 #include "TFile.h"
10 #include "TH1F.h"
11 #include "TH1I.h"
12 #include "TH2F.h"
13 #include "TProfile.h"
14 #include "TTree.h"
15 #include <iostream>
16 #include <algorithm>
17 #include <sstream>
18 
20  : bookPrefix_("ele"), bookIndex_(0), histoNamesReady(false), finalDone_(false)
21  {
22  verbosity_ = conf.getUntrackedParameter<int>("Verbosity") ;
23  finalStep_ = conf.getParameter<std::string>("FinalStep") ;
24  inputFile_ = conf.getParameter<std::string>("InputFile") ;
25  outputFile_ = conf.getParameter<std::string>("OutputFile") ;
26  inputInternalPath_ = conf.getParameter<std::string>("InputFolderName") ;
27  outputInternalPath_ = conf.getParameter<std::string>("OutputFolderName") ;
28  }
29 
31  {}
32 
34  { bookPrefix_ = prefix ; }
35 
37  { bookIndex_ = index ; }
38 
40  {
41  if (bookPrefix_.empty())
42  { return name ; }
43  std::ostringstream oss ;
44  oss<<bookPrefix_ ;
45  if (bookIndex_>=0)
46  { oss<<bookIndex_++ ; }
47  oss<<"_"<<name ;
48  return oss.str() ;
49  }
50 
52  {
53  typedef std::vector<std::string> HistoNames ;
54  typedef HistoNames::iterator HistoNamesItr ;
55  if (!histoNamesReady)
57  HistoNamesItr histoName ;
58  std::vector<HistoNamesItr> res ;
59  for ( histoName = histoNames_.begin() ; histoName != histoNames_.end() ; ++histoName )
60  {
61  std::size_t nsize = name.size(), lsize = histoName->size() ;
62 // if ( (histoName->find(bookPrefix_)==0) &&
63 // (lsize>=nsize) &&
64 // (histoName->find(name)==(lsize-nsize)) )
65 // { res.push_back(histoName) ; }
66  if ( (lsize>=nsize) &&
67  (histoName->find(name)==(lsize-nsize)) )
68  { res.push_back(histoName) ; }
69  }
70  if (res.size()==0)
71  {
72  std::ostringstream oss ;
73  oss<<"Histogram "<<name<<" not found in "<<outputInternalPath_ ;
74  char sep = ':' ;
75  for ( histoName = histoNames_.begin() ; histoName != histoNames_.end() ; ++histoName )
76  { oss<<sep<<' '<<*histoName ; sep = ',' ; }
77  oss<<'.' ;
78  edm::LogWarning("ElectronDqmAnalyzerBase::find")<<oss.str() ;
79  return 0 ;
80  }
81  else if (res.size()>1)
82  {
83  std::ostringstream oss ;
84  oss<<"Ambiguous histograms for "<<name<<" in "<<outputInternalPath_ ;
85  char sep = ':' ;
86  std::vector<HistoNamesItr>::iterator resItr ;
87  for ( resItr = res.begin() ; resItr != res.end() ; ++resItr )
88  { oss<<sep<<' '<<(**resItr) ; sep = ',' ; }
89  oss<<'.' ;
90  edm::LogWarning("ElectronDqmAnalyzerBase::find")<<oss.str() ;
91  return 0 ;
92  }
93  else
94  {
95  return &*res[0] ;
96  }
97  }
98 
100  {
102  if (!store_)
103  { edm::LogError("ElectronDqmAnalyzerBase::prepareStore")<<"No DQMStore found !" ; }
105  if (inputFile_!="")
106  { store_->open(inputFile_) ; }
108  book() ;
109  }
110 
112  {
113  if (finalStep_=="AtRunEnd")
114  {
115  if (finalDone_)
116  { edm::LogWarning("ElectronDqmAnalyzerBase::endRun")<<"finalize() already called" ; }
118  finalize() ;
119  finalDone_ = true ;
120  }
121  }
123  {
124  if (finalStep_=="AtLumiEnd")
125  {
126  if (finalDone_)
127  { edm::LogWarning("ElectronDqmAnalyzerBase::endLuminosityBlock")<<"finalize() already called" ; }
129  finalize() ;
130  finalDone_ = true ;
131  }
132  }
133 
135  {
136  if (finalStep_=="AtJobEnd")
137  {
138  if (finalDone_)
139  { edm::LogWarning("ElectronDqmAnalyzerBase::endJob")<<"finalize() already called" ; }
141  finalize() ;
142  finalDone_ = true ;
143  }
144  if (outputFile_!="")
145  { store_->save(outputFile_) ; }
146  }
147 
149  {
150  const std::string * fullName = find(name) ;
151  if (fullName)
152  { return store_->get(inputInternalPath_+"/"+*fullName) ; }
153  else
154  { return nullptr ; }
155  }
156 
158  {
159  const std::string * fullName = find(name) ;
160  if (fullName)
161  {
163  store_->removeElement(*fullName) ;
164  }
165  }
166 
168  {
169  std::string currentPath = store_->pwd() ;
170  store_->cd() ;
171 
172  std::string currentFolder ;
173  std::vector<std::string> subDirs ;
174  std::vector<std::string>::iterator subDir ;
175  std::string delimiter = "/" ;
176 
177  std::string::size_type lastPos = currentPath.find_first_not_of(delimiter,0) ;
178  std::string::size_type pos = currentPath.find_first_of(delimiter,lastPos) ;
179  while (std::string::npos != pos || std::string::npos != lastPos)
180  {
181  if (currentFolder.empty())
182  { currentFolder = currentPath.substr(lastPos, pos - lastPos) ; }
183  else
184  {
185  currentFolder += "/" ;
186  currentFolder += currentPath.substr(lastPos, pos - lastPos) ;
187  }
188  lastPos = currentPath.find_first_not_of(delimiter, pos);
189  pos = currentPath.find_first_of(delimiter, lastPos);
190 
191  subDirs = store_->getSubdirs() ;
192  for ( subDir = subDirs.begin() ; subDir != subDirs.end() ; subDir++ )
193  {
194  if (currentFolder!=(*subDir))
195  { store_->rmdir(*subDir) ; }
196  }
197  store_->cd(currentFolder) ;
198  }
199  }
200 
202  ( const std::string & name, const std::string & num, const std::string & denom,
203  const std::string & titleX, const std::string & titleY,
204  const std::string & title, const std::string & setEfficiencyFlag )
205  { return bookH1andDivide(name,get(num),get(denom),titleX,titleY,title,setEfficiencyFlag) ; }
206 
208  ( const std::string & name, const std::string & num, const std::string & denom,
209  const std::string & titleX, const std::string & titleY,
210  const std::string & title )
211  { return bookH2andDivide(name,get(num),get(denom),titleX,titleY,title) ; }
212 
214  ( const std::string & clone, const std::string & original,
215  const std::string & title )
216  { return cloneH1(clone,get(original),title) ; }
217 
219  ( const std::string & me2d, const std::string & title, const std::string & titleX, const std::string & titleY,
220  Double_t minimum, Double_t maximum )
221  { return profileX(get(me2d),title,titleX,titleY,minimum,maximum) ; }
222 
224  ( const std::string & me2d,
225  const std::string & title, const std::string & titleX, const std::string & titleY,
226  Double_t minimum, Double_t maximum )
227  { return profileY(get(me2d),title,titleX,titleY,minimum,maximum) ; }
228 
230  ( const std::string & name, const std::string & title,
231  int nchX, double lowX, double highX,
232  const std::string & titleX, const std::string & titleY,
233  Option_t * option )
234  {
235  MonitorElement * me = store_->book1D(newName(name),title,nchX,lowX,highX) ;
236  if (titleX!="") { me->getTH1F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
237  if (titleY!="") { me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
238  if (TString(option)!="") { me->getTH1F()->SetOption(option) ; }
239  return me ;
240  }
241 
243  ( const std::string & name, const std::string & title,
244  int nchX, double lowX, double highX,
245  const std::string & titleX, const std::string & titleY,
246  Option_t * option )
247  {
248  MonitorElement * me = store_->book1D(newName(name),title,nchX,lowX,highX) ;
249  me->getTH1F()->Sumw2() ;
250  if (titleX!="") { me->getTH1F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
251  if (titleY!="") { me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
252  if (TString(option)!="") { me->getTH1F()->SetOption(option) ; }
253  return me ;
254  }
255 
257  ( const std::string & name, const std::string & title,
258  int nchX, double lowX, double highX,
259  int nchY, double lowY, double highY,
260  const std::string & titleX, const std::string & titleY,
261  Option_t * option )
262  {
263  MonitorElement * me = store_->book2D(newName(name),title,nchX,lowX,highX,nchY,lowY,highY) ;
264  if (titleX!="") { me->getTH2F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
265  if (titleY!="") { me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
266  if (TString(option)!="") { me->getTH2F()->SetOption(option) ; }
267  return me ;
268  }
269 
271  ( const std::string & name, const std::string & title,
272  int nchX, double lowX, double highX,
273  int nchY, double lowY, double highY,
274  const std::string & titleX, const std::string & titleY,
275  Option_t * option )
276  {
277  MonitorElement * me = store_->book2D(newName(name),title,nchX,lowX,highX,nchY,lowY,highY) ;
278  me->getTH2F()->Sumw2() ;
279  if (titleX!="") { me->getTH2F()->GetXaxis()->SetTitle(titleX.c_str()) ; }
280  if (titleY!="") { me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str()) ; }
281  if (TString(option)!="") { me->getTH2F()->SetOption(option) ; }
282  return me ;
283  }
284 
286  ( const std::string & name, const std::string & title,
287  int nchX, double lowX, double highX,
288  double lowY, double highY,
289  const std::string & titleX, const std::string & titleY,
290  Option_t * option )
291  {
292  MonitorElement * me = store_->bookProfile(newName(name),title,nchX,lowX,highX,lowY,highY," ") ;
293  if (titleX!="") { me->getTProfile()->GetXaxis()->SetTitle(titleX.c_str()) ; }
294  if (titleY!="") { me->getTProfile()->GetYaxis()->SetTitle(titleY.c_str()) ; }
295  if (TString(option)!="") { me->getTProfile()->SetOption(option) ; }
296  return me ;
297  }
298 
300  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
301  const std::string & titleX, const std::string & titleY,
302  const std::string & title,const std::string & setEfficiencyFlag )
303  {
304  if ((!num)||(!denom)) return nullptr ;
305  std::string name2 = newName(name) ;
306  TH1F * h_temp = (TH1F *)num->getTH1F()->Clone(name2.c_str()) ;
307  h_temp->Reset() ;
308  h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
309  h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
310  h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
311  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
312  if (verbosity_>0) { h_temp->Print() ; }
313  MonitorElement * me = store_->book1D(name2,h_temp) ;
314  if (setEfficiencyFlag == "true") { me->setEfficiencyFlag(); }
315  delete h_temp ;
316  return me ;
317  }
318 
320  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
321  const std::string & titleX, const std::string & titleY,
322  const std::string & title )
323  {
324  if ((!num)||(!denom)) return nullptr ;
325  std::string name2 = newName(name) ;
326  TH2F * h_temp = (TH2F *)num->getTH2F()->Clone(name2.c_str()) ;
327  h_temp->Reset() ;
328  h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
329  h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
330  h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
331  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
332  if (verbosity_>0) { h_temp->Print() ; }
333  MonitorElement * me = store_->book2D(name2,h_temp) ;
334  delete h_temp ;
335  return me ;
336  }
337 
339  ( const std::string & name, MonitorElement * original,
340  const std::string & title )
341  {
342  if (!original) return nullptr ;
343  std::string name2 = newName(name) ;
344  TH1F * h_temp = (TH1F *)original->getTH1F()->Clone(name2.c_str()) ;
345  h_temp->Reset() ;
346  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
347  MonitorElement * me = store_->book1D(name2,h_temp) ;
348  delete h_temp ;
349  return me ;
350  }
351 
353  ( MonitorElement * me2d,
354  const std::string & title, const std::string & titleX, const std::string & titleY,
355  Double_t minimum, Double_t maximum )
356  {
357  if(!me2d) { return nullptr;}
358  std::string name2 = me2d->getName()+"_pfx" ;
359  TProfile * p1_temp = me2d->getTH2F()->ProfileX() ;
360  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
361  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
362  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
363  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
364  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
365  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
366  delete p1_temp ;
367  return me ;
368  }
369 
371  ( MonitorElement * me2d,
372  const std::string & title, const std::string & titleX, const std::string & titleY,
373  Double_t minimum, Double_t maximum )
374  {
375  if(!me2d) { return nullptr;}
376  std::string name2 = me2d->getName()+"_pfy" ;
377  TProfile * p1_temp = me2d->getTH2F()->ProfileY() ;
378  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
379  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
380  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
381  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
382  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
383  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
384  delete p1_temp ;
385  return me ;
386  }
387 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
const std::string & getName(void) const
get name of ME
MonitorElement * bookP1(const std::string &name, const std::string &title, int nchX, double lowX, double highX, double lowY, double highY, const std::string &titleX="", const std::string &titleY="", Option_t *option="E1 P")
MonitorElement * bookH1(const std::string &name, const std::string &title, int nchX, double lowX, double highX, const std::string &titleX="", const std::string &titleY="Events", Option_t *option="E1 P")
std::vector< std::string > getSubdirs(void) const
Definition: DQMStore.cc:1676
void remove(const std::string &name)
void rmdir(const std::string &fullpath)
Definition: DQMStore.cc:3124
virtual void endRun(edm::Run const &, edm::EventSetup const &)
void cd(void)
go to top directory (ie. root)
Definition: DQMStore.cc:661
list original
Definition: definitions.py:57
MonitorElement * profileY(MonitorElement *me2d, const std::string &title="", const std::string &titleX="", const std::string &titleY="", Double_t minimum=-1111, Double_t maximum=-1111)
#define nullptr
uint16_t size_type
MonitorElement * bookH1withSumw2(const std::string &name, const std::string &title, int nchX, double lowX, double highX, const std::string &titleX="", const std::string &titleY="Events", Option_t *option="E1 P")
const std::string * find(const std::string &name)
MonitorElement * cloneH1(const std::string &name, MonitorElement *original, const std::string &title="")
virtual void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
void removeElement(const std::string &name)
Definition: DQMStore.cc:3166
MonitorElement * profileX(MonitorElement *me2d, const std::string &title="", const std::string &titleX="", const std::string &titleY="", Double_t minimum=-1111, Double_t maximum=-1111)
TH1 * getTH1(void) const
void setBookPrefix(const std::string &)
void setVerbose(unsigned level)
Definition: DQMStore.cc:648
ElectronDqmAnalyzerBase(const edm::ParameterSet &conf)
MonitorElement * bookH1andDivide(const std::string &name, MonitorElement *num, MonitorElement *denom, const std::string &titleX, const std::string &titleY, const std::string &title="", const std::string &setEfficiencyFlag="")
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1725
tuple conf
Definition: dbtoconf.py:185
MonitorElement * bookH2andDivide(const std::string &name, MonitorElement *num, MonitorElement *denom, const std::string &titleX, const std::string &titleY, const std::string &title="")
string fullName
std::vector< std::string > histoNames_
TH1F * getTH1F(void) const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
MonitorElement * bookH2(const std::string &name, const std::string &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, const std::string &titleX="", const std::string &titleY="", Option_t *option="COLZ")
std::vector< std::string > getMEs(void) const
get list of (non-dir) MEs of current directory
Definition: DQMStore.cc:1699
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, const uint32_t lumi=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE", const bool resetMEsAfterWriting=false)
Definition: DQMStore.cc:2562
TProfile * getTProfile(void) const
bool open(const std::string &filename, bool overwrite=false, const std::string &path="", const std::string &prepend="", OpenRunDirs stripdirs=KeepRunDirs, bool fileMustExist=true)
Definition: DQMStore.cc:2917
void setEfficiencyFlag(void)
volatile std::atomic< bool > shutdown_flag false
TH2F * getTH2F(void) const
MonitorElement * bookH2withSumw2(const std::string &name, const std::string &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, const std::string &titleX="", const std::string &titleY="", Option_t *option="COLZ")
MonitorElement * get(const std::string &name)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:684
Definition: Run.h:41
std::string newName(const std::string &name)
const std::string & pwd(void) const
Definition: DQMStore.cc:656