CMS 3D CMS Logo

CachingVariable.h
Go to the documentation of this file.
1 #ifndef ConfigurableAnalysis_CachingVariable_H
2 #define ConfigurableAnalysis_CachingVariable_H
3 
12 
14 
15 namespace edm {
16  class EventSetup;
17 }
18 #include <vector>
19 #include "TString.h"
20 
21 class Description {
22  public:
24  Description(std::vector<std::string> & d) : d_(d){}
25  std::string text() const {
27  for (unsigned int i=0;i!=d_.size();++i)
28  text+=d_[i]+"\n";
29  return text;
30  }
31  const std::vector<std::string> lines(){return d_;}
32  void addLine(const std::string & l){d_.push_back(l);}
33  private :
34  std::vector<std::string> d_;
35 };
36 
37 class VariableComputer;
38 
40  public:
41  //every variable return double values
42  typedef double valueType;
43  typedef std::pair<bool, valueType> evalType;
44  typedef std::map<std::string, const CachingVariable*> vMap;
46  CachingVariableFactoryArg( const CachingVariableFactoryArg & copy) : n(copy.n),m(copy.m),iConfig(copy.iConfig){}
51  };
52 
54  cache_(std::make_pair(false,0)),method_(m),
55  name_(n),conf_(iConfig) {}
56 
57  virtual ~CachingVariable() {}
58 
59  //does the variable computes
60  bool compute(const edm::Event & iEvent) const { return baseEval(iEvent).first; }
61 
62  //accessor to the computed/cached value
63  valueType operator()(const edm::Event & iEvent) const { return baseEval(iEvent).second; }
64 
65  const std::string & name() const {return name_;}
66  const std::string & method() const { return method_;}
67  const Description & description()const { return d_;}
68  void addDescriptionLine(const std::string & s){ d_.addLine(s);}
69  const std::string & holderName() const { return holderName_;}
70  void setHolder(std::string hn) const { holderName_=hn;}
71 
72  void print() const {
73  edm::LogVerbatim("CachingVariable")<<name()
74  <<"\n"<<description().text();
75  }
76  protected:
77 
78  mutable evalType cache_;
79  mutable edm::Event::CacheIdentifier_t eventCacheID_=0;
80 
84  void setCache(valueType & v) const {
85  cache_.first=true; cache_.second = v;}
86  void setNotCompute() const {
87  cache_.first=false; cache_.second = 0;}
88  evalType & baseEval(const edm::Event & iEvent) const {
89  if(notSeenThisEventAlready(iEvent)) {
90  LogDebug("CachingVariable")<<name_+":"+holderName_<<" is checking once";
91  cache_=eval(iEvent);
92  }
93  return cache_;
94  }
96  bool retValue = (std::numeric_limits<edm::Event::CacheIdentifier_t>::max() != eventCacheID_ and
97  eventCacheID_ != iEvent.cacheIdentifier());
98  if(retValue) {
99  eventCacheID_=iEvent.cacheIdentifier();
100  }
101  return retValue;
102  }
103 
104  //cannot be made purely virtual otherwise one cannot have purely CachingVariableObjects
105  virtual evalType eval(const edm::Event & iEvent) const {return std::make_pair(false,0);};
106 
107  Description d_;
109  friend class VariableComputer;
110 };
111 
112 
113 class ComputedVariable;
115  public:
117  virtual ~VariableComputer(){}
118 
119  virtual void compute(const edm::Event & iEvent) const = 0;
120  const std::string & name() const { return name_;}
121  void declare(std::string var, edm::ConsumesCollector& iC);
122  void assign(std::string var, double & value) const;
123  void doesNotCompute() const;
124  void doesNotCompute(std::string var) const;
125 
126  bool notSeenThisEventAlready(const edm::Event& iEvent) const {
127  bool retValue = (std::numeric_limits<edm::Event::CacheIdentifier_t>::max() != eventCacheID_ and
128  eventCacheID_ != iEvent.cacheIdentifier());
129  if(retValue) {
130  eventCacheID_=iEvent.cacheIdentifier();
131  }
132  return retValue;
133  }
134 
135  protected:
139  mutable std::map<std::string ,const ComputedVariable *> iCompute_;
141 
142  mutable edm::Event::CacheIdentifier_t eventCacheID_=0;
143 
144 };
145 
146 
149 
152 
154  public:
157  CachingVariable(M,N,P,iC), myComputer(c){}
158  ~ComputedVariable() override{};
159 
160  evalType eval(const edm::Event & iEvent) const override {
161  if (myComputer->notSeenThisEventAlready(iEvent))
162  myComputer->compute(iEvent);
163  return cache_;
164  }
165  private:
166  std::unique_ptr<const VariableComputer> myComputer;
167 };
168 
170  public:
172  ~VariableComputerTest() override{};
173 
174  void compute(const edm::Event & iEvent) const override;
175 };
176 
177 class Splitter : public CachingVariable {
178  public:
180  CachingVariable(method,n,iConfig,iC) {}
181 
182  //purely virtual here
183  CachingVariable::evalType eval(const edm::Event & iEvent) const override =0;
184 
185  unsigned int maxIndex() const { return maxSlots()-1;}
186 
187  //maximum NUMBER of slots: counting over/under flows
188  virtual unsigned int maxSlots() const { return labels_.size();}
189 
190  const std::string shortLabel(unsigned int i) const{
191  if (i>=short_labels_.size()){
192  edm::LogError("Splitter")<<"trying to access slots short_label at index: "<<i<<"while of size: "<<short_labels_.size()<<"\n"<<conf_.dump();
193  return short_labels_.back(); }
194  else return short_labels_[i];}
195 
196  const std::string & label(unsigned int i) const{
197  if (i>=labels_.size()){
198  edm::LogError("Splitter")<<"trying to access slots label at index: "<<i<<"while of size: "<<labels_.size()<<"\n"<<conf_.dump();
199  return labels_.back(); }
200  else return labels_[i];}
201 
202  protected:
203  std::vector<std::string> labels_;
204  std::vector<std::string> short_labels_;
205 };
206 
207 
208 class VarSplitter : public Splitter{
209  public:
211  Splitter("VarSplitter",arg.n,arg.iConfig,iC) {
212  var_=arg.iConfig.getParameter<std::string>("var");
213  useUnderFlow_=arg.iConfig.getParameter<bool>("useUnderFlow");
214  useOverFlow_=arg.iConfig.getParameter<bool>("useOverFlow");
215  slots_=arg.iConfig.getParameter<std::vector<double> >("slots");
216  if (useUnderFlow_){
217  labels_.push_back("underflow");
218  short_labels_.push_back("_"+arg.n+"_underflow");}
219  std::vector<std::string> confLabels;
220  if (arg.iConfig.exists("labels")){
221  confLabels=arg.iConfig.getParameter<std::vector<std::string> >("labels");
222  }
223  else{
224  std::string labelFormat = arg.iConfig.getParameter<std::string>("labelsFormat");
225  for (unsigned int is=0;is!=slots_.size()-1;++is){
226  std::string l(Form(labelFormat.c_str(),slots_[is],slots_[is+1]));
227  confLabels.push_back(l);
228  }
229  }
230  for (unsigned int i=0;i!=confLabels.size();++i){
231  labels_.push_back(confLabels[i]);
232  std::stringstream ss;
233  ss<<"_"<<arg.n<<"_"<<i;
234  short_labels_.push_back(ss.str());
235  }
236  if (useOverFlow_)
237  { labels_.push_back("overFlow");
238  short_labels_.push_back("_"+arg.n+"_overFlow");}
239 
240  //check consistency
241  if (labels_.size()!=maxSlots())
242  edm::LogError("Splitter")<<"splitter with name: "<<name()<<" has inconsistent configuration\n"<<conf_.dump();
243 
244  arg.m[arg.n]=this;
245  }
246 
247  CachingVariable::evalType eval(const edm::Event & iEvent) const override;
248 
249  //redefine the maximum number of slots
250  unsigned int maxSlots() const override{
251  unsigned int s=slots_.size()-1;
252  if (useUnderFlow_) s++;
253  if (useOverFlow_) s++;
254  return s;}
255 
256  protected:
260  std::vector<double> slots_;
261 };
262 
263 template <typename Object, const char * label>
265  public:
267  CachingVariable(std::string(label)+"ExpressionVariable",arg.n,arg.iConfig,iC) , f_(nullptr), forder_(nullptr) {
268  srcTag_=edm::Service<InputTagDistributorService>()->retrieve("src",arg.iConfig);
269  src_=iC.consumes<edm::View<Object> >(srcTag_);
270  //old style constructor
271  if (arg.iConfig.exists("expr") && arg.iConfig.exists("index")){
273  index_=arg.iConfig.getParameter<unsigned int>("index");
275  addDescriptionLine("calculating: "+expr);
276  std::stringstream ss;
277  ss<<"on object at index: "<<index_<<" of: "<<srcTag_;
278 
279  if (arg.iConfig.exists("order")){
280  std::string order=arg.iConfig.getParameter<std::string>("order");
281  forder_ = new StringObjectFunction<Object>(order);
282  ss<<" after sorting according to: "<<order;
283  }else forder_ =nullptr;
284 
285  if (arg.iConfig.exists("selection")){
288  ss<<" and selecting only: "<<selection;
289  }else selector_=nullptr;
290 
291 
292 
293  addDescriptionLine(ss.str()); ss.str("");
294  arg.m[arg.n] = this;
295  }
296  else{
297  //multiple instance constructor
298  std::map<std::string, edm::Entry> indexEntry;
299  if (arg.n.find("_N")!=std::string::npos){
300  //will have to loop over indexes
301  std::vector<unsigned int> indexes = arg.iConfig.getParameter<std::vector<unsigned int> >("indexes");
302  for (unsigned int iI=0;iI!=indexes.size();++iI){
303  edm::ParameterSet toUse = arg.iConfig;
304  edm::Entry e("unsigned int",indexes[iI],true);
305  std::stringstream ss;
306  //add +1 0->1, 1->2, ... in the variable label
307  ss<<indexes[iI]+1;
308  indexEntry.insert(std::make_pair(ss.str(),e));
309  }
310  }//contains "_N"
311 
312  std::map< std::string, edm::Entry> varEntry;
313  if (arg.n.find("_V")!=std::string::npos){
314  //do something fancy for multiple variable from one PSet
315  std::vector<std::string> vars = arg.iConfig.getParameter<std::vector<std::string> >("vars");
316  for (unsigned int v=0;v!=vars.size();++v){
317  unsigned int sep=vars[v].find(":");
318  std::string name=vars[v].substr(0,sep);
319  std::string expr=vars[v].substr(sep+1);
320 
321  edm::Entry e("string",expr,true);
322  varEntry.insert(std::make_pair(name,e));
323  }
324  }//contains "_V"
325 
326  std::string radical = arg.n;
327  //remove the "_V";
328  if (!varEntry.empty())
329  radical = radical.substr(0,radical.size()-2);
330  //remove the "_N";
331  if (!indexEntry.empty())
332  radical = radical.substr(0,radical.size()-2);
333 
334  if(varEntry.empty()){
335  //loop only the indexes
336  for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
337  edm::ParameterSet toUse = arg.iConfig;
338  toUse.insert(true,"index",iIt->second);
339  std::string newVname = radical+iIt->first;
340  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
341  // the constructor auto log the new variable in the map
343  }
344  }else{
345  for (std::map< std::string, edm::Entry>::iterator vIt=varEntry.begin();vIt!=varEntry.end();++vIt){
346  if (indexEntry.empty()){
347  edm::ParameterSet toUse = arg.iConfig;
348  toUse.insert(true,"expr",vIt->second);
349  std::string newVname = radical+vIt->first;
350  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
351  // the constructor auto log the new variable in the map
353  }else{
354  for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
355  edm::ParameterSet toUse = arg.iConfig;
356  toUse.insert(true,"expr",vIt->second);
357  toUse.insert(true,"index",iIt->second);
358  std::string newVname = radical+iIt->first+vIt->first;
359  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
360  // the constructor auto log the new variable in the map
362  }}
363  }
364  }
365  //there is a memory leak here, because the object we are in is not logged in the arg.m, the variable is not valid
366  // anyways, but reside in memory with no ways of de-allocating it.
367  // since the caching variables are actually "global" objects, it does not matter.
368  // we cannot add it to the map, otherwise, it would be considered for eventV ntupler
369  }
370  }
372  if (f_) delete f_;
373  if (forder_) delete forder_;
374  if (selector_) delete selector_;
375  }
376 
378  if (!f_) {
379  edm::LogError(method())<<" no parser attached.";
380  return std::make_pair(false,0);
381  }
383  iEvent.getByToken(src_,oH);
384  if (index_>=oH->size()){
385  LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<srcTag_;
386  return std::make_pair(false,0);
387  }
388 
389  //get the ordering right first. if required
390  if (selector_ || forder_){
391  std::vector<const Object*> copyToSort(0);
392  copyToSort.reserve(oH->size());
393  for (unsigned int i=0;i!=oH->size();++i){
394  if (selector_ && !((*selector_)((*oH)[i]))) continue;
395  copyToSort.push_back(&(*oH)[i]);
396  }
397  if (index_ >= copyToSort.size()) return std::make_pair(false,0);
398  if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
399 
400  const Object * o = copyToSort[index_];
401  return std::make_pair(true,(*f_)(*o));
402  }
403  else{
404  const Object & o = (*oH)[index_];
405  return std::make_pair(true,(*f_)(o));
406  }
407  }
408 
409  private:
412  unsigned int index_;
416 };
417 
418 
419 template< typename LHS,const char * lLHS, typename RHS,const char * lRHS, typename Calculator>
421 public:
423  CachingVariable(Calculator::calculationType()+std::string(lLHS)+std::string(lRHS),arg.n,arg.iConfig,iC),
424  srcLhsTag_(edm::Service<InputTagDistributorService>()->retrieve("srcLhs",arg.iConfig)),
425  srcLhs_(iC.consumes<std::vector<LHS> >(srcLhsTag_)),
426  indexLhs_(arg.iConfig.getParameter<unsigned int>("indexLhs")),
427  srcRhsTag_(edm::Service<InputTagDistributorService>()->retrieve("srcRhs",arg.iConfig)),
428  srcRhs_(iC.consumes<std::vector<RHS> >(srcRhsTag_)),
429  indexRhs_(arg.iConfig.getParameter<unsigned int>("indexRhs"))
430  {
431  std::stringstream ss;
432  addDescriptionLine(Calculator::description());
433  ss<<"with Obj1 at index: "<<indexLhs_<<" of: "<<srcLhs_;
434  addDescriptionLine(ss.str()); ss.str("");
435  ss<<"with Obj2 at index: "<<indexRhs_<<" of: "<<srcRhs_;
436  addDescriptionLine(ss.str()); ss.str("");
437  arg.m[arg.n]=this;
438  }
439 
440  class getObject{
441  public:
442  getObject() : test(false),lhs(0),rhs(0){}
443  bool test;
444  const LHS * lhs;
445  const RHS * rhs;
446  };
450  iEvent.getByToken(srcLhs_, lhsH);
451  if (lhsH.failedToGet()){
452  LogDebug("TwoObjectVariable")<<name()<<" could not get a collection with label: "<<srcLhsTag_;
453  return failed;}
454  if (indexLhs_>=lhsH->size()){
455  LogDebug("TwoObjectVariable")<<name()<<" tries to access index: "<<indexLhs_<<" of: "<<srcLhsTag_<<" with: "<<lhsH->size()<<" entries.";
456  return failed;}
457  const LHS & lhs = (*lhsH)[indexLhs_];
458 
460  iEvent.getByToken(srcRhs_, rhsH);
461  if (rhsH.failedToGet()){
462  LogDebug("TwoObjectVariable")<<name()<<" could not get a collection with label: "<<srcLhsTag_;
463  return failed;}
464 
465  if (indexRhs_>=rhsH->size()){
466  LogDebug("TwoObjectVariable")<<name()<<" tries to access index: "<<indexRhs_<<" of: "<<srcRhsTag_<<" with: "<<rhsH->size()<<" entries.";
467  return failed;}
468  const RHS & rhs = (*rhsH)[indexRhs_];
469 
470  failed.test=true;
471  failed.lhs=&lhs;
472  failed.rhs=&rhs;
473  return failed;
474  }
475 
476  //to be overloaded by the user
478  Calculator calc;
479  return calc(*o.lhs,*o.rhs);
480  }
482  getObject o=objects(iEvent);
483  if (!o.test) return std::make_pair(false,0);
484  return std::make_pair(true,calculate(o));
485  }
486 private:
489  unsigned int indexLhs_;
492  unsigned int indexRhs_;
493 };
494 
495 
497  public:
499  CachingVariable("Power",arg.n,arg.iConfig,iC){
500  power_=arg.iConfig.getParameter<double>("power");
501  var_=arg.iConfig.getParameter<std::string>("var");
502  std::stringstream ss("Calculare X^Y, with X=");
503  ss<<var_<<" and Y="<<power_;
504  addDescriptionLine(ss.str());
505  arg.m[arg.n]=this;
506  }
507  ~VariablePower() override{}
508 
509  //concrete calculation of the variable
510  CachingVariable::evalType eval(const edm::Event & iEvent) const override;
511 
512  private:
513  double power_;
515 };
516 
517 
518 template <typename TYPE>
520  public:
522  CachingVariable("SimpleValueVariable",arg.n,arg.iConfig,iC),
523  src_(iC.consumes<TYPE>(edm::Service<InputTagDistributorService>()->retrieve("src",arg.iConfig))) { arg.m[arg.n]=this;}
526  iEvent.getByToken(src_,value);
527  if (value.failedToGet() || !value.isValid()) return std::make_pair(false,0);
528  else return std::make_pair(true, *value);
529  }
530  private:
532 };
533 
534 template <typename TYPE>
536  public:
538  CachingVariable("SimpleValueVectorVariable",arg.n,arg.iConfig,iC),
539  src_(iC.consumes<TYPE>(edm::Service<InputTagDistributorService>()->retrieve("src",arg.iConfig))),
540  index_(arg.iConfig.getParameter<unsigned int>("index")) { arg.m[arg.n]=this;}
543  iEvent.getByToken(src_,values);
544  if (values.failedToGet() || !values.isValid()) return std::make_pair(false,0);
545  else if (index_>=values->size()) return std::make_pair(false,0);
546  else return std::make_pair(true, (*values)[index_]);
547  }
548 
549  private:
551  unsigned int index_;
552 };
553 
554 
555 
556 
557 
558 
559 
560 #endif
#define LogDebug(id)
CachingVariable(std::string m, std::string n, const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
unsigned long CacheIdentifier_t
Definition: Event.h:109
const Description & description() const
std::string separator_
std::string method_
~ExpressionVariable() override
std::string holderName_
evalType eval(const edm::Event &iEvent) const override
CachingVariable::evalType eval(const edm::Event &iEvent) const override
def copy(args, dbName)
unsigned int maxSlots() const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::InputTag srcTag_
edm::EDGetTokenT< std::vector< LHS > > srcLhs_
const std::string & holderName() const
#define nullptr
selection
main part
Definition: corrVsCorr.py:100
void setHolder(std::string hn) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
VarSplitter(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
StringCutObjectSelector< Object > * selector_
getObject objects(const edm::Event &iEvent) const
edmplugin::PluginFactory< CachingVariable *(CachingVariable::CachingVariableFactoryArg, edm::ConsumesCollector &iC) > CachingVariableFactory
void insert(bool ok_to_replace, char const *, Entry const &)
std::vector< std::string > short_labels_
~ComputedVariable() override
edmplugin::PluginFactory< VariableComputer *(CachingVariable::CachingVariableFactoryArg, edm::ConsumesCollector &iC) > VariableComputerFactory
A arg
Definition: Factorize.h:38
valueType operator()(const edm::Event &iEvent) const
const CachingVariable::CachingVariableFactoryArg & arg_
VariablePower(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
std::vector< double > slots_
unsigned int maxIndex() const
virtual ~VariableComputer()
std::string text() const
char const * label
int iEvent
Definition: GenABIO.cc:224
TwoObjectVariable(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
edm::InputTag srcLhsTag_
~VariablePower() override
const std::string shortLabel(unsigned int i) const
StringObjectFunction< Object > * f_
evalType & baseEval(const edm::Event &iEvent) const
virtual unsigned int maxSlots() const
unsigned int indexLhs_
virtual ~CachingVariable()
CacheIdentifier_t cacheIdentifier() const
Definition: Event.cc:35
void addDescriptionLine(const std::string &s)
const std::string & label(unsigned int i) const
Description(std::vector< std::string > &d)
edm::EDGetTokenT< TYPE > src_
void print() const
void assign(const std::vector< float > &vec, float &a, float &b, float &c, float &d)
edm::InputTag srcRhsTag_
Definition: value.py:1
void setNotCompute() const
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< edm::View< Object > > src_
SimpleValueVariable(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
CachingVariable::evalType eval(const edm::Event &iEvent) const override
std::map< std::string,const ComputedVariable * > iCompute_
SimpleValueVectorVariable(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
bool failedToGet() const
Definition: HandleBase.h:78
StringObjectFunction< Object > * forder_
#define N
Definition: blowfish.cc:9
std::vector< std::string > d_
CachingVariable::evalType eval(const edm::Event &iEvent) const override
std::unique_ptr< const VariableComputer > myComputer
def compute(min, max)
Splitter(std::string method, std::string n, const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC)
edm::ParameterSet conf_
std::pair< OmniClusterRef, TrackingParticleRef > P
unsigned int indexRhs_
std::vector< std::string > labels_
edm::EDGetTokenT< TYPE > src_
const std::vector< std::string > lines()
bool compute(const edm::Event &iEvent) const
std::string name_
ExpressionVariable(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
void addLine(const std::string &l)
HLT enums.
virtual evalType eval(const edm::Event &iEvent) const
std::string var_
bool notSeenThisEventAlready(const edm::Event &iEvent) const
std::string method_
~VariableComputerTest() override
vars
Definition: DeepTauId.cc:77
CachingVariableFactoryArg(std::string &N, CachingVariable::vMap &M, edm::ParameterSet &P)
std::string var_
CachingVariableFactoryArg(const CachingVariableFactoryArg &copy)
CachingVariable::evalType eval(const edm::Event &iEvent) const override
const std::string & name() const
const std::string & name() const
edm::EDGetTokenT< std::vector< RHS > > srcRhs_
ComputedVariable(const std::string &M, std::string &N, edm::ParameterSet &P, const VariableComputer *c, edm::ConsumesCollector &iC)
void setCache(valueType &v) const
std::map< std::string, const CachingVariable * > vMap
const std::string & method() const
std::pair< bool, valueType > evalType
bool notSeenThisEventAlready(const edm::Event &iEvent) const
virtual CachingVariable::valueType calculate(getObject &o) const