CMS 3D CMS Logo

ConfigurableHisto.h
Go to the documentation of this file.
1 #ifndef ConfigurableAnalysis_ConfigurableHisto_H
2 #define ConfigurableAnalysis_ConfigurableHisto_H
3 
6 
7 #include "TH1.h"
8 #include "TH1F.h"
9 #include "TH2F.h"
10 #include "TProfile.h"
11 #include "THStack.h"
12 
15 
17 public:
19  ConfigurableAxis(const edm::ParameterSet& par) : nBin_(0), Min_(0), Max_(0), Label_("") {
20  Label_ = par.getParameter<std::string>("Label");
21 
22  if (par.exists("nBins")) {
23  nBin_ = par.getParameter<unsigned int>("nBins");
24  Min_ = par.getParameter<double>("Min");
25  Max_ = par.getParameter<double>("Max");
26  } else {
27  if (par.exists("vBins"))
28  vBins_ = par.getParameter<std::vector<double> >("vBins");
29  else {
30  Min_ = par.getParameter<double>("Min");
31  Max_ = par.getParameter<double>("Max");
32  }
33  }
34  }
35  bool variableSize() { return !vBins_.empty(); }
36  unsigned int nBin() {
37  if (variableSize())
38  return vBins_.size() - 1;
39  else
40  return nBin_;
41  }
42  double Min() {
43  if (variableSize())
44  return vBins_.front();
45  else
46  return Min_;
47  }
48  double Max() {
49  if (variableSize())
50  return vBins_.back();
51  else
52  return Max_;
53  }
54  const std::string& Label() { return Label_; }
55  const double* xBins() {
56  if (!vBins_.empty())
57  return &(vBins_.front());
58  else
59  return nullptr;
60  }
61 
62 private:
63  std::vector<double> vBins_;
64  unsigned int nBin_;
65  double Min_;
66  double Max_;
68 };
69 
71 public:
72  enum HType { h1, h2, prof };
74  : type_(t), h_(nullptr), name_(name), conf_(iConfig), x_(nullptr), y_(nullptr), z_(nullptr), w_(nullptr) {}
75 
76  virtual ~ConfigurableHisto() {}
77 
78  virtual ConfigurableHisto* clone() const { return new ConfigurableHisto(*this); }
79 
80  virtual void book(TFileDirectory* dir) {
83  ConfigurableAxis xAxis(xAxisPSet);
84  x_ = edm::Service<VariableHelperService>()->get().variable(xAxisPSet.getParameter<std::string>("var"));
85 
86  std::string yLabel = "";
87  bool yVBin = false;
89  if (conf_.exists("yAxis")) {
91  yAxis = ConfigurableAxis(yAxisPSet);
92  yLabel = yAxis.Label();
93  //at least TH2 or TProfile
94  if (yAxisPSet.exists("var"))
95  y_ = edm::Service<VariableHelperService>()->get().variable(yAxisPSet.getParameter<std::string>("var"));
96  yVBin = yAxis.variableSize();
97  }
98 
99  if (conf_.exists("zAxis")) {
100  throw;
101  }
102 
103  bool xVBin = xAxis.variableSize();
104 
105  if (type() == h1) {
106  //make TH1F
107  if (xVBin)
108  h_ = dir->make<TH1F>(name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.xBins());
109  else
110  h_ = dir->make<TH1F>(name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.Min(), xAxis.Max());
111  } else if (type() == h2) {
112  //make TH2F
113  if (xVBin) {
114  if (yVBin)
115  h_ = dir->make<TH2F>(name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.xBins(), yAxis.nBin(), yAxis.xBins());
116  else
117  h_ = dir->make<TH2F>(
118  name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.xBins(), yAxis.nBin(), yAxis.Min(), yAxis.Max());
119  } else {
120  if (yVBin)
121  h_ = dir->make<TH2F>(
122  name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.Min(), xAxis.Max(), yAxis.nBin(), yAxis.xBins());
123  else
124  h_ = dir->make<TH2F>(name_.c_str(),
125  title.c_str(),
126  xAxis.nBin(),
127  xAxis.Min(),
128  xAxis.Max(),
129  yAxis.nBin(),
130  yAxis.Min(),
131  yAxis.Max());
132  }
133  } else if (type() == prof) {
134  //make TProfile
135  std::string pFopt = "";
136  if (conf_.exists("Option"))
137  pFopt = conf_.getParameter<std::string>("Option");
138 
139  if (xVBin)
140  h_ = dir->make<TProfile>(
141  name_.c_str(), title.c_str(), xAxis.nBin(), xAxis.xBins(), yAxis.Min(), yAxis.Max(), pFopt.c_str());
142  else
143  h_ = dir->make<TProfile>(name_.c_str(),
144  title.c_str(),
145  xAxis.nBin(),
146  xAxis.Min(),
147  xAxis.Max(),
148  yAxis.Min(),
149  yAxis.Max(),
150  pFopt.c_str());
151 
152  } else {
153  edm::LogError("ConfigurableHisto") << "cannot book: " << name_ << "\n" << conf_.dump();
154  throw;
155  }
156 
157  //cosmetics
158  h_->GetXaxis()->SetTitle(xAxis.Label().c_str());
159  h_->SetYTitle(yLabel.c_str());
160 
161  if (conf_.exists("weight")) {
162  w_ = edm::Service<VariableHelperService>()->get().variable(conf_.getParameter<std::string>("weight"));
163  }
164  }
165 
166  virtual void fill(const edm::Event& iEvent) {
167  if (!h_)
168  return;
169 
170  double weight = 1.0;
171  if (w_) {
172  if (w_->compute(iEvent))
173  weight = (*w_)(iEvent);
174  else {
175  edm::LogInfo("ConfigurableHisto") << "could not compute the weight for: " << name_ << " with config:\n"
176  << conf_.dump() << " default to 1.0";
177  }
178  }
179 
180  TProfile* pcast(nullptr);
181  TH2* h2cast(nullptr);
182  switch (type_) {
183  case h1:
184  if (!h_)
185  throw;
186  if (x_->compute(iEvent))
187  h_->Fill((*x_)(iEvent), weight);
188  else {
189  edm::LogInfo("ConfigurableHisto") << "could not fill: " << name_ << " with config:\n" << conf_.dump();
190  }
191  break;
192  case prof:
193  pcast = dynamic_cast<TProfile*>(h_);
194  if (!pcast)
195  throw;
196  if (x_->compute(iEvent) && y_->compute(iEvent))
197  pcast->Fill((*x_)(iEvent), (*y_)(iEvent), weight);
198  else {
199  edm::LogInfo("ConfigurableHisto") << "could not fill: " << name_ << " with config:\n" << conf_.dump();
200  }
201  break;
202  case h2:
203  h2cast = dynamic_cast<TH2*>(h_);
204  if (!h2cast)
205  throw;
206  if (x_->compute(iEvent) && y_->compute(iEvent))
207  h2cast->Fill((*x_)(iEvent), (*y_)(iEvent), weight);
208  else {
209  edm::LogInfo("ConfigurableHisto") << "could not fill: " << name_ << " with config:\n" << conf_.dump();
210  }
211  break;
212  }
213  }
214  const HType& type() { return type_; }
215 
216  void complete() {}
217  TH1* h() { return h_; }
218 
219 protected:
221  type_ = master.type_;
222  h_ = nullptr; //no histogram attached in copy constructor
223  name_ = master.name_;
224  conf_ = master.conf_;
225  x_ = master.x_;
226  y_ = master.y_;
227  z_ = master.z_;
228  w_ = master.w_;
229  }
231  TH1* h_;
234 
239 };
240 
242 public:
244  : ConfigurableHisto(t, name, pset), splitter_(nullptr) {
245  std::string title = pset.getParameter<std::string>("title");
246 
247  //allow for many splitters ...
248  if (pset.exists("splitters")) {
249  //you want more than one splitter
250  std::vector<std::string> splitters = pset.getParameter<std::vector<std::string> >("splitters");
251  for (unsigned int s = 0; s != splitters.size(); ++s) {
252  const CachingVariable* v = edm::Service<VariableHelperService>()->get().variable(splitters[s]);
253  const Splitter* splitter = dynamic_cast<const Splitter*>(v);
254  if (!splitter) {
255  edm::LogError("SplittingConfigurableHisto")
256  << "for: " << name_ << " the splitting variable: " << splitters[s] << " is not a Splitter";
257  continue;
258  }
259 
260  //insert in the map
261  std::vector<ConfigurableHisto*>& insertedHisto = subHistoMap_[splitter];
262  //now configure the histograms
263  unsigned int mSlots = splitter->maxSlots();
264  for (unsigned int i = 0; i != mSlots; ++i) {
265  //--- std::cout<<" slot: "<<i<<std::endl;
266  const std::string& slabel = splitter->shortLabel(i);
267  const std::string& label = splitter->label(i);
268  edm::ParameterSet mPset = pset;
269  edm::Entry e("string", title + " for " + label, true);
270  mPset.insert(true, "title", e);
271  insertedHisto.push_back(new ConfigurableHisto(t, name + slabel, mPset));
272  } //loop on slots
273  } //loop on splitters
274 
275  } //if splitters exists
276  else {
277  //single splitter
278  //get the splitting variable
279  const CachingVariable* v =
280  edm::Service<VariableHelperService>()->get().variable(pset.getParameter<std::string>("splitter"));
281  splitter_ = dynamic_cast<const Splitter*>(v);
282  if (!splitter_) {
283  edm::LogError("SplittingConfigurableHisto")
284  << "for: " << name_ << " the splitting variable: " << v->name() << " is not a Splitter";
285  } else {
286  //configure the splitted plots
287  unsigned int mSlots = splitter_->maxSlots();
288  for (unsigned int i = 0; i != mSlots; i++) {
289  const std::string& slabel = splitter_->shortLabel(i);
290  const std::string& label = splitter_->label(i);
291  edm::ParameterSet mPset = pset;
292  edm::Entry e("string", title + " for " + label, true);
293  mPset.insert(true, "title", e);
294  subHistos_.push_back(new ConfigurableHisto(t, name + slabel, mPset));
295  }
296  }
297  }
298  } //end of ctor
299 
300  void book(TFileDirectory* dir) override {
301  //book the base histogram
303 
304  if (!subHistoMap_.empty()) {
305  SubHistoMap::iterator i = subHistoMap_.begin();
306  SubHistoMap::iterator i_end = subHistoMap_.end();
307  for (; i != i_end; ++i) {
308  for (unsigned int h = 0; h != i->second.size(); ++h) {
309  i->second[h]->book(dir);
310  }
311  //book the THStack
312  std::string sName = name_ + "_" + i->first->name();
313  std::string sTitle = "Stack histogram of " + name_ + " for splitter " + i->first->name();
314  subHistoStacks_[i->first] = dir->make<THStack>(sName.c_str(), sTitle.c_str());
315  }
316  } else {
317  for (unsigned int h = 0; h != subHistos_.size(); h++) {
318  subHistos_[h]->book(dir);
319  }
320  //book a THStack
321  std::string sName = name_ + "_" + splitter_->name();
322  std::string sTitle = "Stack histogram of " + name_ + " for splitter " + splitter_->name();
323  stack_ = dir->make<THStack>(sName.c_str(), sTitle.c_str());
324  }
325  }
326 
327  ConfigurableHisto* clone() const override { return new SplittingConfigurableHisto(*this); }
328 
329  void fill(const edm::Event& e) override {
330  //fill the base histogram
332 
333  if (!subHistoMap_.empty()) {
334  SubHistoMap::iterator i = subHistoMap_.begin();
335  SubHistoMap::iterator i_end = subHistoMap_.end();
336  for (; i != i_end; ++i) {
337  const Splitter* splitter = i->first;
338  if (!splitter)
339  continue;
340  if (!splitter->compute(e))
341  continue;
342  unsigned int slot = (unsigned int)(*splitter)(e);
343  if (slot >= i->second.size()) {
344  edm::LogError("SplittingConfigurableHisto")
345  << "slot index: " << slot << " is bigger than slots size: " << i->second.size()
346  << " from variable value: " << (*splitter)(e);
347  continue;
348  }
349  //fill in the proper slot
350  i->second[slot]->fill(e);
351  }
352  } else {
353  //fill the component histograms
354  if (!splitter_)
355  return;
356  if (!splitter_->compute(e)) {
357  return;
358  }
359  unsigned int slot = (unsigned int)(*splitter_)(e);
360  if (slot >= subHistos_.size()) {
361  edm::LogError("SplittingConfigurableHisto")
362  << "slot index: " << slot << " is bigger than slots size: " << subHistos_.size()
363  << " from variable value: " << (*splitter_)(e);
364  return;
365  }
366  subHistos_[slot]->fill(e);
367  }
368  }
369 
370  void complete() {
371  if (!subHistoMap_.empty()) {
372  //fill up the stacks
373  SubHistoMap::iterator i = subHistoMap_.begin();
374  SubHistoMap::iterator i_end = subHistoMap_.end();
375  for (; i != i_end; ++i) {
376  for (unsigned int h = 0; h != i->second.size(); h++) {
377  // if (i->second[h]->h()->Integral==0) continue;// do not add empty histograms. NO, because it will be tough to merge two THStack
378  subHistoStacks_[i->first]->Add(i->second[h]->h(), i->first->label(h).c_str());
379  }
380  }
381 
382  } else {
383  //fill up the only stack
384  for (unsigned int i = 0; i != subHistos_.size(); i++) {
385  stack_->Add(subHistos_[i]->h(), splitter_->label(i).c_str());
386  }
387  }
388  }
389 
390 private:
392  splitter_ = master.splitter_;
393  if (!master.subHistoMap_.empty()) {
394  SubHistoMap::const_iterator i = master.subHistoMap_.begin();
395  SubHistoMap::const_iterator i_end = master.subHistoMap_.end();
396  for (; i != i_end; ++i) {
397  const std::vector<ConfigurableHisto*>& masterHistos = i->second;
398  std::vector<ConfigurableHisto*>& clonedHistos = subHistoMap_[i->first];
399  for (unsigned int i = 0; i != masterHistos.size(); i++) {
400  clonedHistos.push_back(masterHistos[i]->clone());
401  }
402  }
403  } else {
404  for (unsigned int i = 0; i != master.subHistos_.size(); i++) {
405  subHistos_.push_back(master.subHistos_[i]->clone());
406  }
407  }
408  }
409 
410  typedef std::map<const Splitter*, std::vector<ConfigurableHisto*> > SubHistoMap;
411  typedef std::map<const Splitter*, THStack*> SubHistoStacks;
414 
416  std::vector<ConfigurableHisto*> subHistos_;
417  // do you want to have a stack already made from the splitted histos?
418  THStack* stack_;
419 };
420 
421 #endif
splitter
Definition: splitter.py:1
Splitter::shortLabel
const std::string shortLabel(unsigned int i) const
Definition: CachingVariable.h:195
runGCPTkAlMap.title
string title
Definition: runGCPTkAlMap.py:94
ConfigurableAxis::variableSize
bool variableSize()
Definition: ConfigurableHisto.h:35
mps_fire.i
i
Definition: mps_fire.py:428
ConfigurableHisto::h2
Definition: ConfigurableHisto.h:72
SplittingConfigurableHisto::subHistoMap_
SubHistoMap subHistoMap_
Definition: ConfigurableHisto.h:413
ConfigurableHisto::type_
HType type_
Definition: ConfigurableHisto.h:230
CachingVariable.h
SplittingConfigurableHisto::clone
ConfigurableHisto * clone() const override
Definition: ConfigurableHisto.h:327
ConfigurableAxis::nBin
unsigned int nBin()
Definition: ConfigurableHisto.h:36
ConfigurableHisto::ConfigurableHisto
ConfigurableHisto(HType t, std::string name, edm::ParameterSet &iConfig)
Definition: ConfigurableHisto.h:73
SplittingConfigurableHisto::book
void book(TFileDirectory *dir) override
Definition: ConfigurableHisto.h:300
ConfigurableHisto::y_
const CachingVariable * y_
Definition: ConfigurableHisto.h:236
ConfigurableAxis::ConfigurableAxis
ConfigurableAxis(const edm::ParameterSet &par)
Definition: ConfigurableHisto.h:19
ConfigurableAxis::nBin_
unsigned int nBin_
Definition: ConfigurableHisto.h:64
ConfigurableHisto::HType
HType
Definition: ConfigurableHisto.h:72
CachingVariable
Definition: CachingVariable.h:40
TFileDirectory
Definition: TFileDirectory.h:24
ConfigurableAxis::Min
double Min()
Definition: ConfigurableHisto.h:42
SplittingConfigurableHisto::SplittingConfigurableHisto
SplittingConfigurableHisto(const SplittingConfigurableHisto &master)
Definition: ConfigurableHisto.h:391
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
findQualityFiles.v
v
Definition: findQualityFiles.py:179
ConfigurableHisto::prof
Definition: ConfigurableHisto.h:72
ConfigurableHisto::h1
Definition: ConfigurableHisto.h:72
SplittingConfigurableHisto::stack_
THStack * stack_
Definition: ConfigurableHisto.h:418
TFileDirectory.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
SplittingConfigurableHisto::SubHistoStacks
std::map< const Splitter *, THStack * > SubHistoStacks
Definition: ConfigurableHisto.h:411
ConfigurableAxis::Label
const std::string & Label()
Definition: ConfigurableHisto.h:54
edm::ParameterSet::dump
std::string dump(unsigned int indent=0) const
Definition: ParameterSet.cc:832
edm::ParameterSet::insert
void insert(bool ok_to_replace, char const *, Entry const &)
Definition: ParameterSet.cc:380
h
confdb.splitter
def splitter(iterator, n)
Definition: confdb.py:13
ConfigurableAxis::ConfigurableAxis
ConfigurableAxis()
Definition: ConfigurableHisto.h:18
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ConfigurableAxis::vBins_
std::vector< double > vBins_
Definition: ConfigurableHisto.h:63
ConfigurableHisto::clone
virtual ConfigurableHisto * clone() const
Definition: ConfigurableHisto.h:78
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
ConfigurableHisto::w_
const CachingVariable * w_
Definition: ConfigurableHisto.h:238
ConfigurableAxis::xBins
const double * xBins()
Definition: ConfigurableHisto.h:55
ConfigurableHisto
Definition: ConfigurableHisto.h:70
edm::ParameterSet
Definition: ParameterSet.h:47
ConfigurableHisto::fill
virtual void fill(const edm::Event &iEvent)
Definition: ConfigurableHisto.h:166
Splitter
Definition: CachingVariable.h:182
ConfigurableHisto::h
TH1 * h()
Definition: ConfigurableHisto.h:217
HLT_FULL_cff.yAxis
yAxis
Definition: HLT_FULL_cff.py:46144
ConfigurableHisto::conf_
edm::ParameterSet conf_
Definition: ConfigurableHisto.h:233
edm::Service
Definition: Service.h:30
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
ConfigurableAxis::Label_
std::string Label_
Definition: ConfigurableHisto.h:67
ConfigurableAxis::Min_
double Min_
Definition: ConfigurableHisto.h:65
ConfigurableHisto::h_
TH1 * h_
Definition: ConfigurableHisto.h:231
SplittingConfigurableHisto::subHistoStacks_
SubHistoStacks subHistoStacks_
Definition: ConfigurableHisto.h:412
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
ConfigurableAxis::Max
double Max()
Definition: ConfigurableHisto.h:48
SplittingConfigurableHisto::complete
void complete()
Definition: ConfigurableHisto.h:370
ConfigurableHisto::z_
const CachingVariable * z_
Definition: ConfigurableHisto.h:237
ConfigurableHisto::~ConfigurableHisto
virtual ~ConfigurableHisto()
Definition: ConfigurableHisto.h:76
SplittingConfigurableHisto::SplittingConfigurableHisto
SplittingConfigurableHisto(HType t, std::string name, edm::ParameterSet &pset)
Definition: ConfigurableHisto.h:243
ConfigurableAxis::Max_
double Max_
Definition: ConfigurableHisto.h:66
ConfigurableHisto::x_
const CachingVariable * x_
Definition: ConfigurableHisto.h:235
SplittingConfigurableHisto::subHistos_
std::vector< ConfigurableHisto * > subHistos_
Definition: ConfigurableHisto.h:416
ConfigurableHisto::ConfigurableHisto
ConfigurableHisto(const ConfigurableHisto &master)
Definition: ConfigurableHisto.h:220
SplittingConfigurableHisto
Definition: ConfigurableHisto.h:241
ConfigurableHisto::complete
void complete()
Definition: ConfigurableHisto.h:216
HLT_FULL_cff.xAxis
xAxis
Definition: HLT_FULL_cff.py:46142
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ConfigurableAxis
Definition: ConfigurableHisto.h:16
VariableHelper.h
CachingVariable::compute
bool compute(const edm::Event &iEvent) const
Definition: CachingVariable.h:61
CachingVariable::name
const std::string & name() const
Definition: CachingVariable.h:66
ConfigurableHisto::type
const HType & type()
Definition: ConfigurableHisto.h:214
SplittingConfigurableHisto::fill
void fill(const edm::Event &e) override
Definition: ConfigurableHisto.h:329
ParameterSet.h
SplittingConfigurableHisto::SubHistoMap
std::map< const Splitter *, std::vector< ConfigurableHisto * > > SubHistoMap
Definition: ConfigurableHisto.h:410
volumeBasedMagneticField_160812_cfi.master
master
Definition: volumeBasedMagneticField_160812_cfi.py:60
edm::Event
Definition: Event.h:73
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
ConfigurableHisto::book
virtual void book(TFileDirectory *dir)
Definition: ConfigurableHisto.h:80
Splitter::label
const std::string & label(unsigned int i) const
Definition: CachingVariable.h:205
edm::Entry
Definition: Entry.h:40
Splitter::maxSlots
virtual unsigned int maxSlots() const
Definition: CachingVariable.h:193
label
const char * label
Definition: PFTauDecayModeTools.cc:11
weight
Definition: weight.py:1
SplittingConfigurableHisto::splitter_
const Splitter * splitter_
Definition: ConfigurableHisto.h:415
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
ConfigurableHisto::name_
std::string name_
Definition: ConfigurableHisto.h:232
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37