ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/plugins/DataNTupler.h
Revision: 1.3
Committed: Wed Mar 10 14:05:01 2010 UTC (15 years, 2 months ago) by gpetrucc
Content type: text/plain
Branch: MAIN
CVS Tags: V03-00-00, HEAD
Changes since 1.2: +2 -2 lines
Log Message:
Migration to 3_5_X (probably works in 3_4_X too)

File Contents

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