CMS 3D CMS Logo

List of all members | Public Member Functions | Protected Types | Protected Attributes
StringBasedNTupler Class Reference

#include <StringBasedNTupler.h>

Inheritance diagram for StringBasedNTupler:
NTupler

Public Member Functions

void callBack ()
 
void fill (edm::Event &iEvent)
 
uint registerleaves (edm::ProducerBase *producer)
 
 StringBasedNTupler (const edm::ParameterSet &iConfig)
 
 ~StringBasedNTupler ()
 
- Public Member Functions inherited from NTupler
 NTupler ()
 
virtual ~NTupler ()
 

Protected Types

typedef std::map< std::string, std::vector< TreeBranch > > Branches
 

Protected Attributes

Branches branches_
 
uint * bunchCrossing_
 
uint64_t * ev_
 
uint * experimentType_
 
uint * indexDataHolder_
 
uint * lumiblock_
 
std::string * model_params_
 
uint * orbitNumber_
 
bool ownTheTree_
 
uint * run_
 
std::string treeName_
 
float * weight_
 
- Protected Attributes inherited from NTupler
TTree * tree_
 
bool useTFileService_
 

Detailed Description

Definition at line 202 of file StringBasedNTupler.h.

Member Typedef Documentation

typedef std::map<std::string, std::vector<TreeBranch> > StringBasedNTupler::Branches
protected

Definition at line 484 of file StringBasedNTupler.h.

Constructor & Destructor Documentation

StringBasedNTupler::StringBasedNTupler ( const edm::ParameterSet iConfig)
inline

Definition at line 206 of file StringBasedNTupler.h.

References b, TreeBranch::branchAlias(), TreeBranch::className(), edm::ParameterSet::exists(), TreeBranch::expr(), plotBeamSpotDB::first, objects.autophobj::float, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSetNames(), edm::ParameterSet::getUntrackedParameter(), checklumidiff::l, dataset::name, TreeBranch::order(), edm::second(), TreeBranch::selection(), mps_merge::separator, TreeBranch::src(), AlCaHLTBitMon_QueryRunRegistry::string, and TreeBranch::TreeBranch().

206  {
207 
208 
209 
210  edm::ParameterSet branchesPSet = iConfig.getParameter<edm::ParameterSet>("branchesPSet");
211  std::vector<std::string> branches;
212  branchesPSet.getParameterSetNames(branches);
213  const std::string separator = branchesPSet.getUntrackedParameter<std::string>("separator",":");
214  for (uint b=0;b!=branches.size();++b){
215  edm::ParameterSet bPSet = branchesPSet.getParameter<edm::ParameterSet>(branches[b]);
217  if (bPSet.exists("class"))
218  className=bPSet.getParameter<std::string>("class");
219  else
220  className=bPSet.getParameter<std::string>("Class");
222  edm::ParameterSet leavesPSet=bPSet.getParameter<edm::ParameterSet>("leaves");
223  std::string order = "";
224  if (bPSet.exists("order")) order = bPSet.getParameter<std::string>("order");
225  std::string selection = "";
226  if (bPSet.exists("selection")) selection = bPSet.getParameter<std::string>("selection");
227  // do it one by one with configuration [string x = "x"]
228  std::vector<std::string> leaves=leavesPSet.getParameterNamesForType<std::string>();
229  std::string maxName="N"+branches[b];
230  for (uint l=0;l!=leaves.size();++l){
231  std::string leave_expr=leavesPSet.getParameter<std::string>(leaves[l]);
232  std::string branchAlias=branches[b]+"_"+leaves[l];
233 
234  //add a branch manager for this expression on this collection
235  branches_[maxName].push_back(TreeBranch(className, src, leave_expr, order, selection, maxName, branchAlias));
236  }//loop the provided leaves
237 
238  //do it once with configuration [vstring vars = { "x:x" ,... } ] where ":"=separator
239  if (leavesPSet.exists("vars")){
240  std::vector<std::string> leavesS = leavesPSet.getParameter<std::vector<std::string> >("vars");
241  for (uint l=0;l!=leavesS.size();++l){
242  uint sep=leavesS[l].find(separator);
243  std::string name=leavesS[l].substr(0,sep);
244  //removes spaces from the variable name
245  /*uint*/int space = name.find(" ");
246  while (space!=-1/*std::string::npos*/){
247  std::string first = name.substr(0,space);
248  std::string second = name.substr(space+1);
249  name = first+second;
250  space = name.find(" ");
251  }
252  std::string expr=leavesS[l].substr(sep+1);
253  std::string branchAlias=branches[b]+"_"+name;
254 
255  //add a branch manager for this expression on this collection
256  branches_[maxName].push_back(TreeBranch(className, src, expr, order, selection, maxName, branchAlias));
257  }
258  }
259 
260  }//loop the provided branches
261 
262 
263 
264 
265  ev_ = new uint64_t;
266  run_ = new uint;
267  lumiblock_ = new uint;
268  experimentType_ = new uint;
269  bunchCrossing_ = new uint;
270  orbitNumber_ = new uint;
271  weight_ = new float;
273 
274 
275  if (branchesPSet.exists("useTFileService"))
276  useTFileService_=branchesPSet.getParameter<bool>("useTFileService");
277  else
278  useTFileService_=iConfig.getParameter<bool>("useTFileService");
279 
280  if (useTFileService_){
281  if (branchesPSet.exists("treeName")){
282  treeName_=branchesPSet.getParameter<std::string>("treeName");
283  ownTheTree_=true;
284  }
285  else{
286  treeName_=iConfig.getParameter<std::string>("treeName");
287  ownTheTree_=false;
288  }
289  }
290  }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
string separator
Definition: mps_merge.py:77
selection
main part
Definition: corrVsCorr.py:98
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
std::string * model_params_
U second(std::pair< T, U > const &p)
unsigned long long uint64_t
Definition: Time.h:15
double b
Definition: hdecay.h:120
bool useTFileService_
Definition: NTupler.h:29
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
std::string className(const T &t)
Definition: ClassName.h:30
StringBasedNTupler::~StringBasedNTupler ( )
inline

Definition at line 471 of file StringBasedNTupler.h.

471  {
472  delete indexDataHolder_;
473  delete ev_;
474  delete run_;
475  delete lumiblock_;
476  delete experimentType_;
477  delete bunchCrossing_;
478  delete orbitNumber_;
479  delete weight_;
480  delete model_params_;
481  }
std::string * model_params_

Member Function Documentation

void StringBasedNTupler::callBack ( )
inline

Definition at line 454 of file StringBasedNTupler.h.

References b, and TreeBranch::dataHolderPtr().

Referenced by CompleteNTupler::fill().

455  {
456  if (useTFileService_){
457  Branches::iterator iB=branches_.begin();
458  Branches::iterator iB_end=branches_.end();
459  //de-allocate memory now: allocated in branch(...) and released to the pointer.
460  for(;iB!=iB_end;++iB){
461  std::vector<TreeBranch>::iterator iL=iB->second.begin();
462  std::vector<TreeBranch>::iterator iL_end=iB->second.end();
463  for(;iL!=iL_end;++iL){
464  TreeBranch & b=*iL;
465  delete b.dataHolderPtr();
466  }
467  }
468  }
469  }
std::vector< float > * dataHolderPtr()
double b
Definition: hdecay.h:120
bool useTFileService_
Definition: NTupler.h:29
void StringBasedNTupler::fill ( edm::Event iEvent)
inlinevirtual

Implements NTupler.

Definition at line 371 of file StringBasedNTupler.h.

References TreeBranch::assignDataHolderPtr(), b, TreeBranch::branch(), TreeBranch::branchName(), edm::EventBase::bunchCrossing(), LHEEventProduct::comments_begin(), LHEEventProduct::comments_end(), edm::EventID::event(), edm::EventBase::experimentType(), runEdmFileComparison::found, edm::Event::getByLabel(), edm::EventBase::id(), edm::EventBase::isRealData(), edm::EventBase::luminosityBlock(), eostools::move(), edm::EventBase::orbitNumber(), edm::Event::put(), edm::EventID::run(), and GenEventInfoProduct::weight().

Referenced by CompleteNTupler::fill().

371  {
372  // if (!edm::Service<UpdaterService>()->checkOnce("StringBasedNTupler::fill")) return;
373  //well if you do that, you cannot have two ntupler of the same type in the same job...
374 
375  if (useTFileService_){
376  // loop the automated leafer
377  Branches::iterator iB=branches_.begin();
378  Branches::iterator iB_end=branches_.end();
379  uint indexOfIndexInDataHolder=0;
380  for(;iB!=iB_end;++iB,++indexOfIndexInDataHolder){
381  std::vector<TreeBranch>::iterator iL=iB->second.begin();
382  std::vector<TreeBranch>::iterator iL_end=iB->second.end();
383  uint maxS=0;
384  for(;iL!=iL_end;++iL){
385  TreeBranch & b=*iL;
386  // grab the vector of values from the interpretation of expression for the associated collection
387  std::unique_ptr<std::vector<float> > branch(b.branch(iEvent));
388  // calculate the maximum index size.
389  if (branch->size()>maxS) maxS=branch->size();
390  // transfer of (no copy) pointer to the vector of float from the std::unique_ptr to the tree data pointer
391  b.assignDataHolderPtr(branch.release());
392  // for memory tracing, object b is holding the data (not std::unique_ptr) and should delete it for each event (that's not completely optimum)
393  }
394  //assigne the maximum vector size for this collection
395  indexDataHolder_[indexOfIndexInDataHolder]=maxS;
396  }
397 
398  //fill event info.
399  *run_ = iEvent.id().run();
400  *ev_ = iEvent.id().event();
401  // *lumiblock_ = iEvent.id().luminosityBlock();
402  *lumiblock_ = iEvent.luminosityBlock();
403  *experimentType_ = iEvent.experimentType();
404  *bunchCrossing_ = iEvent.bunchCrossing();
405  *orbitNumber_ = iEvent.orbitNumber();
406 
407  *weight_ = 1;
408  if(!iEvent.isRealData()) {
409  edm::Handle<GenEventInfoProduct> wgeneventinfo;
410  iEvent.getByLabel("generator", wgeneventinfo);
411  *weight_ = wgeneventinfo->weight();
412  }
413 
414  typedef std::vector<std::string>::const_iterator comments_const_iterator;
415 // using namespace edm;
416 
418  *model_params_ = "NULL";
419  if(iEvent.getByLabel("source", product)) {
420  comments_const_iterator c_begin = product->comments_begin();
421  comments_const_iterator c_end = product->comments_end();
422 
423  for( comments_const_iterator cit=c_begin; cit!=c_end; ++cit) {
424  size_t found = (*cit).find("model");
425  if( found != std::string::npos) {
426  //std::cout << *cit << std::endl;
427  *model_params_ = *cit;
428  }
429  }
430  }
431 
432 
433  if (ownTheTree_){ tree_->Fill(); }
434  }else{
435  // loop the automated leafer
436  Branches::iterator iB=branches_.begin();
437  Branches::iterator iB_end=branches_.end();
438  for(;iB!=iB_end;++iB){
439  std::vector<TreeBranch>::iterator iL=iB->second.begin();
440  std::vector<TreeBranch>::iterator iL_end=iB->second.end();
441  uint maxS=0;
442  for(;iL!=iL_end;++iL){
443  TreeBranch & b=*iL;
444  std::unique_ptr<std::vector<float> > branch(b.branch(iEvent));
445  if (branch->size()>maxS) maxS=branch->size();
446  iEvent.put(std::move(branch), b.branchName());
447  }
448  //index should be put only once per branch. doe not really mattter for edm root files
449  iEvent.put(std::make_unique<uint>(maxS), iB->first);
450  }
451  }
452  }
RunNumber_t run() const
Definition: EventID.h:39
EventNumber_t event() const
Definition: EventID.h:41
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
TTree * tree_
Definition: NTupler.h:30
const std::string branchName() const
value branch(const edm::Event &iEvent)
int bunchCrossing() const
Definition: EventBase.h:66
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:63
double weight() const
bool isRealData() const
Definition: EventBase.h:64
std::string * model_params_
comments_const_iterator comments_begin() const
int orbitNumber() const
Definition: EventBase.h:67
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:464
void assignDataHolderPtr(std::vector< float > *data)
double b
Definition: hdecay.h:120
edm::EventID id() const
Definition: EventBase.h:60
comments_const_iterator comments_end() const
edm::EventAuxiliary::ExperimentType experimentType() const
Definition: EventBase.h:65
bool useTFileService_
Definition: NTupler.h:29
def move(src, dest)
Definition: eostools.py:510
uint StringBasedNTupler::registerleaves ( edm::ProducerBase producer)
inlinevirtual

Implements NTupler.

Definition at line 294 of file StringBasedNTupler.h.

References b, beamvalidation::br, TreeBranch::branchAlias(), TreeBranch::branchName(), TreeBranch::branchTitle(), TFileService::file(), TFileService::make(), resolutioncreator_cfi::object, and edm::ProductRegistryHelper::produces().

Referenced by CompleteNTupler::registerleaves().

294  {
295  uint nLeaves=0;
296 
297  if (useTFileService_){
299  if (ownTheTree_){
300  ownTheTree_=true;
301  tree_=fs->make<TTree>(treeName_.c_str(),"StringBasedNTupler tree");
302  }else{
303  TObject * object = fs->file().Get(treeName_.c_str());
304  if (!object){
305  ownTheTree_=true;
306  tree_=fs->make<TTree>(treeName_.c_str(),"StringBasedNTupler tree");
307  }
308  else{
309  tree_=dynamic_cast<TTree*>(object);
310  if (!tree_){
311  ownTheTree_=true;
312  tree_=fs->make<TTree>(treeName_.c_str(),"StringBasedNTupler tree");
313  }
314  else ownTheTree_=false;
315  }
316  }
317 
318  //reserve memory for the indexes
319  indexDataHolder_ = new uint[branches_.size()];
320  // loop the automated leafer
321  Branches::iterator iB=branches_.begin();
322  Branches::iterator iB_end=branches_.end();
323  uint indexOfIndexInDataHolder=0;
324  for(;iB!=iB_end;++iB,++indexOfIndexInDataHolder){
325  //create a branch for the index: an integer
326  tree_->Branch(iB->first.c_str(), &(indexDataHolder_[indexOfIndexInDataHolder]),(iB->first+"/i").c_str());
327  //loop on the "leaves"
328  std::vector<TreeBranch>::iterator iL=iB->second.begin();
329  std::vector<TreeBranch>::iterator iL_end=iB->second.end();
330  for(;iL!=iL_end;++iL){
331  TreeBranch & b=*iL;
332  //create a branch for the leaves: vector of floats
333  TBranch * br = tree_->Branch(b.branchAlias().c_str(),"std::vector<float>",iL->dataHolderPtrAdress());
334  br->SetTitle(b.branchTitle().c_str());
335  nLeaves++;
336  }
337  }
338 
339  //extra leaves for event info.
340  tree_->Branch("run",run_,"run/i");
341  tree_->Branch("event",ev_,"event/l");
342  tree_->Branch("lumiblock",lumiblock_,"lumiblock/i");
343  tree_->Branch("experimentType",experimentType_,"experimentType/i");
344  tree_->Branch("bunchCrossing",bunchCrossing_,"bunchCrossing/i");
345  tree_->Branch("orbitNumber",orbitNumber_,"orbitNumber/i");
346  tree_->Branch("weight",weight_,"weight/f");
347  tree_->Branch("model_params",&model_params_);
348 
349  }
350  else{
351  // loop the automated leafer
352  Branches::iterator iB=branches_.begin();
353  Branches::iterator iB_end=branches_.end();
354  for(;iB!=iB_end;++iB){
355  //the index. should produce it only once
356  // a simple uint for the index
357  producer->produces<uint>(iB->first).setBranchAlias(iB->first);
358  std::vector<TreeBranch>::iterator iL=iB->second.begin();
359  std::vector<TreeBranch>::iterator iL_end=iB->second.end();
360  for(;iL!=iL_end;++iL){
361  TreeBranch & b=*iL;
362  //a vector of float for each leave
363  producer->produces<std::vector<float> >(b.branchName()).setBranchAlias(b.branchAlias());
364  nLeaves++;
365  }
366  }
367  }
368  return nLeaves;
369  }
const std::string & branchTitle() const
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
TTree * tree_
Definition: NTupler.h:30
const std::string & branchAlias() const
const std::string branchName() const
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
std::string * model_params_
TFile & file() const
return opened TFile
Definition: TFileService.h:37
double b
Definition: hdecay.h:120
bool useTFileService_
Definition: NTupler.h:29

Member Data Documentation

Branches StringBasedNTupler::branches_
protected

Definition at line 485 of file StringBasedNTupler.h.

uint* StringBasedNTupler::bunchCrossing_
protected

Definition at line 496 of file StringBasedNTupler.h.

uint64_t* StringBasedNTupler::ev_
protected

Definition at line 492 of file StringBasedNTupler.h.

uint* StringBasedNTupler::experimentType_
protected

Definition at line 495 of file StringBasedNTupler.h.

uint* StringBasedNTupler::indexDataHolder_
protected

Definition at line 489 of file StringBasedNTupler.h.

uint* StringBasedNTupler::lumiblock_
protected

Definition at line 494 of file StringBasedNTupler.h.

std::string* StringBasedNTupler::model_params_
protected

Definition at line 499 of file StringBasedNTupler.h.

uint* StringBasedNTupler::orbitNumber_
protected

Definition at line 497 of file StringBasedNTupler.h.

bool StringBasedNTupler::ownTheTree_
protected

Definition at line 487 of file StringBasedNTupler.h.

uint* StringBasedNTupler::run_
protected

Definition at line 493 of file StringBasedNTupler.h.

std::string StringBasedNTupler::treeName_
protected

Definition at line 488 of file StringBasedNTupler.h.

float* StringBasedNTupler::weight_
protected

Definition at line 498 of file StringBasedNTupler.h.