CMS 3D CMS Logo

VariableNTupler.h
Go to the documentation of this file.
1 #ifndef VariableNtupler_NTupler_H
2 #define VariableNtupler_NTupler_H
3 
5 
9 
11 #include "TTree.h"
12 #include "TBranch.h"
13 #include "TFile.h"
14 
16 
17 #include <algorithm>
18 
19 class VariableNTupler : public NTupler {
20 public:
22  ownTheTree_ = false;
23  edm::ParameterSet variablePSet = iConfig.getParameter<edm::ParameterSet>("variablesPSet");
24  if (variablePSet.getParameter<bool>("allVariables")) {
27  for (; v != v_end; ++v) {
28  leaves_[v->second->name()] = v->second;
29  }
30  } else {
31  std::vector<std::string> leaves = variablePSet.getParameter<std::vector<std::string> >("leaves");
32  for (uint i = 0; i != leaves.size(); ++i) {
33  leaves_[leaves[i]] = edm::Service<VariableHelperService>()->get().variable(leaves[i]);
34  }
35  }
36  if (variablePSet.exists("useTFileService"))
37  useTFileService_ = variablePSet.getParameter<bool>("useTFileService");
38  else
39  useTFileService_ = iConfig.getParameter<bool>("useTFileService");
40 
41  if (useTFileService_) {
42  if (variablePSet.exists("treeName"))
43  treeName_ = variablePSet.getParameter<std::string>("treeName");
44  else
45  treeName_ = iConfig.getParameter<std::string>("treeName");
46  }
47  }
48 
49  uint registerleaves(edm::ProducesCollector producesCollector) override {
50  uint nLeaves = 0;
51  if (useTFileService_) {
52  //loop the leaves registered
53  nLeaves = leaves_.size();
54  // make arrays of pointer to the actual values
55  dataHolder_ = new double[nLeaves];
56  iterator i = leaves_.begin();
57  iterator i_end = leaves_.end();
59  if (ownTheTree_) {
60  ownTheTree_ = true;
61  tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
62  } else {
63  TObject* object = fs->file().Get(treeName_.c_str());
64  if (!object) {
65  ownTheTree_ = true;
66  tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
67  } else {
68  tree_ = dynamic_cast<TTree*>(object);
69  if (!tree_) {
70  ownTheTree_ = true;
71  tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
72  } else
73  ownTheTree_ = false;
74  }
75  }
76  uint iInDataHolder = 0;
77  for (; i != i_end; ++i, ++iInDataHolder) {
78  tree_->Branch(i->first.c_str(), &(dataHolder_[iInDataHolder]), (i->first + "/D").c_str());
79  }
80  } else {
81  //loop the leaves registered
82  iterator i = leaves_.begin();
83  iterator i_end = leaves_.end();
84  for (; i != i_end; ++i) {
85  nLeaves++;
86  std::string lName(i->first);
87  std::replace(lName.begin(), lName.end(), '_', '0');
88  producesCollector.produces<double>(lName).setBranchAlias(i->first);
89  }
90  }
91  return nLeaves;
92  }
93 
94  void fill(edm::Event& iEvent) override {
95  if (useTFileService_) {
96  //fill the data holder
97  iterator i = leaves_.begin();
98  iterator i_end = leaves_.end();
99  uint iInDataHolder = 0;
100  for (; i != i_end; ++i, ++iInDataHolder) {
101  dataHolder_[iInDataHolder] = (*i->second)(iEvent);
102  }
103  //fill into root;
104  if (ownTheTree_) {
105  tree_->Fill();
106  }
107  } else {
108  //other leaves
109  iterator i = leaves_.begin();
110  iterator i_end = leaves_.end();
111  for (; i != i_end; ++i) {
112  auto leafValue = std::make_unique<double>((*i->second)(iEvent));
113  std::string lName(i->first);
114  std::replace(lName.begin(), lName.end(), '_', '0');
115  iEvent.put(std::move(leafValue), lName);
116  }
117  }
118  }
119  void callBack() {}
120 
121 protected:
122  typedef std::map<std::string, const CachingVariable*>::iterator iterator;
123  std::map<std::string, const CachingVariable*> leaves_;
124 
127  double* dataHolder_;
128 };
129 
130 #endif
std::map< std::string, const CachingVariable * >::iterator iterator
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
TTree * tree_
Definition: NTupler.h:28
std::map< std::string, const CachingVariable * > leaves_
VariableNTupler(const edm::ParameterSet &iConfig)
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
bool exists(std::string const &parameterName) const
checks if a parameter exists
def replace(string, replacements)
std::string treeName_
int iEvent
Definition: GenABIO.cc:224
uint registerleaves(edm::ProducesCollector producesCollector) override
std::map< std::string, const CachingVariable * >::const_iterator iterator
bool useTFileService_
Definition: NTupler.h:27
def move(src, dest)
Definition: eostools.py:511
void fill(edm::Event &iEvent) override