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"))
29  {
32  for(;v!=v_end;++v){
33  leaves_[v->second->name()]=v->second;
34  }
35  }
36  else{
37  std::vector<std::string> leaves=variablePSet.getParameter<std::vector<std::string> >("leaves");
38  for (uint i=0;i!=leaves.size();++i){
39  leaves_[leaves[i]]= edm::Service<VariableHelperService>()->get().variable(leaves[i]);
40  }
41  }
42  if (variablePSet.exists("useTFileService"))
43  useTFileService_=variablePSet.getParameter<bool>("useTFileService");
44  else
45  useTFileService_=iConfig.getParameter<bool>("useTFileService");
46 
47  if (useTFileService_){
48  if (variablePSet.exists("treeName"))
49  treeName_=variablePSet.getParameter<std::string>("treeName");
50  else
51  treeName_=iConfig.getParameter<std::string>("treeName");
52  }
53  }
54 
56  uint nLeaves=0;
57  if (useTFileService_){
58  //loop the leaves registered
59  nLeaves=leaves_.size();
60  // make arrays of pointer to the actual values
61  dataHolder_=new double[nLeaves];
62  iterator i=leaves_.begin();
63  iterator i_end= leaves_.end();
65  if (ownTheTree_){
66  ownTheTree_=true;
67  tree_=fs->make<TTree>(treeName_.c_str(),"VariableNTupler tree");
68  }else{
69  TObject * object = fs->file().Get(treeName_.c_str());
70  if (!object){
71  ownTheTree_=true;
72  tree_=fs->make<TTree>(treeName_.c_str(),"VariableNTupler tree");
73  }
74  else{
75  tree_=dynamic_cast<TTree*>(object);
76  if (!tree_){
77  ownTheTree_=true;
78  tree_=fs->make<TTree>(treeName_.c_str(),"VariableNTupler tree");
79  }
80  else ownTheTree_=false;
81  }
82  }
83  uint iInDataHolder=0;
84  for(;i!=i_end;++i,++iInDataHolder){
85  tree_->Branch(i->first.c_str(), &(dataHolder_[iInDataHolder]), (i->first+"/D").c_str());
86  }
87  }else{
88  //loop the leaves registered
89  iterator i=leaves_.begin();
90  iterator i_end= leaves_.end();
91  for(;i!=i_end;++i){
92  nLeaves++;
93  std::string lName(i->first);
94  std::replace(lName.begin(), lName.end(), '_','0');
95  producer->produces<double>(lName.c_str()).setBranchAlias(i->first);
96  }
97  }
98  return nLeaves;
99  }
100 
102  if (useTFileService_){
103  //fill the data holder
104  iterator i=leaves_.begin();
105  iterator i_end=leaves_.end();
106  uint iInDataHolder=0;
107  for(;i!=i_end;++i,++iInDataHolder){
108  dataHolder_[iInDataHolder]=(*i->second)(iEvent);
109  }
110  //fill into root;
111  if (ownTheTree_) {
112  tree_->Fill();
113  }
114  }else{
115  //other leaves
116  iterator i=leaves_.begin();
117  iterator i_end=leaves_.end();
118  for(;i!=i_end;++i){
119  auto leafValue = std::make_unique<double>((*i->second)(iEvent));
120  std::string lName(i->first);
121  std::replace(lName.begin(), lName.end(),'_','0');
122  iEvent.put(std::move(leafValue), lName.c_str());
123  }
124  }
125  }
126  void callBack(){}
127 
128  protected:
129  typedef std::map<std::string, const CachingVariable *>::iterator iterator;
130  std::map<std::string, const CachingVariable *> leaves_;
131 
134  double * dataHolder_;
135 };
136 
137 
138 #endif
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
void fill(edm::Event &iEvent)
TTree * tree_
Definition: NTupler.h:30
VariableNTupler(const edm::ParameterSet &iConfig)
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
std::map< std::string, const CachingVariable * >::iterator iterator
int iEvent
Definition: GenABIO.cc:230
uint registerleaves(edm::ProducerBase *producer)
TFile & file() const
return opened TFile
Definition: TFileService.h:37
std::map< std::string, const CachingVariable * > leaves_
bool useTFileService_
Definition: NTupler.h:29
std::map< std::string, const CachingVariable * >::const_iterator iterator
def move(src, dest)
Definition: eostools.py:510