CMS 3D CMS Logo

CommonAnalyzer.cc
Go to the documentation of this file.
1 #include <iostream>
3 #include "TFile.h"
4 #include "TDirectory.h"
5 #include "TObject.h"
6 #include "TH1F.h"
7 #include "TNamed.h"
8 #include "TList.h"
9 #include "TKey.h"
10 #include "TClass.h"
11 #include <string>
12 #include <vector>
13 
14 CommonAnalyzer::CommonAnalyzer(TFile* file, const char* run, const char* mod, const char* path, const char* prefix)
15  : _file(file), _runnumber(run), _module(mod), _path(path), _prefix(prefix) {}
16 
18  : _file(dtca._file), _runnumber(dtca._runnumber), _module(dtca._module), _path(dtca._path), _prefix(dtca._prefix) {}
19 
21  if (this != &dtca) {
22  _file = dtca._file;
23  _runnumber = dtca._runnumber;
24  _module = dtca._module;
25  _path = dtca._path;
26  _prefix = dtca._prefix;
27  }
28  return *this;
29 }
30 
33 void CommonAnalyzer::setModule(const char* mod) { _module = mod; }
34 void CommonAnalyzer::setPath(const char* path) { _path = path; }
36 
38 const std::string& CommonAnalyzer::getModule() const { return _module; }
39 const std::string& CommonAnalyzer::getPath() const { return _path; }
40 const std::string& CommonAnalyzer::getPrefix() const { return _prefix; }
41 
42 TObject* CommonAnalyzer::getObject(const char* name) const {
43  TObject* obj = nullptr;
44 
46  if (_file) {
47  bool ok = _file->cd(fullpath.c_str());
48  if (ok && gDirectory) {
49  obj = gDirectory->Get(name);
50  }
51  }
52  return obj;
53 }
54 
55 TNamed* CommonAnalyzer::getObjectWithSuffix(const char* name, const char* suffix) const {
56  TNamed* obj = (TNamed*)getObject(name);
57 
58  if (obj) {
59  if (!strstr(obj->GetTitle(), "run")) {
60  char htitle[300];
61  sprintf(htitle, "%s %s run %s", obj->GetTitle(), suffix, _runnumber.c_str());
62  obj->SetTitle(htitle);
63  }
64  }
65  return obj;
66 }
67 
68 const std::vector<unsigned int> CommonAnalyzer::getRunList() const { return getList("run"); }
69 
70 const std::vector<unsigned int> CommonAnalyzer::getFillList() const { return getList("fill"); }
71 
72 const std::vector<unsigned int> CommonAnalyzer::getList(const char* what) const {
73  std::vector<unsigned int> runlist;
74  char searchstring[100];
75  char decodestring[100];
76  sprintf(searchstring, "%s_", what);
77  sprintf(decodestring, "%s_%%u", what);
78 
80  if (_file) {
81  bool ok = _file->cd(fullpath.c_str());
82  if (ok && gDirectory) {
83  TList* keys = gDirectory->GetListOfKeys();
84  TListIter it(keys);
85  TKey* key = nullptr;
86  while ((key = (TKey*)it.Next())) {
87  std::cout << key->GetName() << std::endl;
88  TClass cl(key->GetClassName());
89  if (cl.InheritsFrom("TDirectory") && strstr(key->GetName(), searchstring) != nullptr) {
90  unsigned int run;
91  sscanf(key->GetName(), decodestring, &run);
92  runlist.push_back(run);
93  }
94  }
95  }
96  }
97  // sort(runlist);
98  return runlist;
99 }
100 
101 TH1F* CommonAnalyzer::getBinomialRatio(const CommonAnalyzer& denom, const char* name, const int rebin) const {
102  TH1F* den = (TH1F*)denom.getObject(name);
103  TH1F* num = (TH1F*)getObject(name);
104  TH1F* ratio = nullptr;
105 
106  if (den != nullptr && num != nullptr) {
107  TH1F* denreb = den;
108  TH1F* numreb = num;
109  if (rebin > 0) {
110  denreb = (TH1F*)den->Rebin(rebin, "denrebinned");
111  numreb = (TH1F*)num->Rebin(rebin, "numrebinned");
112  }
113 
114  ratio = new TH1F(*numreb);
115  ratio->SetDirectory(nullptr);
116  ratio->Reset();
117  ratio->Sumw2();
118  ratio->Divide(numreb, denreb, 1, 1, "B");
119  delete denreb;
120  delete numreb;
121  }
122 
123  return ratio;
124 }
TH1F * getBinomialRatio(const CommonAnalyzer &denom, const char *name, const int rebin=-1) const
const std::string & getRunNumber() const
TNamed * getObjectWithSuffix(const char *name, const char *suffix="") const
std::string _prefix
void setRunNumber(const char *run)
TObject * getObject(const char *name) const
std::string _runnumber
const std::string & getPath() const
std::string _module
void setModule(const char *mod)
const std::string & getPrefix() const
CommonAnalyzer & operator=(const CommonAnalyzer &dtca)
std::string _path
const std::vector< unsigned int > getList(const char *what) const
void setPath(const char *path)
CommonAnalyzer(TFile *file, const char *run, const char *mod, const char *path="", const char *prefix="")
const std::vector< unsigned int > getRunList() const
const std::string & getModule() const
void setFile(TFile *file)
void setPrefix(const char *prefix)
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
const std::vector< unsigned int > getFillList() const