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