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 
39 std::string ElectronDqmAnalyzerBase::newName( const std::string & name )
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 
51 const std::string * ElectronDqmAnalyzerBase::find( const std::string & name )
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 0 ; }
155  }
156 
157 void ElectronDqmAnalyzerBase::remove( const std::string & name )
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 )
205  { return bookH1andDivide(name,get(num),get(denom),titleX,titleY,title) ; }
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 )
303  {
304  if ((!num)||(!denom)) return 0 ;
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  delete h_temp ;
315  return me ;
316  }
317 
319  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
320  const std::string & titleX, const std::string & titleY,
321  const std::string & title )
322  {
323  if ((!num)||(!denom)) return 0 ;
324  std::string name2 = newName(name) ;
325  TH2F * h_temp = (TH2F *)num->getTH2F()->Clone(name2.c_str()) ;
326  h_temp->Reset() ;
327  h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
328  h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
329  h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
330  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
331  if (verbosity_>0) { h_temp->Print() ; }
332  MonitorElement * me = store_->book2D(name2,h_temp) ;
333  delete h_temp ;
334  return me ;
335  }
336 
338  ( const std::string & name, MonitorElement * original,
339  const std::string & title )
340  {
341  if (!original) return 0 ;
342  std::string name2 = newName(name) ;
343  TH1F * h_temp = (TH1F *)original->getTH1F()->Clone(name2.c_str()) ;
344  h_temp->Reset() ;
345  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
346  MonitorElement * me = store_->book1D(name2,h_temp) ;
347  delete h_temp ;
348  return me ;
349  }
350 
352  ( MonitorElement * me2d,
353  const std::string & title, const std::string & titleX, const std::string & titleY,
354  Double_t minimum, Double_t maximum )
355  {
356  std::string name2 = me2d->getName()+"_pfx" ;
357  TProfile * p1_temp = me2d->getTH2F()->ProfileX() ;
358  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
359  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
360  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
361  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
362  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
363  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
364  delete p1_temp ;
365  return me ;
366  }
367 
369  ( MonitorElement * me2d,
370  const std::string & title, const std::string & titleX, const std::string & titleY,
371  Double_t minimum, Double_t maximum )
372  {
373  std::string name2 = me2d->getName()+"_pfy" ;
374  TProfile * p1_temp = me2d->getTH2F()->ProfileY() ;
375  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
376  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
377  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
378  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
379  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
380  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
381  delete p1_temp ;
382  return me ;
383  }
384 
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:1419
void remove(const std::string &name)
void rmdir(const std::string &fullpath)
Definition: DQMStore.cc:2530
virtual void endRun(edm::Run const &, edm::EventSetup const &)
void cd(void)
go to top directory (ie. root)
Definition: DQMStore.cc:406
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2113
list original
Definition: definitions.py:60
MonitorElement * profileY(MonitorElement *me2d, const std::string &title="", const std::string &titleX="", const std::string &titleY="", Double_t minimum=-1111, Double_t maximum=-1111)
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:2572
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:393
ElectronDqmAnalyzerBase(const edm::ParameterSet &conf)
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1468
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
long long int num
Definition: procUtils.cc:71
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:1442
TProfile * getTProfile(void) const
MonitorElement * bookH1andDivide(const std::string &name, MonitorElement *num, MonitorElement *denom, const std::string &titleX, const std::string &titleY, const std::string &title="")
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:2432
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:429
Definition: Run.h:33
std::string newName(const std::string &name)
const std::string & pwd(void) const
Definition: DQMStore.cc:401