1 |
< |
// -*- C++ -*- |
2 |
< |
// |
3 |
< |
// Package: DataNTupler |
4 |
< |
// Class: DataNTupler |
5 |
< |
// |
6 |
< |
/**\class DataNTupler DataNTupler.cc Gio/DataNTupler/src/DataNTupler.cc |
1 |
> |
#include "UserCode/GPetrucc/plugins/DataNTupler.h" |
2 |
|
|
8 |
– |
Description: <one line class summary> |
9 |
– |
|
10 |
– |
Implementation: |
11 |
– |
<Notes on implementation> |
12 |
– |
*/ |
13 |
– |
// |
14 |
– |
// Original Author: Sep 15 09:45 |
15 |
– |
// Created: Mon Sep 15 09:49:08 CEST 2008 |
16 |
– |
// $Id$ |
17 |
– |
// |
18 |
– |
// |
19 |
– |
|
20 |
– |
|
21 |
– |
// system include files |
22 |
– |
#include <memory> |
23 |
– |
#include <map> |
24 |
– |
#include <algorithm> |
25 |
– |
|
26 |
– |
// user include files |
27 |
– |
#include "FWCore/Framework/interface/Frameworkfwd.h" |
28 |
– |
#include "FWCore/Framework/interface/EDFilter.h" |
29 |
– |
|
30 |
– |
#include "FWCore/Framework/interface/Event.h" |
3 |
|
#include "FWCore/Framework/interface/MakerMacros.h" |
4 |
|
|
33 |
– |
#include "FWCore/ParameterSet/interface/ParameterSet.h" |
34 |
– |
|
35 |
– |
#include "FWCore/ServiceRegistry/interface/Service.h" |
36 |
– |
#include "PhysicsTools/UtilAlgos/interface/TFileService.h" |
37 |
– |
#include "PhysicsTools/Utilities/interface/StringCutObjectSelector.h" |
38 |
– |
#include "PhysicsTools/Utilities/interface/StringObjectFunction.h" |
39 |
– |
|
40 |
– |
#include "DataFormats/Common/interface/View.h" |
41 |
– |
|
5 |
|
#include "DataFormats/PatCandidates/interface/Muon.h" |
6 |
|
#include "DataFormats/PatCandidates/interface/GenericParticle.h" |
7 |
|
#include "DataFormats/HepMCCandidate/interface/GenParticle.h" |
8 |
|
|
46 |
– |
#include <TTree.h> |
47 |
– |
|
48 |
– |
// |
49 |
– |
// class decleration |
50 |
– |
// |
51 |
– |
|
52 |
– |
template<class T> |
53 |
– |
class DataNTupler : public edm::EDFilter { |
54 |
– |
public: |
55 |
– |
explicit DataNTupler(const edm::ParameterSet&); |
56 |
– |
~DataNTupler(); |
57 |
– |
|
58 |
– |
|
59 |
– |
private: |
60 |
– |
virtual void beginJob(const edm::EventSetup&) ; |
61 |
– |
virtual bool filter(edm::Event&, const edm::EventSetup&); |
62 |
– |
virtual void endJob() ; |
63 |
– |
|
64 |
– |
// ----------member data --------------------------- |
65 |
– |
typedef StringCutObjectSelector<T> Selector; |
66 |
– |
typedef StringObjectFunction<T> Function; |
67 |
– |
edm::InputTag src_; |
68 |
– |
Selector cut_; |
69 |
– |
Function sortBy_; |
70 |
– |
|
71 |
– |
TTree *tree_; |
72 |
– |
int number_, max_; |
73 |
– |
|
74 |
– |
|
75 |
– |
struct BaseVar { |
76 |
– |
virtual ~BaseVar() {} |
77 |
– |
virtual void init(const edm::Event &e) {} |
78 |
– |
virtual void branch(const std::string &name, TTree *tree, const char *numberVar, size_t max) = 0; |
79 |
– |
virtual void fill(const edm::Ptr<T> &t, size_t idx) = 0; |
80 |
– |
}; |
81 |
– |
|
82 |
– |
template<typename T2> |
83 |
– |
struct StringFunctionVar : public BaseVar { |
84 |
– |
StringFunctionVar(const std::string &func) : func_(func) {} |
85 |
– |
void branch(const std::string &name, TTree *tree, const char *numberVar, size_t max) { |
86 |
– |
char buff[255]; |
87 |
– |
sprintf(buff,"%s[%s]/%s", name.c_str(), numberVar, (typeid(T2) == typeid(int) ? "I" : "F")); |
88 |
– |
data_.resize(max+1); |
89 |
– |
tree->Branch(name.c_str(), &data_[0], buff); |
90 |
– |
} |
91 |
– |
void fill(const edm::Ptr<T> &t, size_t idx) { |
92 |
– |
data_[idx] = static_cast<T2>(func_(*t)); |
93 |
– |
} |
94 |
– |
|
95 |
– |
private: |
96 |
– |
Function func_; |
97 |
– |
std::vector<T2> data_; |
98 |
– |
}; |
99 |
– |
|
100 |
– |
struct StringCutVar : public BaseVar { |
101 |
– |
StringCutVar(const std::string &cut) : cut_(cut) {} |
102 |
– |
void branch(const std::string &name, TTree *tree, const char *numberVar, size_t max) { |
103 |
– |
char buff[255]; |
104 |
– |
sprintf(buff,"%s[%s]/b", name.c_str(), numberVar); |
105 |
– |
data_.resize(max+1); |
106 |
– |
tree->Branch(name.c_str(), &data_[0], buff); |
107 |
– |
} |
108 |
– |
void fill(const edm::Ptr<T> &t, size_t idx) { |
109 |
– |
data_[idx] = cut_(*t) ? 1 : 0; |
110 |
– |
} |
111 |
– |
|
112 |
– |
private: |
113 |
– |
Selector cut_; |
114 |
– |
std::vector<uint8_t> data_; |
115 |
– |
}; |
116 |
– |
|
117 |
– |
|
118 |
– |
template<typename T2> |
119 |
– |
struct ValueMapVar : public BaseVar { |
120 |
– |
ValueMapVar(const edm::InputTag &src) : src_(src) {} |
121 |
– |
void init(const edm::Event &e) { e.getByLabel(src_, map_); } |
122 |
– |
void branch(const std::string &name, TTree *tree, const char *numberVar, size_t max) { |
123 |
– |
char buff[255]; |
124 |
– |
sprintf(buff,"%s[%s]/%s", name.c_str(), numberVar, (typeid(T2) == typeid(int) ? "I" : "F")); |
125 |
– |
data_.resize(max+1); |
126 |
– |
tree->Branch(name.c_str(), &data_[0], buff); |
127 |
– |
} |
128 |
– |
void fill(const edm::Ptr<T> &t, size_t idx) { |
129 |
– |
data_[idx] = (*map_)[t]; |
130 |
– |
} |
131 |
– |
|
132 |
– |
private: |
133 |
– |
edm::InputTag src_; |
134 |
– |
std::vector<T2> data_; |
135 |
– |
edm::Handle<edm::ValueMap<T2> > map_; |
136 |
– |
}; |
137 |
– |
|
138 |
– |
std::map<std::string, BaseVar *> vars_; |
139 |
– |
}; |
140 |
– |
|
141 |
– |
// |
142 |
– |
// constructors and destructor |
143 |
– |
// |
144 |
– |
template<typename T> |
145 |
– |
DataNTupler<T>::DataNTupler(const edm::ParameterSet& iConfig) : |
146 |
– |
src_(iConfig.getParameter<edm::InputTag>("src")), |
147 |
– |
cut_(iConfig.getParameter<std::string>("cut")), |
148 |
– |
sortBy_(iConfig.getParameter<std::string>("sortBy")), |
149 |
– |
max_(iConfig.getParameter<uint32_t>("maxNumber")) |
150 |
– |
{ |
151 |
– |
edm::Service<TFileService> fs; |
152 |
– |
tree_ = fs->make<TTree>( src_.label().c_str(), iConfig.getParameter<std::string>("cut").c_str() ); |
153 |
– |
|
154 |
– |
std::string numberVar = iConfig.existsAs<std::string>("numberVar") ? iConfig.getParameter<std::string>("numberVar") : "n"; |
155 |
– |
tree_->Branch(numberVar.c_str(), &number_, (numberVar+"/I").c_str()); |
156 |
– |
|
157 |
– |
edm::ParameterSet variables = iConfig.getParameter<edm::ParameterSet>("variables"); |
158 |
– |
std::vector<std::string> names = variables.getParameterNamesForType<edm::ParameterSet>(); |
159 |
– |
for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) { |
160 |
– |
edm::ParameterSet var = variables.getParameter<edm::ParameterSet>(*it); |
161 |
– |
std::string type = var.getParameter<std::string>("type"); |
162 |
– |
if (type == "IntFunction") { |
163 |
– |
vars_[*it] = new StringFunctionVar<int>(var.getParameter<std::string>("expression")); |
164 |
– |
} else if (type == "Function") { |
165 |
– |
vars_[*it] = new StringFunctionVar<float>(var.getParameter<std::string>("expression")); |
166 |
– |
} else if (type == "Cut") { |
167 |
– |
vars_[*it] = new StringCutVar(var.getParameter<std::string>("cut")); |
168 |
– |
} else if (type == "IntValueMap") { |
169 |
– |
vars_[*it] = new ValueMapVar<int>(var.getParameter<edm::InputTag>("src")); |
170 |
– |
} else if (type == "FloatValueMap") { |
171 |
– |
vars_[*it] = new ValueMapVar<float>(var.getParameter<edm::InputTag>("src")); |
172 |
– |
} else throw cms::Exception("Configuration") << "Type " << type << " not known.\n"; |
173 |
– |
} |
174 |
– |
|
175 |
– |
for (typename std::map<std::string, BaseVar *>::iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) { |
176 |
– |
it->second->branch(it->first, tree_, numberVar.c_str(), max_); |
177 |
– |
} |
178 |
– |
} |
179 |
– |
|
180 |
– |
template<typename T> |
181 |
– |
DataNTupler<T>::~DataNTupler() |
182 |
– |
{ |
183 |
– |
for (typename std::map<std::string, BaseVar *>::iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) { |
184 |
– |
delete it->second; |
185 |
– |
} |
186 |
– |
} |
187 |
– |
|
188 |
– |
|
189 |
– |
// ------------ method called to for each event ------------ |
190 |
– |
template<typename T> |
191 |
– |
bool |
192 |
– |
DataNTupler<T>::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) |
193 |
– |
{ |
194 |
– |
using namespace edm; |
195 |
– |
Handle<View<T> > objects; |
196 |
– |
iEvent.getByLabel(src_, objects); |
197 |
– |
|
198 |
– |
// Initialize |
199 |
– |
for (typename std::map<std::string, BaseVar *>::iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) { |
200 |
– |
it->second->init(iEvent); |
201 |
– |
} |
202 |
– |
|
203 |
– |
// Select and sort |
204 |
– |
std::vector<std::pair<float, int> > toBeSorted; |
205 |
– |
for (int i = 0, n = objects->size(); (i < n); ++i) { |
206 |
– |
const T &t = (*objects)[i]; |
207 |
– |
if (cut_(t)) { toBeSorted.push_back(std::pair<float,int>(sortBy_(t), i)); } |
208 |
– |
} |
209 |
– |
std::sort(toBeSorted.begin(), toBeSorted.end()); |
210 |
– |
|
211 |
– |
// Fill variables |
212 |
– |
for (number_ = 0; (number_ <= max_) && (size_t(number_) < toBeSorted.size()); ++number_) { |
213 |
– |
const edm::Ptr<T> &t = objects->ptrAt(toBeSorted[number_].second); |
214 |
– |
for (typename std::map<std::string, BaseVar *>::iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) { |
215 |
– |
it->second->fill(t, number_); |
216 |
– |
} |
217 |
– |
} |
218 |
– |
|
219 |
– |
// Write entry in tree |
220 |
– |
tree_->Fill(); |
221 |
– |
return true; |
222 |
– |
} |
223 |
– |
|
224 |
– |
|
225 |
– |
// ------------ method called once each job just before starting event loop ------------ |
226 |
– |
template<typename T> |
227 |
– |
void |
228 |
– |
DataNTupler<T>::beginJob(const edm::EventSetup&) |
229 |
– |
{ |
230 |
– |
} |
231 |
– |
|
232 |
– |
// ------------ method called once each job just after ending the event loop ------------ |
233 |
– |
template<typename T> |
234 |
– |
void |
235 |
– |
DataNTupler<T>::endJob() { |
236 |
– |
} |
237 |
– |
|
9 |
|
//define this as a plug-in |
10 |
|
typedef DataNTupler<pat::Muon> PATMuonNTupler; |
11 |
|
typedef DataNTupler<pat::GenericParticle> PATGenericParticleNTupler; |