CMS 3D CMS Logo

ExpressionHisto.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_ExpressionHisto_h
2 #define CommonTools_Utils_ExpressionHisto_h
3 // -*- C++ -*-
4 //
5 // Package: UtilAlgos
6 // Class : ExpressionHisto
7 //
16 //
17 // Original Author: Benedikt HEGNER
18 // Created: Fri Jun 1 14:35:22 CEST 2007
19 // $Id: ExpressionHisto.h,v 1.2 2009/07/09 10:52:01 gpetrucc Exp $
20 //
21 
22 // system include files
23 
24 // user include files
28 
29 #include "TFile.h"
30 #include "TH1F.h"
31 #include "TH1.h"
32 
33 template <typename T>
35 public:
36  ExpressionHisto(const edm::ParameterSet& iConfig);
38 
45  bool fill(const T& element, double weight = 1.0, uint32_t i = 0);
46 
47 private:
48  double min, max;
49  int nbins;
51  uint32_t nhistos;
53  TH1F** hist;
55 };
56 
57 template <typename T>
59  : min(iConfig.template getUntrackedParameter<double>("min")),
60  max(iConfig.template getUntrackedParameter<double>("max")),
61  nbins(iConfig.template getUntrackedParameter<int>("nbins")),
62  name(iConfig.template getUntrackedParameter<std::string>("name")),
63  description(iConfig.template getUntrackedParameter<std::string>("description")),
64  function(iConfig.template getUntrackedParameter<std::string>("plotquantity"),
65  iConfig.template getUntrackedParameter<bool>("lazyParsing", false)) {
66  int32_t itemsToPlot = iConfig.template getUntrackedParameter<int32_t>("itemsToPlot", -1);
67  if (itemsToPlot <= 0) {
68  nhistos = 1;
69  separatePlots = false;
70  } else {
71  nhistos = itemsToPlot;
72  separatePlots = true;
73  }
74 }
75 
76 template <typename T>
78 
79 template <typename T>
81  hist = new TH1F*[nhistos];
82  char buff[1024], baff[1024];
83  if (separatePlots) {
84  for (uint32_t i = 0; i < nhistos; i++) {
85  if (strstr(name.c_str(), "%d") != nullptr) {
86  snprintf(buff, 1024, name.c_str(), i + 1);
87  } else {
88  snprintf(buff, 1024, "%s [#%d]", name.c_str(), i + 1);
89  }
90  if (strstr(description.c_str(), "%d") != nullptr) {
91  snprintf(baff, 1024, description.c_str(), i + 1);
92  } else {
93  snprintf(baff, 1024, "%s [#%d]", description.c_str(), i + 1);
94  }
95  hist[i] = fs.make<TH1F>(buff, baff, nbins, min, max);
96  }
97  } else {
98  hist[0] = fs.make<TH1F>(name.c_str(), description.c_str(), nbins, min, max);
99  }
100 }
101 
102 template <typename T>
103 bool ExpressionHisto<T>::fill(const T& element, double weight, uint32_t i) {
104  if (!separatePlots)
105  hist[0]->Fill(function(element), weight);
106  else if (i < nhistos)
107  hist[i]->Fill(function(element), weight);
108  else
109  return false;
110  return true;
111 }
112 
113 #endif
Definition: weight.py:1
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
ExpressionHisto(const edm::ParameterSet &iConfig)
std::string description
std::string name
__shared__ Hist hist
bool fill(const T &element, double weight=1.0, uint32_t i=0)
void initialize(TFileDirectory &fs)
long double T