CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CollHandle.h
Go to the documentation of this file.
1 /*
2  */
3 
4 #ifndef EcalDigis_CollHandle_h
5 #define EcalDigis_CollHandle_h
6 
11 
24 template <class T>
25 class CollHandle {
26  //constructor(s) and destructor(s)
27 public:
33  CollHandle(const edm::InputTag& tag, bool failIfNotFound = true, bool notFoundWarn = true)
34  : tag_(tag),
35  token_(),
38  failIfNotFound_(failIfNotFound),
39  notFoundWarn_(notFoundWarn) {}
40 
41  //method(s)
42 public:
43  /*
44  Receives the "ConsumesCollector" from the EDAnalyzer and declares the usage
45  of the collection, as well as storing the token for it.
46  */
47 
48  void setToken(edm::ConsumesCollector& collector) { token_ = collector.consumes<T>(tag_); }
49 
58  void read(const edm::Event& event) {
59  // try{
60  edm::Handle<T> hColl;
61  event.getByToken(token_, hColl);
62 
63  //If we must be tolerant to product absence, then
64  //we must check validaty before calling Handle::operator*
65  //to prevent exception throw:
66  if (!failIfNotFound_ // product-not-found tolerant mode
67  && !hColl.isValid()) { // and the product was not found
68  if (notFoundWarn_ && !notFoundAlreadyWarned_) { //warning logged only once
69  edm::LogWarning("ProductNotFound") << tag_
70  << " product "
71  "of type '"
72  << typeid(T).name() << "' was not found!";
74  }
76  } else {
77  currentColl_ = &(*hColl);
78  }
79  }
80 
84  const T* operator->() const { return currentColl_; }
85 
89  const T& operator*() const { return *currentColl_; }
90 
91  edm::InputTag tag() const { return tag_; }
92 
93 private:
94  //attribute(s)
95 protected:
96 private:
100 
101  /* EDM "Token" that is used in the actual data retrieval */
103 
106  const T* currentColl_;
107 
111  const T emptyColl_;
112 
116 
120 
124 };
125 
126 #endif
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const T * operator->() const
Definition: CollHandle.h:84
edm::InputTag tag() const
Definition: CollHandle.h:91
const T * currentColl_
Definition: CollHandle.h:106
bool notFoundAlreadyWarned_
Definition: CollHandle.h:115
bool isValid() const
Definition: HandleBase.h:70
const T & operator*() const
Definition: CollHandle.h:89
void read(const edm::Event &event)
Definition: CollHandle.h:58
const edm::InputTag tag_
Definition: CollHandle.h:99
CollHandle(const edm::InputTag &tag, bool failIfNotFound=true, bool notFoundWarn=true)
Definition: CollHandle.h:33
bool notFoundWarn_
Definition: CollHandle.h:123
edm::EDGetTokenT< T > token_
Definition: CollHandle.h:102
void setToken(edm::ConsumesCollector &collector)
Definition: CollHandle.h:48
Log< level::Warning, false > LogWarning
const T emptyColl_
Definition: CollHandle.h:111
long double T
bool failIfNotFound_
Definition: CollHandle.h:119