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) override
 
uint registerleaves (edm::ProducesCollector producesCollector) override
 
 StringBasedNTupler (const edm::ParameterSet &iConfig)
 
 ~StringBasedNTupler () override
 
- 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 201 of file StringBasedNTupler.h.

Member Typedef Documentation

◆ Branches

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

Definition at line 473 of file StringBasedNTupler.h.

Constructor & Destructor Documentation

◆ StringBasedNTupler()

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

Definition at line 203 of file StringBasedNTupler.h.

203  {
204  edm::ParameterSet branchesPSet = iConfig.getParameter<edm::ParameterSet>("branchesPSet");
205  std::vector<std::string> branches;
206  branchesPSet.getParameterSetNames(branches);
207  const std::string separator = branchesPSet.getUntrackedParameter<std::string>("separator", ":");
208  for (uint b = 0; b != branches.size(); ++b) {
209  edm::ParameterSet bPSet = branchesPSet.getParameter<edm::ParameterSet>(branches[b]);
210  std::string className = "";
211  if (bPSet.exists("class"))
212  className = bPSet.getParameter<std::string>("class");
213  else
214  className = bPSet.getParameter<std::string>("Class");
215  edm::InputTag src = edm::Service<InputTagDistributorService>()->retrieve("src", bPSet);
216  edm::ParameterSet leavesPSet = bPSet.getParameter<edm::ParameterSet>("leaves");
217  std::string order = "";
218  if (bPSet.exists("order"))
219  order = bPSet.getParameter<std::string>("order");
220  std::string selection = "";
221  if (bPSet.exists("selection"))
222  selection = bPSet.getParameter<std::string>("selection");
223  // do it one by one with configuration [string x = "x"]
224  std::vector<std::string> leaves = leavesPSet.getParameterNamesForType<std::string>();
225  std::string maxName = "N" + branches[b];
226  for (uint l = 0; l != leaves.size(); ++l) {
227  std::string leave_expr = leavesPSet.getParameter<std::string>(leaves[l]);
228  std::string branchAlias = branches[b] + "_" + leaves[l];
229 
230  //add a branch manager for this expression on this collection
231  branches_[maxName].push_back(TreeBranch(className, src, leave_expr, order, selection, maxName, branchAlias));
232  } //loop the provided leaves
233 
234  //do it once with configuration [vstring vars = { "x:x" ,... } ] where ":"=separator
235  if (leavesPSet.exists("vars")) {
236  std::vector<std::string> leavesS = leavesPSet.getParameter<std::vector<std::string> >("vars");
237  for (uint l = 0; l != leavesS.size(); ++l) {
238  uint sep = leavesS[l].find(separator);
239  std::string name = leavesS[l].substr(0, sep);
240  //removes spaces from the variable name
241  /*uint*/ int space = name.find(" ");
242  while (space != -1 /*std::string::npos*/) {
243  std::string first = name.substr(0, space);
244  std::string second = name.substr(space + 1);
245  name = first + second;
246  space = name.find(" ");
247  }
248  std::string expr = leavesS[l].substr(sep + 1);
249  std::string branchAlias = branches[b] + "_" + name;
250 
251  //add a branch manager for this expression on this collection
252  branches_[maxName].push_back(TreeBranch(className, src, expr, order, selection, maxName, branchAlias));
253  }
254  }
255 
256  } //loop the provided branches
257 
258  ev_ = new uint64_t;
259  run_ = new uint;
260  lumiblock_ = new uint;
261  experimentType_ = new uint;
262  bunchCrossing_ = new uint;
263  orbitNumber_ = new uint;
264  weight_ = new float;
266 
267  if (branchesPSet.exists("useTFileService"))
268  useTFileService_ = branchesPSet.getParameter<bool>("useTFileService");
269  else
270  useTFileService_ = iConfig.getParameter<bool>("useTFileService");
271 
272  if (useTFileService_) {
273  if (branchesPSet.exists("treeName")) {
274  treeName_ = branchesPSet.getParameter<std::string>("treeName");
275  ownTheTree_ = true;
276  } else {
277  treeName_ = iConfig.getParameter<std::string>("treeName");
278  ownTheTree_ = false;
279  }
280  }
281  }

References b, branches_, bunchCrossing_, className(), ev_, edm::ParameterSet::exists(), experimentType_, jets_cff::expr, dqmdumpme::first, dqmMemoryStats::float, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSetNames(), edm::ParameterSet::getUntrackedParameter(), cmsLHEtoEOSManager::l, lumiblock_, model_params_, Skims_PA_cff::name, orbitNumber_, eventshapeDQM_cfi::order, ownTheTree_, run_, edm::second(), corrVsCorr::selection, mps_merge::separator, TrackRefitter_38T_cff::src, AlCaHLTBitMon_QueryRunRegistry::string, treeName_, parallelization::uint, NTupler::useTFileService_, and weight_.

◆ ~StringBasedNTupler()

StringBasedNTupler::~StringBasedNTupler ( )
inlineoverride

Definition at line 460 of file StringBasedNTupler.h.

460  {
461  delete indexDataHolder_;
462  delete ev_;
463  delete run_;
464  delete lumiblock_;
465  delete experimentType_;
466  delete bunchCrossing_;
467  delete orbitNumber_;
468  delete weight_;
469  delete model_params_;
470  }

References bunchCrossing_, ev_, experimentType_, indexDataHolder_, lumiblock_, model_params_, orbitNumber_, run_, and weight_.

Member Function Documentation

◆ callBack()

void StringBasedNTupler::callBack ( )
inline

Definition at line 444 of file StringBasedNTupler.h.

444  {
445  if (useTFileService_) {
446  Branches::iterator iB = branches_.begin();
447  Branches::iterator iB_end = branches_.end();
448  //de-allocate memory now: allocated in branch(...) and released to the pointer.
449  for (; iB != iB_end; ++iB) {
450  std::vector<TreeBranch>::iterator iL = iB->second.begin();
451  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
452  for (; iL != iL_end; ++iL) {
453  TreeBranch& b = *iL;
454  delete b.dataHolderPtr();
455  }
456  }
457  }
458  }

References b, branches_, and NTupler::useTFileService_.

Referenced by CompleteNTupler::fill().

◆ fill()

void StringBasedNTupler::fill ( edm::Event iEvent)
inlineoverridevirtual

Implements NTupler.

Definition at line 358 of file StringBasedNTupler.h.

358  {
359  // if (!edm::Service<UpdaterService>()->checkOnce("StringBasedNTupler::fill")) return;
360  //well if you do that, you cannot have two ntupler of the same type in the same job...
361 
362  if (useTFileService_) {
363  // loop the automated leafer
364  Branches::iterator iB = branches_.begin();
365  Branches::iterator iB_end = branches_.end();
366  uint indexOfIndexInDataHolder = 0;
367  for (; iB != iB_end; ++iB, ++indexOfIndexInDataHolder) {
368  std::vector<TreeBranch>::iterator iL = iB->second.begin();
369  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
370  uint maxS = 0;
371  for (; iL != iL_end; ++iL) {
372  TreeBranch& b = *iL;
373  // grab the vector of values from the interpretation of expression for the associated collection
374  std::unique_ptr<std::vector<float> > branch(b.branch(iEvent));
375  // calculate the maximum index size.
376  if (branch->size() > maxS)
377  maxS = branch->size();
378  // transfer of (no copy) pointer to the vector of float from the std::unique_ptr to the tree data pointer
379  b.assignDataHolderPtr(branch.release());
380  // 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)
381  }
382  //assigne the maximum vector size for this collection
383  indexDataHolder_[indexOfIndexInDataHolder] = maxS;
384  }
385 
386  //fill event info.
387  *run_ = iEvent.id().run();
388  *ev_ = iEvent.id().event();
389  // *lumiblock_ = iEvent.id().luminosityBlock();
390  *lumiblock_ = iEvent.luminosityBlock();
391  *experimentType_ = iEvent.experimentType();
392  *bunchCrossing_ = iEvent.bunchCrossing();
393  *orbitNumber_ = iEvent.orbitNumber();
394 
395  *weight_ = 1;
396  if (!iEvent.isRealData()) {
397  edm::Handle<GenEventInfoProduct> wgeneventinfo;
398  iEvent.getByLabel("generator", wgeneventinfo);
399  *weight_ = wgeneventinfo->weight();
400  }
401 
402  typedef std::vector<std::string>::const_iterator comments_const_iterator;
403  // using namespace edm;
404 
406  *model_params_ = "NULL";
407  if (iEvent.getByLabel("source", product)) {
408  comments_const_iterator c_begin = product->comments_begin();
409  comments_const_iterator c_end = product->comments_end();
410 
411  for (comments_const_iterator cit = c_begin; cit != c_end; ++cit) {
412  size_t found = (*cit).find("model");
413  if (found != std::string::npos) {
414  //std::cout << *cit << std::endl;
415  *model_params_ = *cit;
416  }
417  }
418  }
419 
420  if (ownTheTree_) {
421  tree_->Fill();
422  }
423  } else {
424  // loop the automated leafer
425  Branches::iterator iB = branches_.begin();
426  Branches::iterator iB_end = branches_.end();
427  for (; iB != iB_end; ++iB) {
428  std::vector<TreeBranch>::iterator iL = iB->second.begin();
429  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
430  uint maxS = 0;
431  for (; iL != iL_end; ++iL) {
432  TreeBranch& b = *iL;
433  std::unique_ptr<std::vector<float> > branch(b.branch(iEvent));
434  if (branch->size() > maxS)
435  maxS = branch->size();
436  iEvent.put(std::move(branch), b.branchName());
437  }
438  //index should be put only once per branch. doe not really mattter for edm root files
439  iEvent.put(std::make_unique<uint>(maxS), iB->first);
440  }
441  }
442  }

References b, MicroEventContent_cff::branch, branches_, bunchCrossing_, LHEEventProduct::comments_begin(), LHEEventProduct::comments_end(), ev_, experimentType_, newFWLiteAna::found, iEvent, indexDataHolder_, lumiblock_, model_params_, eostools::move(), orbitNumber_, ownTheTree_, run_, NTupler::tree_, parallelization::uint, NTupler::useTFileService_, GenEventInfoProduct::weight(), and weight_.

Referenced by CompleteNTupler::fill().

◆ registerleaves()

uint StringBasedNTupler::registerleaves ( edm::ProducesCollector  producesCollector)
inlineoverridevirtual

Implements NTupler.

Definition at line 283 of file StringBasedNTupler.h.

283  {
284  uint nLeaves = 0;
285 
286  if (useTFileService_) {
288  if (ownTheTree_) {
289  ownTheTree_ = true;
290  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
291  } else {
292  TObject* object = fs->file().Get(treeName_.c_str());
293  if (!object) {
294  ownTheTree_ = true;
295  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
296  } else {
297  tree_ = dynamic_cast<TTree*>(object);
298  if (!tree_) {
299  ownTheTree_ = true;
300  tree_ = fs->make<TTree>(treeName_.c_str(), "StringBasedNTupler tree");
301  } else
302  ownTheTree_ = false;
303  }
304  }
305 
306  //reserve memory for the indexes
307  indexDataHolder_ = new uint[branches_.size()];
308  // loop the automated leafer
309  Branches::iterator iB = branches_.begin();
310  Branches::iterator iB_end = branches_.end();
311  uint indexOfIndexInDataHolder = 0;
312  for (; iB != iB_end; ++iB, ++indexOfIndexInDataHolder) {
313  //create a branch for the index: an integer
314  tree_->Branch(iB->first.c_str(), &(indexDataHolder_[indexOfIndexInDataHolder]), (iB->first + "/i").c_str());
315  //loop on the "leaves"
316  std::vector<TreeBranch>::iterator iL = iB->second.begin();
317  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
318  for (; iL != iL_end; ++iL) {
319  TreeBranch& b = *iL;
320  //create a branch for the leaves: vector of floats
321  TBranch* br = tree_->Branch(b.branchAlias().c_str(), "std::vector<float>", iL->dataHolderPtrAdress());
322  br->SetTitle(b.branchTitle().c_str());
323  nLeaves++;
324  }
325  }
326 
327  //extra leaves for event info.
328  tree_->Branch("run", run_, "run/i");
329  tree_->Branch("event", ev_, "event/l");
330  tree_->Branch("lumiblock", lumiblock_, "lumiblock/i");
331  tree_->Branch("experimentType", experimentType_, "experimentType/i");
332  tree_->Branch("bunchCrossing", bunchCrossing_, "bunchCrossing/i");
333  tree_->Branch("orbitNumber", orbitNumber_, "orbitNumber/i");
334  tree_->Branch("weight", weight_, "weight/f");
335  tree_->Branch("model_params", &model_params_);
336 
337  } else {
338  // loop the automated leafer
339  Branches::iterator iB = branches_.begin();
340  Branches::iterator iB_end = branches_.end();
341  for (; iB != iB_end; ++iB) {
342  //the index. should produce it only once
343  // a simple uint for the index
344  producesCollector.produces<uint>(iB->first).setBranchAlias(iB->first);
345  std::vector<TreeBranch>::iterator iL = iB->second.begin();
346  std::vector<TreeBranch>::iterator iL_end = iB->second.end();
347  for (; iL != iL_end; ++iL) {
348  TreeBranch& b = *iL;
349  //a vector of float for each leave
350  producesCollector.produces<std::vector<float> >(b.branchName()).setBranchAlias(b.branchAlias());
351  nLeaves++;
352  }
353  }
354  }
355  return nLeaves;
356  }

References b, beamvalidation::br, branches_, bunchCrossing_, ev_, experimentType_, TFileService::file(), indexDataHolder_, lumiblock_, TFileService::make(), model_params_, orbitNumber_, ownTheTree_, edm::ProducesCollector::produces(), run_, NTupler::tree_, treeName_, parallelization::uint, NTupler::useTFileService_, and weight_.

Referenced by CompleteNTupler::registerleaves().

Member Data Documentation

◆ branches_

Branches StringBasedNTupler::branches_
protected

Definition at line 474 of file StringBasedNTupler.h.

Referenced by callBack(), fill(), registerleaves(), and StringBasedNTupler().

◆ bunchCrossing_

uint* StringBasedNTupler::bunchCrossing_
protected

◆ ev_

uint64_t* StringBasedNTupler::ev_
protected

◆ experimentType_

uint* StringBasedNTupler::experimentType_
protected

◆ indexDataHolder_

uint* StringBasedNTupler::indexDataHolder_
protected

Definition at line 478 of file StringBasedNTupler.h.

Referenced by fill(), registerleaves(), and ~StringBasedNTupler().

◆ lumiblock_

uint* StringBasedNTupler::lumiblock_
protected

◆ model_params_

std::string* StringBasedNTupler::model_params_
protected

◆ orbitNumber_

uint* StringBasedNTupler::orbitNumber_
protected

◆ ownTheTree_

bool StringBasedNTupler::ownTheTree_
protected

Definition at line 476 of file StringBasedNTupler.h.

Referenced by fill(), registerleaves(), and StringBasedNTupler().

◆ run_

uint* StringBasedNTupler::run_
protected

◆ treeName_

std::string StringBasedNTupler::treeName_
protected

Definition at line 477 of file StringBasedNTupler.h.

Referenced by registerleaves(), and StringBasedNTupler().

◆ weight_

float* StringBasedNTupler::weight_
protected
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
MicroEventContent_cff.branch
branch
Definition: MicroEventContent_cff.py:178
TFileService::file
TFile & file() const
return opened TFile
Definition: TFileService.h:37
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
StringBasedNTupler::experimentType_
uint * experimentType_
Definition: StringBasedNTupler.h:484
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
edm::Handle
Definition: AssociativeIterator.h:50
parallelization.uint
uint
Definition: parallelization.py:124
dqmdumpme.first
first
Definition: dqmdumpme.py:55
StringBasedNTupler::ev_
uint64_t * ev_
Definition: StringBasedNTupler.h:481
mps_merge.separator
string separator
Definition: mps_merge.py:79
TreeBranch
Definition: StringBasedNTupler.h:42
LHEEventProduct::comments_begin
comments_const_iterator comments_begin() const
Definition: LHEEventProduct.h:50
StringBasedNTupler::indexDataHolder_
uint * indexDataHolder_
Definition: StringBasedNTupler.h:478
corrVsCorr.selection
selection
main part
Definition: corrVsCorr.py:100
b
double b
Definition: hdecay.h:118
StringBasedNTupler::model_params_
std::string * model_params_
Definition: StringBasedNTupler.h:488
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
StringBasedNTupler::orbitNumber_
uint * orbitNumber_
Definition: StringBasedNTupler.h:486
edm::ParameterSet
Definition: ParameterSet.h:47
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
StringBasedNTupler::treeName_
std::string treeName_
Definition: StringBasedNTupler.h:477
eventshapeDQM_cfi.order
order
Definition: eventshapeDQM_cfi.py:8
beamvalidation.br
br
Definition: beamvalidation.py:398
StringBasedNTupler::run_
uint * run_
Definition: StringBasedNTupler.h:482
StringBasedNTupler::ownTheTree_
bool ownTheTree_
Definition: StringBasedNTupler.h:476
edm::Service
Definition: Service.h:30
edm::ParameterSet::getParameterNamesForType
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::ProducesCollector::produces
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
Definition: ProducesCollector.h:52
StringBasedNTupler::lumiblock_
uint * lumiblock_
Definition: StringBasedNTupler.h:483
StringBasedNTupler::bunchCrossing_
uint * bunchCrossing_
Definition: StringBasedNTupler.h:485
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
NTupler::tree_
TTree * tree_
Definition: NTupler.h:30
StringBasedNTupler::branches_
Branches branches_
Definition: StringBasedNTupler.h:474
NTupler::useTFileService_
bool useTFileService_
Definition: NTupler.h:29
eostools.move
def move(src, dest)
Definition: eostools.py:511
jets_cff.expr
expr
Definition: jets_cff.py:483
LHEEventProduct::comments_end
comments_const_iterator comments_end() const
Definition: LHEEventProduct.h:51
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
className
std::string className(const T &t)
Definition: ClassName.h:31
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
StringBasedNTupler::weight_
float * weight_
Definition: StringBasedNTupler.h:487
edm::InputTag
Definition: InputTag.h:15
TFileService::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileService.h:64
GenEventInfoProduct::weight
double weight() const
Definition: GenEventInfoProduct.h:35
edm::ParameterSet::getParameterSetNames
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
Definition: ParameterSet.cc:724