CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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

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

Definition at line 473 of file StringBasedNTupler.h.

Constructor & Destructor Documentation

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

Definition at line 203 of file StringBasedNTupler.h.

References b, branches_, bunchCrossing_, className(), ev_, edm::ParameterSet::exists(), experimentType_, first, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSetNames(), edm::ParameterSet::getUntrackedParameter(), cmsLHEtoEOSManager::l, lumiblock_, model_params_, mergeVDriftHistosByStation::name, orbitNumber_, ownTheTree_, run_, edm::second(), corrVsCorr::selection, edm::separator(), alcazmumu_cfi::src, AlCaHLTBitMon_QueryRunRegistry::string, treeName_, parallelization::uint(), NTupler::useTFileService_, and weight_.

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  }
T getUntrackedParameter(std::string const &, T const &) const
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:179
std::string * model_params_
U second(std::pair< T, U > const &p)
unsigned long long uint64_t
Definition: Time.h:13
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double b
Definition: hdecay.h:118
static std::string const separator(":")
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 460 of file StringBasedNTupler.h.

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

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  }
std::string * model_params_

Member Function Documentation

void StringBasedNTupler::callBack ( )
inline

Definition at line 444 of file StringBasedNTupler.h.

References b, branches_, TreeBranch::dataHolderPtr(), and NTupler::useTFileService_.

Referenced by CompleteNTupler::fill().

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

References TreeBranch::assignDataHolderPtr(), b, TreeBranch::branch(), branches_, TreeBranch::branchName(), edm::EventBase::bunchCrossing(), bunchCrossing_, ev_, edm::EventID::event(), edm::EventBase::experimentType(), experimentType_, newFWLiteAna::found, edm::Event::getByLabel(), edm::EventBase::id(), indexDataHolder_, edm::EventBase::isRealData(), lumiblock_, edm::EventBase::luminosityBlock(), model_params_, eostools::move(), edm::EventBase::orbitNumber(), orbitNumber_, ownTheTree_, edm::Event::put(), edm::EventID::run(), run_, NTupler::tree_, parallelization::uint(), NTupler::useTFileService_, and weight_.

Referenced by CompleteNTupler::fill().

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  }
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:133
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
bool isRealData() const
Definition: EventBase.h:62
std::string * model_params_
def move
Definition: eostools.py:511
int orbitNumber() const
Definition: EventBase.h:65
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:500
void assignDataHolderPtr(std::vector< float > *data)
double b
Definition: hdecay.h:118
edm::EventID id() const
Definition: EventBase.h:59
edm::EventAuxiliary::ExperimentType experimentType() const
Definition: EventBase.h:63
bool useTFileService_
Definition: NTupler.h:29
uint StringBasedNTupler::registerleaves ( edm::ProducesCollector  producesCollector)
inlineoverridevirtual

Implements NTupler.

Definition at line 283 of file StringBasedNTupler.h.

References b, beamvalidation::br, TreeBranch::branchAlias(), branches_, TreeBranch::branchName(), TreeBranch::branchTitle(), bunchCrossing_, ev_, experimentType_, TFileService::file(), compareTotals::fs, 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().

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

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

uint* StringBasedNTupler::bunchCrossing_
protected
uint64_t* StringBasedNTupler::ev_
protected
uint* StringBasedNTupler::experimentType_
protected
uint* StringBasedNTupler::indexDataHolder_
protected

Definition at line 478 of file StringBasedNTupler.h.

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

uint* StringBasedNTupler::lumiblock_
protected
std::string* StringBasedNTupler::model_params_
protected
uint* StringBasedNTupler::orbitNumber_
protected
bool StringBasedNTupler::ownTheTree_
protected

Definition at line 476 of file StringBasedNTupler.h.

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

uint* StringBasedNTupler::run_
protected
std::string StringBasedNTupler::treeName_
protected

Definition at line 477 of file StringBasedNTupler.h.

Referenced by registerleaves(), and StringBasedNTupler().

float* StringBasedNTupler::weight_
protected