CMS 3D CMS Logo

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