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

Member Typedef Documentation

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

Definition at line 478 of file StringBasedNTupler.h.

Constructor & Destructor Documentation

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

Definition at line 208 of file StringBasedNTupler.h.

References b, TreeBranch::branchAlias(), TreeBranch::className(), edm::ParameterSet::exists(), TreeBranch::expr(), dqmdumpme::first, dqmMemoryStats::float, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSetNames(), edm::ParameterSet::getUntrackedParameter(), cmsLHEtoEOSManager::l, Skims_PA_cff::name, TreeBranch::order(), edm::second(), TreeBranch::selection(), mps_merge::separator, TreeBranch::src(), AlCaHLTBitMon_QueryRunRegistry::string, TreeBranch::TreeBranch(), and parallelization::uint.

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

Definition at line 465 of file StringBasedNTupler.h.

465  {
466  delete indexDataHolder_;
467  delete ev_;
468  delete run_;
469  delete lumiblock_;
470  delete experimentType_;
471  delete bunchCrossing_;
472  delete orbitNumber_;
473  delete weight_;
474  delete model_params_;
475  }
std::string * model_params_

Member Function Documentation

void StringBasedNTupler::callBack ( )
inline

Definition at line 449 of file StringBasedNTupler.h.

References b, and TreeBranch::dataHolderPtr().

Referenced by CompleteNTupler::fill().

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

Implements NTupler.

Definition at line 363 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(), newFWLiteAna::found, edm::Event::getByLabel(), edm::EventBase::id(), edm::EventBase::isRealData(), edm::EventBase::luminosityBlock(), eostools::move(), edm::EventBase::orbitNumber(), edm::Event::put(), edm::EventID::run(), parallelization::uint, and GenEventInfoProduct::weight().

Referenced by CompleteNTupler::fill().

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

Implements NTupler.

Definition at line 288 of file StringBasedNTupler.h.

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

Referenced by CompleteNTupler::registerleaves().

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

Member Data Documentation

Branches StringBasedNTupler::branches_
protected

Definition at line 479 of file StringBasedNTupler.h.

uint* StringBasedNTupler::bunchCrossing_
protected

Definition at line 490 of file StringBasedNTupler.h.

uint64_t* StringBasedNTupler::ev_
protected

Definition at line 486 of file StringBasedNTupler.h.

uint* StringBasedNTupler::experimentType_
protected

Definition at line 489 of file StringBasedNTupler.h.

uint* StringBasedNTupler::indexDataHolder_
protected

Definition at line 483 of file StringBasedNTupler.h.

uint* StringBasedNTupler::lumiblock_
protected

Definition at line 488 of file StringBasedNTupler.h.

std::string* StringBasedNTupler::model_params_
protected

Definition at line 493 of file StringBasedNTupler.h.

uint* StringBasedNTupler::orbitNumber_
protected

Definition at line 491 of file StringBasedNTupler.h.

bool StringBasedNTupler::ownTheTree_
protected

Definition at line 481 of file StringBasedNTupler.h.

uint* StringBasedNTupler::run_
protected

Definition at line 487 of file StringBasedNTupler.h.

std::string StringBasedNTupler::treeName_
protected

Definition at line 482 of file StringBasedNTupler.h.

float* StringBasedNTupler::weight_
protected

Definition at line 492 of file StringBasedNTupler.h.