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 198 of file StringBasedNTupler.h.

Member Typedef Documentation

◆ Branches

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

Definition at line 470 of file StringBasedNTupler.h.

Constructor & Destructor Documentation

◆ StringBasedNTupler()

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

Definition at line 200 of file StringBasedNTupler.h.

References b, branches_, bunchCrossing_, className(), ev_, edm::ParameterSet::exists(), experimentType_, electrons_cff::expr, dqmdumpme::first, nano_mu_digi_cff::float, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSetNames(), edm::ParameterSet::getUntrackedParameter(), MainPageGenerator::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_.

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

◆ ~StringBasedNTupler()

StringBasedNTupler::~StringBasedNTupler ( )
inlineoverride

Definition at line 457 of file StringBasedNTupler.h.

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

457  {
458  delete indexDataHolder_;
459  delete ev_;
460  delete run_;
461  delete lumiblock_;
462  delete experimentType_;
463  delete bunchCrossing_;
464  delete orbitNumber_;
465  delete weight_;
466  delete model_params_;
467  }
std::string * model_params_

Member Function Documentation

◆ callBack()

void StringBasedNTupler::callBack ( )
inline

Definition at line 441 of file StringBasedNTupler.h.

References b, branches_, and NTupler::useTFileService_.

Referenced by CompleteNTupler::fill().

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

◆ fill()

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

Implements NTupler.

Definition at line 355 of file StringBasedNTupler.h.

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().

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

◆ registerleaves()

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

Implements NTupler.

Definition at line 280 of file StringBasedNTupler.h.

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

Referenced by CompleteNTupler::registerleaves().

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

Member Data Documentation

◆ branches_

Branches StringBasedNTupler::branches_
protected

Definition at line 471 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 475 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 473 of file StringBasedNTupler.h.

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

◆ run_

uint* StringBasedNTupler::run_
protected

◆ treeName_

std::string StringBasedNTupler::treeName_
protected

Definition at line 474 of file StringBasedNTupler.h.

Referenced by registerleaves(), and StringBasedNTupler().

◆ weight_

float* StringBasedNTupler::weight_
protected