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