CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMGenericClient.cc
Go to the documentation of this file.
1 /*
2  * Class:DQMGenericClient
3  *
4  *
5  *
6  * \author Junghwan Goh - SungKyunKwan University
7  */
8 
10 
16 
17 #include <TH1F.h>
18 #include <TClass.h>
19 #include <TString.h>
20 #include <TPRegexp.h>
21 
22 #include <cmath>
23 #include <climits>
24 #include <boost/tokenizer.hpp>
25 
26 using namespace std;
27 using namespace edm;
28 
30 
31 TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]");
32 TPRegexp nonPerlWildcard("\\w\\*|^\\*");
33 
35 {
36  typedef std::vector<edm::ParameterSet> VPSet;
37  typedef std::vector<std::string> vstring;
38  typedef boost::escaped_list_separator<char> elsc;
39 
40  elsc commonEscapes("\\", " \t", "\'");
41 
42  verbose_ = pset.getUntrackedParameter<unsigned int>("verbose", 0);
43 
44  // Parse efficiency commands
45  vstring effCmds = pset.getParameter<vstring>("efficiency");
46  for ( vstring::const_iterator effCmd = effCmds.begin();
47  effCmd != effCmds.end(); ++effCmd )
48  {
49  if ( effCmd->empty() ) continue;
50 
51  boost::tokenizer<elsc> tokens(*effCmd, commonEscapes);
52 
53  vector<string> args;
54  for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
55  iToken != tokens.end(); ++iToken) {
56  if ( iToken->empty() ) continue;
57  args.push_back(*iToken);
58  }
59 
60  if ( args.size() < 4 ) {
61  LogInfo("DQMGenericClient") << "Wrong input to effCmds\n";
62  continue;
63  }
64 
65  EfficOption opt;
66  opt.name = args[0];
67  opt.title = args[1];
68  opt.numerator = args[2];
69  opt.denominator = args[3];
70  opt.isProfile = false;
71 
72  const string typeName = args.size() == 4 ? "eff" : args[4];
73  if ( typeName == "eff" ) opt.type = 1;
74  else if ( typeName == "fake" ) opt.type = 2;
75  else opt.type = 0;
76 
77  efficOptions_.push_back(opt);
78  }
79 
80  VPSet efficSets = pset.getUntrackedParameter<VPSet>("efficiencySets", VPSet());
81  for ( VPSet::const_iterator efficSet = efficSets.begin();
82  efficSet != efficSets.end(); ++efficSet )
83  {
84  EfficOption opt;
85  opt.name = efficSet->getUntrackedParameter<string>("name");
86  opt.title = efficSet->getUntrackedParameter<string>("title");
87  opt.numerator = efficSet->getUntrackedParameter<string>("numerator");
88  opt.denominator = efficSet->getUntrackedParameter<string>("denominator");
89  opt.isProfile = false;
90 
91  const string typeName = efficSet->getUntrackedParameter<string>("typeName", "eff");
92  if ( typeName == "eff" ) opt.type = 1;
93  else if ( typeName == "fake" ) opt.type = 2;
94  else opt.type = 0;
95 
96  efficOptions_.push_back(opt);
97  }
98 
99  // Parse profiles
100  vstring profileCmds = pset.getUntrackedParameter<vstring>("efficiencyProfile", vstring());
101  for ( vstring::const_iterator profileCmd = profileCmds.begin();
102  profileCmd != profileCmds.end(); ++profileCmd )
103  {
104  if ( profileCmd->empty() ) continue;
105 
106  boost::tokenizer<elsc> tokens(*profileCmd, commonEscapes);
107 
108  vector<string> args;
109  for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
110  iToken != tokens.end(); ++iToken) {
111  if ( iToken->empty() ) continue;
112  args.push_back(*iToken);
113  }
114 
115  if ( args.size() < 4 ) {
116  LogInfo("DQMGenericClient") << "Wrong input to profileCmds\n";
117  continue;
118  }
119 
120  EfficOption opt;
121  opt.name = args[0];
122  opt.title = args[1];
123  opt.numerator = args[2];
124  opt.denominator = args[3];
125  opt.isProfile = true;
126 
127  const string typeName = args.size() == 4 ? "eff" : args[4];
128  if ( typeName == "eff" ) opt.type = 1;
129  else if ( typeName == "fake" ) opt.type = 2;
130  else opt.type = 0;
131 
132  efficOptions_.push_back(opt);
133  }
134 
135  VPSet profileSets = pset.getUntrackedParameter<VPSet>("efficiencyProfileSets", VPSet());
136  for ( VPSet::const_iterator profileSet = profileSets.begin();
137  profileSet != profileSets.end(); ++profileSet )
138  {
139  EfficOption opt;
140  opt.name = profileSet->getUntrackedParameter<string>("name");
141  opt.title = profileSet->getUntrackedParameter<string>("title");
142  opt.numerator = profileSet->getUntrackedParameter<string>("numerator");
143  opt.denominator = profileSet->getUntrackedParameter<string>("denominator");
144  opt.isProfile = true;
145 
146  const string typeName = profileSet->getUntrackedParameter<string>("typeName", "eff");
147  if ( typeName == "eff" ) opt.type = 1;
148  else if ( typeName == "fake" ) opt.type = 2;
149  else opt.type = 0;
150 
151  efficOptions_.push_back(opt);
152  }
153 
154  // Parse resolution commands
155  vstring resCmds = pset.getParameter<vstring>("resolution");
156  for ( vstring::const_iterator resCmd = resCmds.begin();
157  resCmd != resCmds.end(); ++resCmd )
158  {
159  if ( resCmd->empty() ) continue;
160  boost::tokenizer<elsc> tokens(*resCmd, commonEscapes);
161 
162  vector<string> args;
163  for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
164  iToken != tokens.end(); ++iToken) {
165  if ( iToken->empty() ) continue;
166  args.push_back(*iToken);
167  }
168 
169  if ( args.size() != 3 ) {
170  LogInfo("DQMGenericClient") << "Wrong input to resCmds\n";
171  continue;
172  }
173 
174  ResolOption opt;
175  opt.namePrefix = args[0];
176  opt.titlePrefix = args[1];
177  opt.srcName = args[2];
178 
179  resolOptions_.push_back(opt);
180  }
181 
182  VPSet resolSets = pset.getUntrackedParameter<VPSet>("resolutionSets", VPSet());
183  for ( VPSet::const_iterator resolSet = resolSets.begin();
184  resolSet != resolSets.end(); ++resolSet )
185  {
186  ResolOption opt;
187  opt.namePrefix = resolSet->getUntrackedParameter<string>("namePrefix");
188  opt.titlePrefix = resolSet->getUntrackedParameter<string>("titlePrefix");
189  opt.srcName = resolSet->getUntrackedParameter<string>("srcName");
190 
191  resolOptions_.push_back(opt);
192  }
193 
194  // Parse Normalization commands
195  vstring normCmds = pset.getUntrackedParameter<vstring>("normalization", vstring());
196  for ( vstring::const_iterator normCmd = normCmds.begin();
197  normCmd != normCmds.end(); ++normCmd )
198  {
199  if ( normCmd->empty() ) continue;
200  boost::tokenizer<elsc> tokens(*normCmd, commonEscapes);
201 
202  vector<string> args;
203  for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
204  iToken != tokens.end(); ++iToken) {
205  if ( iToken->empty() ) continue;
206  args.push_back(*iToken);
207  }
208 
209  if ( args.empty() or args.size() > 2 ) {
210  LogInfo("DQMGenericClient") << "Wrong input to normCmds\n";
211  continue;
212  }
213 
214  NormOption opt;
215  opt.name = args[0];
216  opt.normHistName = args.size() == 2 ? args[1] : args[0];
217 
218  normOptions_.push_back(opt);
219  }
220 
221  VPSet normSets = pset.getUntrackedParameter<VPSet>("normalizationSets", VPSet());
222  for ( VPSet::const_iterator normSet = normSets.begin();
223  normSet != normSets.end(); ++normSet )
224  {
225  NormOption opt;
226  opt.name = normSet->getUntrackedParameter<string>("name");
227  opt.normHistName = normSet->getUntrackedParameter<string>("normalizedTo", opt.name);
228 
229  normOptions_.push_back(opt);
230  }
231 
232  // Cumulative distributions
233  vstring cdCmds = pset.getUntrackedParameter<vstring>("cumulativeDists", vstring());
234  for ( vstring::const_iterator cdCmd = cdCmds.begin();
235  cdCmd != cdCmds.end(); ++cdCmd )
236  {
237  if ( cdCmd->empty() ) continue;
238  boost::tokenizer<elsc> tokens(*cdCmd, commonEscapes);
239 
240  vector<string> args;
241  for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
242  iToken != tokens.end(); ++iToken) {
243  if ( iToken->empty() ) continue;
244  args.push_back(*iToken);
245  }
246 
247  if ( args.size() != 1 ) {
248  LogInfo("DQMGenericClient") << "Wrong input to cdCmds\n";
249  continue;
250  }
251 
252  CDOption opt;
253  opt.name = args[0];
254 
255  cdOptions_.push_back(opt);
256  }
257 
258  VPSet cdSets = pset.getUntrackedParameter<VPSet>("cumulativeDistSets", VPSet());
259  for ( VPSet::const_iterator cdSet = cdSets.begin();
260  cdSet != cdSets.end(); ++cdSet )
261  {
262  CDOption opt;
263  opt.name = cdSet->getUntrackedParameter<string>("name");
264 
265  cdOptions_.push_back(opt);
266  }
267 
268  outputFileName_ = pset.getUntrackedParameter<string>("outputFileName", "");
269  subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
270 
271  resLimitedFit_ = pset.getUntrackedParameter<bool>("resolutionLimitedFit",false);
272  isWildcardUsed_ = false;
273 }
274 
276 
277  typedef vector<string> vstring;
278 
279  // Update 2014-04-02
280  // Migrated back to the endJob. the DQMFileSaver logic has
281  // to be reviewed to guarantee that the endJob is properly
282  // considered. The splitting per run is done centrally when
283  // running the harvesting in production
284 
285  // Update 2009-09-23
286  // Migrated all code from endJob to this function
287  // endJob is not necessarily called in the proper sequence
288  // and does not necessarily book histograms produced in
289  // that step.
290  // It more robust to do the histogram manipulation in
291  // this endRun function
292 
293  // needed to access the DQMStore::save method
294  theDQM = 0;
295  theDQM = Service<DQMStore>().operator->();
296 
297  // Process wildcard in the sub-directory
298  set<string> subDirSet;
299 
300  for(vstring::const_iterator iSubDir = subDirs_.begin();
301  iSubDir != subDirs_.end(); ++iSubDir) {
302  string subDir = *iSubDir;
303 
304  if ( subDir[subDir.size()-1] == '/' ) subDir.erase(subDir.size()-1);
305 
306  if ( TString(subDir).Contains(metacharacters) ) {
307  isWildcardUsed_ = true;
308 
309  const string::size_type shiftPos = subDir.rfind('/');
310  const string searchPath = subDir.substr(0, shiftPos);
311  const string pattern = subDir.substr(shiftPos + 1, subDir.length());
312  //std::cout << "\n\n\n\nLooking for all subdirs of " << subDir << std::endl;
313 
314  findAllSubdirectories (ibooker, igetter, searchPath, &subDirSet, pattern);
315 
316  }
317  else {
318  subDirSet.insert(subDir);
319  }
320  }
321 
322  for(set<string>::const_iterator iSubDir = subDirSet.begin();
323  iSubDir != subDirSet.end(); ++iSubDir) {
324  const string& dirName = *iSubDir;
325 
326  for ( vector<EfficOption>::const_iterator efficOption = efficOptions_.begin();
327  efficOption != efficOptions_.end(); ++efficOption )
328  {
329  computeEfficiency(ibooker, igetter, dirName, efficOption->name, efficOption->title,
330  efficOption->numerator, efficOption->denominator,
331  efficOption->type, efficOption->isProfile);
332  }
333 
334  for ( vector<ResolOption>::const_iterator resolOption = resolOptions_.begin();
335  resolOption != resolOptions_.end(); ++resolOption )
336  {
337  computeResolution(ibooker, igetter, dirName, resolOption->namePrefix, resolOption->titlePrefix, resolOption->srcName);
338  }
339 
340  for ( vector<NormOption>::const_iterator normOption = normOptions_.begin();
341  normOption != normOptions_.end(); ++normOption )
342  {
343  normalizeToEntries(ibooker, igetter, dirName, normOption->name, normOption->normHistName);
344  }
345 
346  for ( vector<CDOption>::const_iterator cdOption = cdOptions_.begin();
347  cdOption != cdOptions_.end(); ++cdOption )
348  {
349  makeCumulativeDist(ibooker, igetter, dirName, cdOption->name);
350  }
351  }
352 
353  if ( ! outputFileName_.empty() ) theDQM->save(outputFileName_);
354 
355 }
356 
357 void DQMGenericClient::computeEfficiency (DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter, const string& startDir, const string& efficMEName,
358  const string& efficMETitle, const string& recoMEName, const string& simMEName, const int type, const bool makeProfile)
359 {
360  if ( ! igetter.dirExists(startDir) ) {
361  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
362  LogInfo("DQMGenericClient") << "computeEfficiency() : "
363  << "Cannot find sub-directory " << startDir << endl;
364  }
365  return;
366  }
367 
368  ibooker.cd();
369 
370  ME* simME = igetter.get(startDir+"/"+simMEName);
371  ME* recoME = igetter.get(startDir+"/"+recoMEName);
372 
373  if ( !simME ) {
374  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
375  LogInfo("DQMGenericClient") << "computeEfficiency() : "
376  << "No sim-ME '" << simMEName << "' found\n";
377  }
378  return;
379  }
380 
381  if ( !recoME ) {
382  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
383  LogInfo("DQMGenericClient") << "computeEfficiency() : "
384  << "No reco-ME '" << recoMEName << "' found\n";
385  }
386  return;
387  }
388 
389  // Treat everything as the base class, TH1
390 
391  TH1* hSim = simME ->getTH1();
392  TH1* hReco = recoME->getTH1();
393 
394  if ( !hSim || !hReco ) {
395  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
396  LogInfo("DQMGenericClient") << "computeEfficiency() : "
397  << "Cannot create TH1 from ME\n";
398  }
399  return;
400  }
401 
402  string efficDir = startDir;
403  string newEfficMEName = efficMEName;
404  string::size_type shiftPos;
405  if ( string::npos != (shiftPos = efficMEName.rfind('/')) ) {
406  efficDir += "/"+efficMEName.substr(0, shiftPos);
407  newEfficMEName.erase(0, shiftPos+1);
408  }
409  ibooker.setCurrentFolder(efficDir);
410 
411  if (makeProfile) {
412  TProfile * efficHist = (hReco->GetXaxis()->GetXbins()->GetSize()==0) ?
413  new TProfile(newEfficMEName.c_str(), efficMETitle.c_str(),
414  hReco->GetXaxis()->GetNbins(),
415  hReco->GetXaxis()->GetXmin(),
416  hReco->GetXaxis()->GetXmax()) :
417  new TProfile(newEfficMEName.c_str(), efficMETitle.c_str(),
418  hReco->GetXaxis()->GetNbins(),
419  hReco->GetXaxis()->GetXbins()->GetArray());
420 
421  efficHist->GetXaxis()->SetTitle(hSim->GetXaxis()->GetTitle());
422  efficHist->GetYaxis()->SetTitle(hSim->GetYaxis()->GetTitle());
423 
424 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,27,0)
425  for (int i=1; i <= hReco->GetNbinsX(); i++) {
426 
427  const double nReco = hReco->GetBinContent(i);
428  const double nSim = hSim->GetBinContent(i);
429  if(nSim > INT_MAX || nSim < INT_MIN || nReco > INT_MAX || nReco < INT_MIN)
430  {
431  LogError("DQMGenericClient") << "computeEfficiency() : "
432  << "Overflow: bin content either too large or too small to be casted to int";
433  return;
434  }
435 
436  if(std::string(hSim->GetXaxis()->GetBinLabel(i)) != "")
437  efficHist->GetXaxis()->SetBinLabel(i, hSim->GetXaxis()->GetBinLabel(i));
438 
439  if ( nSim == 0 || nReco > nSim ) continue;
440  const double effVal = nReco/nSim;
441 
442  const double errLo = TEfficiency::ClopperPearson((int)nSim,
443  (int)nReco,
444  0.683,false);
445  const double errUp = TEfficiency::ClopperPearson((int)nSim,
446  (int)nReco,
447  0.683,true);
448  const double errVal = (effVal - errLo > errUp - effVal) ? effVal - errLo : errUp - effVal;
449  efficHist->SetBinContent(i, effVal);
450  efficHist->SetBinEntries(i, 1);
451  efficHist->SetBinError(i, sqrt(effVal * effVal + errVal * errVal));
452  }
453 #else
454  for (int i=1; i <= hReco->GetNbinsX(); i++) {
455 
456  const double nReco = hReco->GetBinContent(i);
457  const double nSim = hSim->GetBinContent(i);
458  if(nSim > INT_MAX || nSim < INT_MIN || nReco > INT_MAX || nReco < INT_MIN)
459  {
460  LogError("DQMGenericClient") << "computeEfficiency() : "
461  << "Overflow: bin content either too large or too small to be casted to int";
462  return;
463  }
464 
465  TGraphAsymmErrorsWrapper asymm;
466  std::pair<double, double> efficiencyWithError;
467  efficiencyWithError = asymm.efficiency((int)nReco,
468  (int)nSim);
469  double effVal = efficiencyWithError.first;
470  double errVal = efficiencyWithError.second;
471  if ((int)nSim > 0) {
472  efficHist->SetBinContent(i, effVal);
473  efficHist->SetBinEntries(i, 1);
474  efficHist->SetBinError(i, sqrt(effVal * effVal + errVal * errVal));
475  }
476  if(std::string(hSim->GetXaxis()->GetBinLabel(i)) != "")
477  efficHist->GetXaxis()->SetBinLabel(i, hSim->GetXaxis()->GetBinLabel(i));
478  }
479 #endif
480  ibooker.bookProfile(newEfficMEName.c_str(),efficHist);
481  delete efficHist;
482  }
483 
484  else {
485 
486  TH1* efficHist = (TH1*)hSim->Clone(newEfficMEName.c_str());
487  efficHist->SetTitle(efficMETitle.c_str());
488 
489  // Here is where you have trouble --- you need
490  // to understand what type of hist you have.
491 
492  ME* efficME = 0;
493 
494  // Parse the class name
495  // This works, but there might be a better way
496  TClass * myHistClass = efficHist->IsA();
497  TString histClassName = myHistClass->GetName();
498 
499  if (histClassName == "TH1F"){
500  efficME = ibooker.book1D(newEfficMEName, (TH1F*)efficHist);
501  } else if (histClassName == "TH2F"){
502  efficME = ibooker.book2D(newEfficMEName, (TH2F*)efficHist);
503  } else if (histClassName == "TH3F"){
504  efficME = ibooker.book3D(newEfficMEName, (TH3F*)efficHist);
505  }
506 
507 
508  if ( !efficME ) {
509  LogInfo("DQMGenericClient") << "computeEfficiency() : "
510  << "Cannot book effic-ME from the DQM\n";
511  return;
512  }
513 
514  // Update: 2009-9-16 slaunwhj
515  // call the most generic efficiency function
516  // works up to 3-d histograms
517 
518  generic_eff (hSim, hReco, efficME, type);
519 
520  // const int nBin = efficME->getNbinsX();
521  // for(int bin = 0; bin <= nBin; ++bin) {
522  // const float nSim = simME ->getBinContent(bin);
523  // const float nReco = recoME->getBinContent(bin);
524  // float eff =0;
525  // if (type=="fake")eff = nSim ? 1-nReco/nSim : 0.;
526  // else eff= nSim ? nReco/nSim : 0.;
527  // const float err = nSim && eff <= 1 ? sqrt(eff*(1-eff)/nSim) : 0.;
528  // efficME->setBinContent(bin, eff);
529  // efficME->setBinError(bin, err);
530  // }
531  efficME->setEntries(simME->getEntries());
532 
533  }
534 
535  // Global efficiency
536  ME* globalEfficME = igetter.get(efficDir+"/globalEfficiencies");
537  if ( !globalEfficME ) globalEfficME = ibooker.book1D("globalEfficiencies", "Global efficiencies", 1, 0, 1);
538  if ( !globalEfficME ) {
539  LogInfo("DQMGenericClient") << "computeEfficiency() : "
540  << "Cannot book globalEffic-ME from the DQM\n";
541  return;
542  }
543  TH1F* hGlobalEffic = globalEfficME->getTH1F();
544  if ( !hGlobalEffic ) {
545  LogInfo("DQMGenericClient") << "computeEfficiency() : "
546  << "Cannot create TH1F from ME, globalEfficME\n";
547  return;
548  }
549 
550  const float nSimAll = hSim->GetEntries();
551  const float nRecoAll = hReco->GetEntries();
552  float efficAll=0;
553  if ( type == 1 ) efficAll = nSimAll ? nRecoAll/nSimAll : 0;
554  else if ( type == 2 ) efficAll = nSimAll ? 1-nRecoAll/nSimAll : 0;
555  const float errorAll = nSimAll && efficAll < 1 ? sqrt(efficAll*(1-efficAll)/nSimAll) : 0;
556 
557  const int iBin = hGlobalEffic->Fill(newEfficMEName.c_str(), 0);
558  hGlobalEffic->SetBinContent(iBin, efficAll);
559  hGlobalEffic->SetBinError(iBin, errorAll);
560 }
561 
562 void DQMGenericClient::computeResolution(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter, const string& startDir, const string& namePrefix, const string& titlePrefix,
563  const std::string& srcName)
564 {
565  if ( ! igetter.dirExists(startDir) ) {
566  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
567  LogInfo("DQMGenericClient") << "computeResolution() : "
568  << "Cannot find sub-directory " << startDir << endl;
569  }
570  return;
571  }
572 
573  ibooker.cd();
574 
575  ME* srcME = igetter.get(startDir+"/"+srcName);
576  if ( !srcME ) {
577  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
578  LogInfo("DQMGenericClient") << "computeResolution() : "
579  << "No source ME '" << srcName << "' found\n";
580  }
581  return;
582  }
583 
584  TH2F* hSrc = srcME->getTH2F();
585  if ( !hSrc ) {
586  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
587  LogInfo("DQMGenericClient") << "computeResolution() : "
588  << "Cannot create TH2F from source-ME\n";
589  }
590  return;
591  }
592 
593  const int nBin = hSrc->GetNbinsX();
594 
595  string newDir = startDir;
596  string newPrefix = namePrefix;
597  string::size_type shiftPos;
598  if ( string::npos != (shiftPos = namePrefix.rfind('/')) ) {
599  newDir += "/"+namePrefix.substr(0, shiftPos);
600  newPrefix.erase(0, shiftPos+1);
601  }
602 
603  ibooker.setCurrentFolder(newDir);
604 
605  float * lowedgesfloats = new float[nBin+1];
606  ME* meanME;
607  ME* sigmaME;
608  if (hSrc->GetXaxis()->GetXbins()->GetSize())
609  {
610  for (int j=0; j<nBin+1; ++j)
611  lowedgesfloats[j] = (float)hSrc->GetXaxis()->GetXbins()->GetAt(j);
612  meanME = ibooker.book1D(newPrefix+"_Mean", titlePrefix+" Mean", nBin, lowedgesfloats);
613  sigmaME = ibooker.book1D(newPrefix+"_Sigma", titlePrefix+" Sigma", nBin, lowedgesfloats);
614  }
615  else
616  {
617  meanME = ibooker.book1D(newPrefix+"_Mean", titlePrefix+" Mean", nBin,
618  hSrc->GetXaxis()->GetXmin(),
619  hSrc->GetXaxis()->GetXmax());
620  sigmaME = ibooker.book1D(newPrefix+"_Sigma", titlePrefix+" Sigma", nBin,
621  hSrc->GetXaxis()->GetXmin(),
622  hSrc->GetXaxis()->GetXmax());
623  }
624 
625  if (meanME && sigmaME)
626  {
627  meanME->setEfficiencyFlag();
628  sigmaME->setEfficiencyFlag();
629 
630  if (! resLimitedFit_ ) {
631  FitSlicesYTool fitTool(srcME);
632  fitTool.getFittedMeanWithError(meanME);
633  fitTool.getFittedSigmaWithError(sigmaME);
635  } else {
636  limitedFit(srcME,meanME,sigmaME);
637  }
638  }
639  delete[] lowedgesfloats;
640 }
641 
642 void DQMGenericClient::normalizeToEntries(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter, const std::string& startDir, const std::string& histName,
643  const std::string& normHistName)
644 {
645  if ( ! igetter.dirExists(startDir) ) {
646  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
647  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
648  << "Cannot find sub-directory " << startDir << endl;
649  }
650  return;
651  }
652 
653  ibooker.cd();
654 
655  ME* element = igetter.get(startDir+"/"+histName);
656  ME* normME = igetter.get(startDir+"/"+normHistName);
657 
658  if ( !element ) {
659  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
660  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
661  << "No such element '" << histName << "' found\n";
662  }
663  return;
664  }
665 
666  if ( !normME ) {
667  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
668  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
669  << "No such element '" << normHistName << "' found\n";
670  }
671  return;
672  }
673 
674  TH1F* hist = element->getTH1F();
675  if ( !hist) {
676  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
677  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
678  << "Cannot create TH1F from ME\n";
679  }
680  return;
681  }
682 
683  TH1F* normHist = normME->getTH1F();
684  if ( !normHist ) {
685  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
686  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
687  << "Cannot create TH1F from ME\n";
688  }
689  return;
690  }
691 
692  const double entries = normHist->GetEntries();
693  if ( entries != 0 ) {
694  hist->Scale(1./entries);
695  }
696  else {
697  LogInfo("DQMGenericClient") << "normalizeToEntries() : "
698  << "Zero entries in histogram\n";
699  }
700 
701  return;
702 }
703 
704 void DQMGenericClient::makeCumulativeDist(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter, const std::string& startDir, const std::string& cdName)
705 {
706  if ( ! igetter.dirExists(startDir) ) {
707  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
708  LogInfo("DQMGenericClient") << "makeCumulativeDist() : "
709  << "Cannot find sub-directory " << startDir << endl;
710  }
711  return;
712  }
713 
714  ibooker.cd();
715 
716  ME* element_cd = igetter.get(startDir+"/"+cdName);
717 
718  if ( !element_cd ) {
719  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
720  LogInfo("DQMGenericClient") << "makeCumulativeDist() : "
721  << "No such element '" << cdName << "' found\n";
722  }
723  return;
724  }
725 
726  TH1F* cd = element_cd->getTH1F();
727 
728  if ( !cd ) {
729  if ( verbose_ >= 2 || (verbose_ == 1 && !isWildcardUsed_) ) {
730  LogInfo("DQMGenericClient") << "makeCumulativeDist() : "
731  << "Cannot create TH1F from ME\n";
732  }
733  return;
734  }
735 
736  int n_bins = cd->GetNbinsX() + 1;
737 
738  for (int i = 1; i <= n_bins; i++) {
739  cd->SetBinContent(i,cd->GetBinContent(i) + cd->GetBinContent(i-1));
740  }
741 
742  return;
743 }
744 
746 {
747  TH2F * histo = srcME->getTH2F();
748 
749  static int i = 0;
750  i++;
751 
752  // Fit slices projected along Y from bins in X
753  double cont_min = 100; //Minimum number of entries
754  Int_t binx = histo->GetXaxis()->GetNbins();
755 
756  for (int i = 1; i <= binx ; i++) {
757  TString iString(i);
758  TH1 *histoY = histo->ProjectionY(" ", i, i);
759  double cont = histoY->GetEntries();
760 
761  if (cont >= cont_min) {
762  float minfit = histoY->GetMean() - histoY->GetRMS();
763  float maxfit = histoY->GetMean() + histoY->GetRMS();
764 
765  TF1 *fitFcn = new TF1(TString("g")+histo->GetName()+iString,"gaus",minfit,maxfit);
766  double x1,x2;
767  fitFcn->GetRange(x1,x2);
768 
769  histoY->Fit(fitFcn,"QR0","",x1,x2);
770 
771 // histoY->Fit(fitFcn->GetName(),"RME");
772  double *par = fitFcn->GetParameters();
773  double *err = fitFcn->GetParErrors();
774 
775  meanME->setBinContent(i, par[1]);
776  meanME->setBinError(i, err[1]);
777 // meanME->setBinEntries(i, 1.);
778 // meanME->setBinError(i,sqrt(err[1]*err[1]+par[1]*par[1]));
779 
780  sigmaME->setBinContent(i, par[2]);
781  sigmaME->setBinError(i, err[2]);
782 // sigmaME->setBinEntries(i, 1.);
783 // sigmaME->setBinError(i,sqrt(err[2]*err[2]+par[2]*par[2]));
784 
785  if(fitFcn) delete fitFcn;
786  if(histoY) delete histoY;
787  }
788  else {
789  if(histoY) delete histoY;
790  continue;
791  }
792  }
793 }
794 
795 //=================================
796 
798  const TString& _pattern = TString("")) {
799  TString pattern = _pattern;
800  if (!igetter.dirExists(dir)) {
801  LogError("DQMGenericClient") << " DQMGenericClient::findAllSubdirectories ==> Missing folder " << dir << " !!!";
802  return;
803  }
804  if (pattern != "") {
805  if (pattern.Contains(nonPerlWildcard)) pattern.ReplaceAll("*",".*");
806  TPRegexp regexp(pattern);
807  ibooker.cd(dir);
808  vector <string> foundDirs = igetter.getSubdirs();
809  for(vector<string>::const_iterator iDir = foundDirs.begin();
810  iDir != foundDirs.end(); ++iDir) {
811  TString dirName = iDir->substr(iDir->rfind('/') + 1, iDir->length());
812  if (dirName.Contains(regexp))
813  findAllSubdirectories (ibooker, igetter, *iDir, myList);
814  }
815  }
816  //std::cout << "Looking for directory " << dir ;
817  else if (igetter.dirExists(dir)){
818  //std::cout << "... it exists! Inserting it into the list ";
819  myList->insert(dir);
820  //std::cout << "... now list has size " << myList->size() << std::endl;
821  ibooker.cd(dir);
822  findAllSubdirectories (ibooker, igetter, dir, myList, "*");
823  } else {
824  //std::cout << "... DOES NOT EXIST!!! Skip bogus dir" << std::endl;
825 
826  LogInfo ("DQMGenericClient") << "Trying to find sub-directories of " << dir
827  << " failed because " << dir << " does not exist";
828 
829  }
830  return;
831 }
832 
833 
834 void DQMGenericClient::generic_eff (TH1* denom, TH1* numer, MonitorElement* efficiencyHist, const int type) {
835  for (int iBinX = 1; iBinX < denom->GetNbinsX()+1; iBinX++){
836  for (int iBinY = 1; iBinY < denom->GetNbinsY()+1; iBinY++){
837  for (int iBinZ = 1; iBinZ < denom->GetNbinsZ()+1; iBinZ++){
838 
839  int globalBinNum = denom->GetBin(iBinX, iBinY, iBinZ);
840 
841  float numerVal = numer->GetBinContent(globalBinNum);
842  float denomVal = denom->GetBinContent(globalBinNum);
843 
844  float effVal = 0;
845 
846  // fake eff is in use
847  if (type == 2 ) {
848  effVal = denomVal ? (1 - numerVal / denomVal) : 0;
849  } else {
850  effVal = denomVal ? numerVal / denomVal : 0;
851  }
852 
853  float errVal = (denomVal && (effVal <=1)) ? sqrt(effVal*(1-effVal)/denomVal) : 0;
854 
855  LogDebug ("DQMGenericClient") << "(iBinX, iBinY, iBinZ) = "
856  << iBinX << ", "
857  << iBinY << ", "
858  << iBinZ << "), global bin = " << globalBinNum
859  << "eff = " << numerVal << " / " << denomVal
860  << " = " << effVal
861  << " ... setting the error for that bin ... " << endl
862  << endl;
863 
864 
865  efficiencyHist->setBinContent(globalBinNum, effVal);
866  efficiencyHist->setBinError(globalBinNum, errVal);
867  efficiencyHist->setEfficiencyFlag();
868  }
869  }
870  }
871 
872  //efficiencyHist->setMinimum(0.0);
873  //efficiencyHist->setMaximum(1.0);
874 }
875 
876 
877 /* vim:set ts=2 sts=2 sw=2 expandtab: */
#define LogDebug(id)
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override
void setBinContent(int binx, double content)
set content of bin (1-D)
void getFittedSigmaWithError(MonitorElement *)
Fill the ME with the sigma value (with error) of the gaussian fit in each slice.
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
MonitorElement * bookProfile(Args &&...args)
Definition: DQMStore.h:155
vector< string > vstring
Definition: ExoticaDQM.cc:75
void cd(void)
Definition: DQMStore.cc:266
MonitorElement * get(const std::string &path)
Definition: DQMStore.cc:291
TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]")
uint16_t size_type
Definition: ME.h:11
T sqrt(T t)
Definition: SSEVec.h:48
void normalizeToEntries(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const std::string &startDir, const std::string &histName, const std::string &normHistName)
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:113
int j
Definition: DBlmapReader.cc:9
void computeResolution(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const std::string &startDir, const std::string &fitMEPrefix, const std::string &fitMETitlePrefix, const std::string &srcMEName)
void setBinError(int binx, double error)
set uncertainty on content of bin (1-D)
bool dirExists(const std::string &path)
Definition: DQMStore.cc:307
void findAllSubdirectories(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, std::string dir, std::set< std::string > *myList, const TString &pattern)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:274
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:131
void getFittedMeanWithError(MonitorElement *)
Fill the ME with the mean value (with error) of the gaussian fit in each slice.
int cont
MonitorElement ME
std::vector< std::string > getSubdirs(void)
Definition: DQMStore.cc:295
TPRegexp nonPerlWildcard("\\w\\*|^\\*")
void computeEfficiency(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const std::string &startDir, const std::string &efficMEName, const std::string &efficMETitle, const std::string &recoMEName, const std::string &simMEName, const int type=1, const bool makeProfile=false)
void makeCumulativeDist(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const std::string &startDir, const std::string &cdName)
void setEfficiencyFlag(void)
DQMGenericClient(const edm::ParameterSet &pset)
dbl *** dir
Definition: mlp_gen.cc:35
TH2F * getTH2F(void) const
MonitorElement * book3D(Args &&...args)
Definition: DQMStore.h:149
void limitedFit(MonitorElement *srcME, MonitorElement *meanME, MonitorElement *sigmaME)
void generic_eff(TH1 *denom, TH1 *numer, MonitorElement *efficiencyHist, const int type=1)