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 
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
mps_fire.i
i
Definition: mps_fire.py:428
VariableNTupler::fill
void fill(edm::Event &iEvent) override
Definition: VariableNTupler.h:96
EDProducer.h
VariableNTupler::ownTheTree_
bool ownTheTree_
Definition: VariableNTupler.h:127
TFileService::file
TFile & file() const
return opened TFile
Definition: TFileService.h:37
VariableNTupler::dataHolder_
double * dataHolder_
Definition: VariableNTupler.h:129
EDFilter.h
findQualityFiles.v
v
Definition: findQualityFiles.py:179
parallelization.uint
uint
Definition: parallelization.py:124
ProducesCollector.h
VariableNTupler::registerleaves
uint registerleaves(edm::ProducesCollector producesCollector) override
Definition: VariableNTupler.h:51
VariableHelper::iterator
std::map< std::string, const CachingVariable * >::const_iterator iterator
Definition: VariableHelper.h:19
simpleEdmComparison.lName
lName
Definition: simpleEdmComparison.py:125
TFileService.h
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
edm::ParameterSet
Definition: ParameterSet.h:47
NTupler
Definition: NTupler.h:20
edm::Service
Definition: Service.h:30
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::ProducesCollector::produces
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
Definition: ProducesCollector.h:52
VariableNTupler
Definition: VariableNTupler.h:21
TFileService::make
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
InputTag.h
VariableNTupler::iterator
std::map< std::string, const CachingVariable * >::iterator iterator
Definition: VariableNTupler.h:124
NTupler::tree_
TTree * tree_
Definition: NTupler.h:30
NTupler::useTFileService_
bool useTFileService_
Definition: NTupler.h:29
NTupler.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
edm::ProducesCollector
Definition: ProducesCollector.h:43
Frameworkfwd.h
VariableNTupler::leaves_
std::map< std::string, const CachingVariable * > leaves_
Definition: VariableNTupler.h:125
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
VariableNTupler::VariableNTupler
VariableNTupler(const edm::ParameterSet &iConfig)
Definition: VariableNTupler.h:23
VariableNTupler::callBack
void callBack()
Definition: VariableNTupler.h:121
VariableHelper.h
edm::Event
Definition: Event.h:73
VariableNTupler::treeName_
std::string treeName_
Definition: VariableNTupler.h:128
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444