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  std::string name2 = newName(name) ;
305  TH1F * h_temp = (TH1F *)num->getTH1F()->Clone(name2.c_str()) ;
306  h_temp->Reset() ;
307  h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
308  h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
309  h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
310  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
311  if (verbosity_>0) { h_temp->Print() ; }
312  MonitorElement * me = store_->book1D(name2,h_temp) ;
313  delete h_temp ;
314  return me ;
315  }
316 
318  ( const std::string & name, MonitorElement * num, MonitorElement * denom,
319  const std::string & titleX, const std::string & titleY,
320  const std::string & title )
321  {
322  std::string name2 = newName(name) ;
323  TH2F * h_temp = (TH2F *)num->getTH2F()->Clone(name2.c_str()) ;
324  h_temp->Reset() ;
325  h_temp->Divide(num->getTH1(),denom->getTH1(),1,1,"b") ;
326  h_temp->GetXaxis()->SetTitle(titleX.c_str()) ;
327  h_temp->GetYaxis()->SetTitle(titleY.c_str()) ;
328  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
329  if (verbosity_>0) { h_temp->Print() ; }
330  MonitorElement * me = store_->book2D(name2,h_temp) ;
331  delete h_temp ;
332  return me ;
333  }
334 
336  ( const std::string & name, MonitorElement * original,
337  const std::string & title )
338  {
339  std::string name2 = newName(name) ;
340  TH1F * h_temp = (TH1F *)original->getTH1F()->Clone(name2.c_str()) ;
341  h_temp->Reset() ;
342  if (title!="") { h_temp->SetTitle(title.c_str()) ; }
343  MonitorElement * me = store_->book1D(name2,h_temp) ;
344  delete h_temp ;
345  return me ;
346  }
347 
349  ( MonitorElement * me2d,
350  const std::string & title, const std::string & titleX, const std::string & titleY,
351  Double_t minimum, Double_t maximum )
352  {
353  std::string name2 = me2d->getName()+"_pfx" ;
354  TProfile * p1_temp = me2d->getTH2F()->ProfileX() ;
355  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
356  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
357  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
358  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
359  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
360  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
361  delete p1_temp ;
362  return me ;
363  }
364 
366  ( MonitorElement * me2d,
367  const std::string & title, const std::string & titleX, const std::string & titleY,
368  Double_t minimum, Double_t maximum )
369  {
370  std::string name2 = me2d->getName()+"_pfy" ;
371  TProfile * p1_temp = me2d->getTH2F()->ProfileY() ;
372  if (title!="") { p1_temp->SetTitle(title.c_str()) ; }
373  if (titleX!="") { p1_temp->GetXaxis()->SetTitle(titleX.c_str()) ; }
374  if (titleY!="") { p1_temp->GetYaxis()->SetTitle(titleY.c_str()) ; }
375  if (minimum!=-1111) { p1_temp->SetMinimum(minimum) ; }
376  if (maximum!=-1111) { p1_temp->SetMaximum(maximum) ; }
377  MonitorElement * me = store_->bookProfile(name2,p1_temp) ;
378  delete p1_temp ;
379  return me ;
380  }
381 
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:1216
void remove(const std::string &name)
void rmdir(const std::string &fullpath)
Definition: DQMStore.cc:2311
virtual void endRun(edm::Run const &, edm::EventSetup const &)
void cd(void)
go to top directory (ie. root)
Definition: DQMStore.cc:209
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:1898
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:2353
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:196
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:1265
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_
T * clone(const T *tp)
Definition: Ptr.h:42
TH1F * getTH1F(void) const
long long int num
Definition: procUtils.cc:71
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:1239
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:2213
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:232
Definition: Run.h:32
std::string newName(const std::string &name)
const std::string & pwd(void) const
Definition: DQMStore.cc:204