CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StringBasedNTupler.h
Go to the documentation of this file.
1 #ifndef StringBasedNTupler_NTupler_H
2 #define StringBasedNTupler_NTupler_H
3 
9 
11 #include "TTree.h"
12 #include "TBranch.h"
13 #include "TFile.h"
14 
16 
23 
25 
28 
29 #include <memory>
30 #include <string>
31 #include <sstream>
32 
38 
39 // LHE Event
41 
42 class TreeBranch {
43 public:
44  TreeBranch() : class_(""), expr_(""), order_(""), selection_(""), maxIndexName_(""), branchAlias_("") {}
47  : class_(C), src_(S), expr_(E), order_(O), selection_(SE), maxIndexName_(Mi), branchAlias_(Ba) {
48  branchTitle_ = E + " calculated on " + C + " object from " + S.encode();
49  if (!O.empty())
50  branchTitle_ += " ordered according to " + O;
51  if (!SE.empty())
52  branchTitle_ += " selecting on " + SE;
53  edm::LogInfo("TreeBranch") << "the branch with alias: " << branchAlias_ << " corresponds to: " << branchTitle_;
54  }
55 
56  const std::string& className() const { return class_; }
57  const edm::InputTag& src() const { return src_; }
58  const std::string& expr() const { return expr_; }
59  const std::string& order() const { return order_; }
60  const std::string& selection() const { return selection_; }
61  const std::string& maxIndexName() const { return maxIndexName_; }
62  const std::string branchName() const {
64  std::replace(name.begin(), name.end(), '_', '0');
65  return std::string(name);
66  }
67  const std::string& branchAlias() const { return branchAlias_; }
68  const std::string& branchTitle() const { return branchTitle_; }
69  typedef std::unique_ptr<std::vector<float>> value;
70  value branch(const edm::Event& iEvent);
71 
72  std::vector<float>** dataHolderPtrAdress() { return &dataHolderPtr_; }
73  std::vector<float>* dataHolderPtr() { return dataHolderPtr_; }
74  void assignDataHolderPtr(std::vector<float>* data) { dataHolderPtr_ = data; }
75 
76 private:
85 
86  std::vector<float>* dataHolderPtr_;
87 };
88 
89 template <typename Object>
91 public:
94 
96  const float defaultValue = 0.;
97  // grab the object
99  iEvent.getByLabel(B.src(), oH);
100  //empty vector if product not found
101  if (oH.failedToGet()) {
102  edm::LogError("StringBranchHelper") << "cannot open: " << B.src();
103  value_ = std::make_unique<std::vector<float>>(0);
104  } else {
105  //parser for the object expression
107  //allocate enough memory for the data holder
108  value_ = std::make_unique<std::vector<float>>(1);
109  try {
110  (*value_)[0] = (expr)(*oH);
111  } catch (...) {
112  LogDebug("StringLeaveHelper") << "could not evaluate expression: " << B.expr()
113  << " on class: " << B.className();
114  (*value_)[0] = defaultValue;
115  }
116  }
117  }
118 
119 private:
121 };
122 
123 template <typename Object, typename Collection = std::vector<Object>>
125 public:
128 
130  const float defaultValue = 0.;
131 
132  // grab the collection
134  iEvent.getByLabel(B.src(), oH);
135 
136  //empty vector if product not found
137  if (oH.failedToGet()) {
138  if (!(iEvent.isRealData() && B.className() == "reco::GenParticle")) { //don't output genparticle error in data
139  edm::LogError("StringBranchHelper") << "cannot open: " << B.src() << " " << B.className();
140  }
141  value_ = std::make_unique<std::vector<float>>();
142  } else {
143  //parser for the object expression
145  //allocate enough memory for the data holder
146  value_ = std::make_unique<std::vector<float>>();
147  value_->reserve(oH->size());
148 
150  if (!B.selection().empty()) {
151  //std::cout<<"trying to get to a selection"<<std::endl;
152  selection = new StringCutObjectSelector<Object>(B.selection());
153  //std::cout<<"got the objet"<<std::endl;
154  }
155  uint i_end = oH->size();
156  //sort things first if requested
157  if (!B.order().empty()) {
159  // allocate a vector of pointers (we are using view) to be sorted
160  std::vector<const Object*> copyToSort(oH->size());
161  for (uint i = 0; i != i_end; ++i)
162  copyToSort[i] = &(*oH)[i];
163  std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(&order));
164  //then loop and fill
165  for (uint i = 0; i != i_end; ++i) {
166  //try and catch is necessary because ...
167  try {
168  if (selection && !((*selection)(*(copyToSort)[i])))
169  continue;
170  value_->push_back((expr)(*(copyToSort)[i]));
171  } catch (...) {
172  LogDebug("StringBranchHelper")
173  << "with sorting. could not evaluate expression: " << B.expr() << " on class: " << B.className();
174  value_->push_back(defaultValue); //push a default value to not change the indexing
175  }
176  }
177  } else {
178  //actually fill the vector of values
179  for (uint i = 0; i != i_end; ++i) {
180  //try and catch is necessary because ...
181  try {
182  if (selection && !((*selection)((*oH)[i])))
183  continue;
184  value_->push_back((expr)((*oH)[i]));
185  } catch (...) {
186  LogDebug("StringBranchHelper")
187  << "could not evaluate expression: " << B.expr() << " on class: " << B.className();
188  value_->push_back(defaultValue); //push a default value to not change the indexing
189  }
190  }
191  }
192  if (selection)
193  delete selection;
194  }
195  }
196 
197 private:
199 };
200 
201 class StringBasedNTupler : public NTupler {
202 public:
204  edm::ParameterSet branchesPSet = iConfig.getParameter<edm::ParameterSet>("branchesPSet");
205  std::vector<std::string> branches;
206  branchesPSet.getParameterSetNames(branches);
207  const std::string separator = branchesPSet.getUntrackedParameter<std::string>("separator", ":");
208  for (uint b = 0; b != branches.size(); ++b) {
209  edm::ParameterSet bPSet = branchesPSet.getParameter<edm::ParameterSet>(branches[b]);
210  std::string className = "";
211  if (bPSet.exists("class"))
212  className = bPSet.getParameter<std::string>("class");
213  else
214  className = bPSet.getParameter<std::string>("Class");
215  edm::InputTag src = edm::Service<InputTagDistributorService>()->retrieve("src", bPSet);
216  edm::ParameterSet leavesPSet = bPSet.getParameter<edm::ParameterSet>("leaves");
217  std::string order = "";
218  if (bPSet.exists("order"))
219  order = bPSet.getParameter<std::string>("order");
220  std::string selection = "";
221  if (bPSet.exists("selection"))
222  selection = bPSet.getParameter<std::string>("selection");
223  // do it one by one with configuration [string x = "x"]
224  std::vector<std::string> leaves = leavesPSet.getParameterNamesForType<std::string>();
225  std::string maxName = "N" + branches[b];
226  for (uint l = 0; l != leaves.size(); ++l) {
227  std::string leave_expr = leavesPSet.getParameter<std::string>(leaves[l]);
228  std::string branchAlias = branches[b] + "_" + leaves[l];
229 
230  //add a branch manager for this expression on this collection
231  branches_[maxName].push_back(TreeBranch(className, src, leave_expr, order, selection, maxName, branchAlias));
232  } //loop the provided leaves
233 
234  //do it once with configuration [vstring vars = { "x:x" ,... } ] where ":"=separator
235  if (leavesPSet.exists("vars")) {
236  std::vector<std::string> leavesS = leavesPSet.getParameter<std::vector<std::string>>("vars");
237  for (uint l = 0; l != leavesS.size(); ++l) {
238  uint sep = leavesS[l].find(separator);
239  std::string name = leavesS[l].substr(0, sep);
240  //removes spaces from the variable name
241  /*uint*/ int space = name.find(' ');
242  while (space != -1 /*std::string::npos*/) {
243  std::string first = name.substr(0, space);
244  std::string second = name.substr(space + 1);
245  name = first + second;
246  space = name.find(' ');
247  }
248  std::string expr = leavesS[l].substr(sep + 1);
249  std::string branchAlias = branches[b] + "_" + name;
250 
251  //add a branch manager for this expression on this collection
252  branches_[maxName].push_back(TreeBranch(className, src, expr, order, selection, maxName, branchAlias));
253  }
254  }
255 
256  } //loop the provided branches
257 
258  ev_ = new uint64_t;
259  run_ = new uint;
260  lumiblock_ = new uint;
261  experimentType_ = new uint;
262  bunchCrossing_ = new uint;
263  orbitNumber_ = new uint;
264  weight_ = new float;
266 
267  if (branchesPSet.exists("useTFileService"))
268  useTFileService_ = branchesPSet.getParameter<bool>("useTFileService");
269  else
270  useTFileService_ = iConfig.getParameter<bool>("useTFileService");
271 
272  if (useTFileService_) {
273  if (branchesPSet.exists("treeName")) {
274  treeName_ = branchesPSet.getParameter<std::string>("treeName");
275  ownTheTree_ = true;
276  } else {
277  treeName_ = iConfig.getParameter<std::string>("treeName");
278  ownTheTree_ = false;
279  }
280  }
281  }
282 
283  uint registerleaves(edm::ProducesCollector producesCollector) override {
284  uint nLeaves = 0;
285 
286  if (useTFileService_) {
288  if (ownTheTree_) {
289  ownTheTree_ = true;
290  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
291  } else {
292  TObject* object = fs->file().Get(treeName_.c_str());
293  if (!object) {
294  ownTheTree_ = true;
295  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
296  } else {
297  tree_ = dynamic_cast<TTree*>(object);
298  if (!tree_) {
299  ownTheTree_ = true;
300  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
301  } else
302  ownTheTree_ = false;
303  }
304  }
305 
306  //reserve memory for the indexes
307  indexDataHolder_ = new uint[branches_.size()];
308  // loop the automated leafer
309  Branches::iterator iB = branches_.begin();
310  Branches::iterator iB_end = branches_.end();
311  uint indexOfIndexInDataHolder = 0;
312  for (; iB != iB_end; ++iB, ++indexOfIndexInDataHolder) {
313  //create a branch for the index: an integer
314  tree_->Branch(iB->first.c_str(), &(indexDataHolder_[indexOfIndexInDataHolder]), (iB->first + "/i").c_str());
315  //loop on the "leaves"
316  std::vector<TreeBranch>::iterator iL = iB->second.begin();
317  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
318  for (; iL != iL_end; ++iL) {
319  TreeBranch& b = *iL;
320  //create a branch for the leaves: vector of floats
321  TBranch* br = tree_->Branch(b.branchAlias().c_str(), "std::vector<float>", iL->dataHolderPtrAdress());
322  br->SetTitle(b.branchTitle().c_str());
323  nLeaves++;
324  }
325  }
326 
327  //extra leaves for event info.
328  tree_->Branch("run", run_, "run/i");
329  tree_->Branch("event", ev_, "event/l");
330  tree_->Branch("lumiblock", lumiblock_, "lumiblock/i");
331  tree_->Branch("experimentType", experimentType_, "experimentType/i");
332  tree_->Branch("bunchCrossing", bunchCrossing_, "bunchCrossing/i");
333  tree_->Branch("orbitNumber", orbitNumber_, "orbitNumber/i");
334  tree_->Branch("weight", weight_, "weight/f");
335  tree_->Branch("model_params", &model_params_);
336 
337  } else {
338  // loop the automated leafer
339  Branches::iterator iB = branches_.begin();
340  Branches::iterator iB_end = branches_.end();
341  for (; iB != iB_end; ++iB) {
342  //the index. should produce it only once
343  // a simple uint for the index
344  producesCollector.produces<uint>(iB->first).setBranchAlias(iB->first);
345  std::vector<TreeBranch>::iterator iL = iB->second.begin();
346  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
347  for (; iL != iL_end; ++iL) {
348  TreeBranch& b = *iL;
349  //a vector of float for each leave
350  producesCollector.produces<std::vector<float>>(b.branchName()).setBranchAlias(b.branchAlias());
351  nLeaves++;
352  }
353  }
354  }
355  return nLeaves;
356  }
357 
358  void fill(edm::Event& iEvent) override {
359  // if (!edm::Service<UpdaterService>()->checkOnce("StringBasedNTupler::fill")) return;
360  //well if you do that, you cannot have two ntupler of the same type in the same job...
361 
362  if (useTFileService_) {
363  // loop the automated leafer
364  Branches::iterator iB = branches_.begin();
365  Branches::iterator iB_end = branches_.end();
366  uint indexOfIndexInDataHolder = 0;
367  for (; iB != iB_end; ++iB, ++indexOfIndexInDataHolder) {
368  std::vector<TreeBranch>::iterator iL = iB->second.begin();
369  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
370  uint maxS = 0;
371  for (; iL != iL_end; ++iL) {
372  TreeBranch& b = *iL;
373  // grab the vector of values from the interpretation of expression for the associated collection
374  std::unique_ptr<std::vector<float>> branch(b.branch(iEvent));
375  // calculate the maximum index size.
376  if (branch->size() > maxS)
377  maxS = branch->size();
378  // transfer of (no copy) pointer to the vector of float from the std::unique_ptr to the tree data pointer
379  b.assignDataHolderPtr(branch.release());
380  // for memory tracing, object b is holding the data (not std::unique_ptr) and should delete it for each event (that's not completely optimum)
381  }
382  //assigne the maximum vector size for this collection
383  indexDataHolder_[indexOfIndexInDataHolder] = maxS;
384  }
385 
386  //fill event info.
387  *run_ = iEvent.id().run();
388  *ev_ = iEvent.id().event();
389  // *lumiblock_ = iEvent.id().luminosityBlock();
390  *lumiblock_ = iEvent.luminosityBlock();
391  *experimentType_ = iEvent.experimentType();
392  *bunchCrossing_ = iEvent.bunchCrossing();
393  *orbitNumber_ = iEvent.orbitNumber();
394 
395  *weight_ = 1;
396  if (!iEvent.isRealData()) {
397  edm::Handle<GenEventInfoProduct> wgeneventinfo;
398  iEvent.getByLabel("generator", wgeneventinfo);
399  *weight_ = wgeneventinfo->weight();
400  }
401 
402  typedef std::vector<std::string>::const_iterator comments_const_iterator;
403  // using namespace edm;
404 
406  *model_params_ = "NULL";
407  if (iEvent.getByLabel("source", product)) {
408  comments_const_iterator c_begin = product->comments_begin();
409  comments_const_iterator c_end = product->comments_end();
410 
411  for (comments_const_iterator cit = c_begin; cit != c_end; ++cit) {
412  size_t found = (*cit).find("model");
413  if (found != std::string::npos) {
414  //std::cout << *cit << std::endl;
415  *model_params_ = *cit;
416  }
417  }
418  }
419 
420  if (ownTheTree_) {
421  tree_->Fill();
422  }
423  } else {
424  // loop the automated leafer
425  Branches::iterator iB = branches_.begin();
426  Branches::iterator iB_end = branches_.end();
427  for (; iB != iB_end; ++iB) {
428  std::vector<TreeBranch>::iterator iL = iB->second.begin();
429  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
430  uint maxS = 0;
431  for (; iL != iL_end; ++iL) {
432  TreeBranch& b = *iL;
433  std::unique_ptr<std::vector<float>> branch(b.branch(iEvent));
434  if (branch->size() > maxS)
435  maxS = branch->size();
436  iEvent.put(std::move(branch), b.branchName());
437  }
438  //index should be put only once per branch. doe not really mattter for edm root files
439  iEvent.put(std::make_unique<uint>(maxS), iB->first);
440  }
441  }
442  }
443 
444  void callBack() {
445  if (useTFileService_) {
446  Branches::iterator iB = branches_.begin();
447  Branches::iterator iB_end = branches_.end();
448  //de-allocate memory now: allocated in branch(...) and released to the pointer.
449  for (; iB != iB_end; ++iB) {
450  std::vector<TreeBranch>::iterator iL = iB->second.begin();
451  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
452  for (; iL != iL_end; ++iL) {
453  TreeBranch& b = *iL;
454  delete b.dataHolderPtr();
455  }
456  }
457  }
458  }
459 
460  ~StringBasedNTupler() override {
461  delete indexDataHolder_;
462  delete ev_;
463  delete run_;
464  delete lumiblock_;
465  delete experimentType_;
466  delete bunchCrossing_;
467  delete orbitNumber_;
468  delete weight_;
469  delete model_params_;
470  }
471 
472 protected:
473  typedef std::map<std::string, std::vector<TreeBranch>> Branches;
475 
479 
480  //event info
487  float* weight_;
489 };
490 
491 #endif
const std::string & branchTitle() const
RunNumber_t run() const
Definition: EventID.h:38
EventNumber_t event() const
Definition: EventID.h:40
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
TTree * tree_
Definition: NTupler.h:30
Definition: APVGainStruct.h:7
const std::string & branchAlias() const
std::vector< float > * dataHolderPtr()
const std::string branchName() const
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
void fill(edm::Event &iEvent) override
value branch(const edm::Event &iEvent)
std::string order_
selection
main part
Definition: corrVsCorr.py:100
bool exists(std::string const &parameterName) const
checks if a parameter exists
int bunchCrossing() const
Definition: EventBase.h:64
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
std::string branchTitle_
std::string maxIndexName_
TreeBranch::value value
StringLeaveHelper(const TreeBranch &B, const edm::Event &iEvent)
const std::string & selection() const
Log< level::Error, false > LogError
bool isRealData() const
Definition: EventBase.h:62
std::string encode() const
Definition: InputTag.cc:159
std::vector< float > * dataHolderPtr_
const std::string & order() const
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
std::string * model_params_
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:224
const edm::InputTag & src() const
def move
Definition: eostools.py:511
std::map< std::string, std::vector< TreeBranch > > Branches
~StringBasedNTupler() override
TreeBranch(std::string C, edm::InputTag S, std::string E, std::string O, std::string SE, std::string Mi, std::string Ba)
std::string branchAlias_
int orbitNumber() const
Definition: EventBase.h:65
std::unique_ptr< std::vector< float > > value
std::string expr_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:500
TreeBranch::value value
uint registerleaves(edm::ProducesCollector producesCollector) override
edm::InputTag src_
void assignDataHolderPtr(std::vector< float > *data)
Log< level::Info, false > LogInfo
bool failedToGet() const
Definition: HandleBase.h:72
TFile & file() const
return opened TFile
Definition: TFileService.h:37
unsigned long long uint64_t
Definition: Time.h:13
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double b
Definition: hdecay.h:118
std::string selection_
const std::string & className() const
edm::EventID id() const
Definition: EventBase.h:59
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
edm::EventAuxiliary::ExperimentType experimentType() const
Definition: EventBase.h:63
static std::string const separator(":")
const std::string & maxIndexName() const
std::string class_
bool useTFileService_
Definition: NTupler.h:29
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
const std::string & expr() const
StringBranchHelper(const TreeBranch &B, const edm::Event &iEvent)
std::string className(const T &t)
Definition: ClassName.h:31
StringBasedNTupler(const edm::ParameterSet &iConfig)
#define LogDebug(id)
std::vector< float > ** dataHolderPtrAdress()