CMS 3D CMS Logo

HistoAnalyzer.h
Go to the documentation of this file.
1 #ifndef UtilAlgos_HistoAnalyzer_h
2 #define UtilAlgos_HistoAnalyzer_h
3 
20 
21 template <typename C>
22 class HistoAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
23 public:
27  ~HistoAnalyzer() override;
28 
29 protected:
31  void analyze(const edm::Event&, const edm::EventSetup&) override;
32 
33 private:
41  std::vector<ExpressionHisto<typename C::value_type>*> vhistograms;
42 };
43 
44 template <typename C>
46  : srcToken_(consumes<C>(par.template getParameter<edm::InputTag>("src"))),
47  usingWeights_(par.exists("weights")),
48  weightsToken_(
49  mayConsume<double>(par.template getUntrackedParameter<edm::InputTag>("weights", edm::InputTag("fake")))) {
50  usesResource(TFileService::kSharedResource);
52  std::vector<edm::ParameterSet> histograms = par.template getParameter<std::vector<edm::ParameterSet> >("histograms");
53  std::vector<edm::ParameterSet>::const_iterator it = histograms.begin();
54  std::vector<edm::ParameterSet>::const_iterator end = histograms.end();
55 
56  // create the histograms from the given parameter sets
57  for (; it != end; ++it) {
59  hist->initialize(fs->tFileDirectory());
60  vhistograms.push_back(hist);
61  }
62 }
63 
64 template <typename C>
66  // delete all histograms and clear the vector of pointers
67  typename std::vector<ExpressionHisto<typename C::value_type>*>::iterator it = vhistograms.begin();
68  typename std::vector<ExpressionHisto<typename C::value_type>*>::iterator end = vhistograms.end();
69  for (; it != end; ++it) {
70  (*it)->~ExpressionHisto<typename C::value_type>();
71  }
72  vhistograms.clear();
73 }
74 
75 template <typename C>
77  edm::Handle<C> coll;
78  iEvent.getByToken(srcToken_, coll);
79  double weight = 1.0;
80  if (usingWeights_) {
81  edm::Handle<double> weightColl;
82  iEvent.getByToken(weightsToken_, weightColl);
83  weight = *weightColl;
84  }
85 
86  typename std::vector<ExpressionHisto<typename C::value_type>*>::iterator it = vhistograms.begin();
87  typename std::vector<ExpressionHisto<typename C::value_type>*>::iterator end = vhistograms.end();
88 
89  for (; it != end; ++it) {
90  uint32_t i = 0;
91  for (typename C::const_iterator elem = coll->begin(); elem != coll->end(); ++elem, ++i) {
92  if (!(*it)->fill(*elem, weight, i)) {
93  break;
94  }
95  }
96  }
97 }
98 
99 #endif
static const std::string kSharedResource
Definition: TFileService.h:76
edm::EDGetTokenT< C > srcToken_
label of the collection to be read in
Definition: HistoAnalyzer.h:35
Definition: weight.py:1
std::vector< ExpressionHisto< typename C::value_type > * > vhistograms
vector of the histograms
Definition: HistoAnalyzer.h:41
int iEvent
Definition: GenABIO.cc:224
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
~HistoAnalyzer() override
destructor
Definition: HistoAnalyzer.h:65
HLT enums.
edm::EDGetTokenT< double > weightsToken_
label of the weight collection (can be null for weights = 1)
Definition: HistoAnalyzer.h:39
void analyze(const edm::Event &, const edm::EventSetup &) override
process an event
Definition: HistoAnalyzer.h:76
HistoAnalyzer(const edm::ParameterSet &)
constructor from parameter set
Definition: HistoAnalyzer.h:45
bool usingWeights_
Do we weight events?
Definition: HistoAnalyzer.h:37