Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:32:03

0001 /*
0002  */
0003 
0004 #ifndef EcalDigis_CollHandle_h
0005 #define EcalDigis_CollHandle_h
0006 
0007 #include "FWCore/Utilities/interface/InputTag.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/ConsumesCollector.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 /** Utilitity class for handling an EDM data collection. This class
0013  * acts as a wrapper of the EDM collection.
0014  * <P>An InputTag indentifying the collection is passed to the constructor.
0015  * The collection is retrieved from the event by a call to the read() method.
0016  * The CollHandle class instance can then be used as a pointer to the retrieved
0017  * collection.
0018  * <P>Absence of the collection from the event can be optionnaly tolerated: see
0019  * failIfNotFound parameter of the CollHandle() constructor.
0020  * <P>In case the collection was not (yet) retrieved from the collection, it
0021  * acts as a pointers to an empty collection.
0022  * <P>The templace parameter T specifies the type of the data collection.
0023  */
0024 template <class T>
0025 class CollHandle {
0026   //constructor(s) and destructor(s)
0027 public:
0028   /** Constructs a CollHandle.
0029    * @param tag InputTag identifying the data collection
0030    * @param failIfNotFound pass true if the absence of the collection
0031    * in the event must be considered as an error. See read() method.
0032    */
0033   CollHandle(const edm::InputTag& tag, bool failIfNotFound = true, bool notFoundWarn = true)
0034       : tag_(tag),
0035         token_(),
0036         currentColl_(&emptyColl_),
0037         notFoundAlreadyWarned_(false),
0038         failIfNotFound_(failIfNotFound),
0039         notFoundWarn_(notFoundWarn) {}
0040 
0041   //method(s)
0042 public:
0043   /*
0044     Receives the "ConsumesCollector" from the EDAnalyzer and declares the usage
0045      of the collection, as well as storing the token for it.
0046   */
0047 
0048   void setToken(edm::ConsumesCollector& collector) { token_ = collector.consumes<T>(tag_); }
0049 
0050   /** Retrieves the collection from the event. If failIfNotFound is true and
0051    * the collection is not found, then an edm::Exception is thrown. For other
0052    * case of exception throw see edm::Event::getByToken() method documentation.
0053    * If failIfNotFound is false and the collection is not found, an empty
0054    * collection is used; a warn message will be logged the first time
0055    * the collection is not found.
0056    * @param event the EDM event the collection must be retrieved from.
0057    */
0058   void read(const edm::Event& event) {
0059     //    try{
0060     edm::Handle<T> hColl;
0061     event.getByToken(token_, hColl);
0062 
0063     //If we must be tolerant to product absence, then
0064     //we must check validaty before calling Handle::operator*
0065     //to prevent exception throw:
0066     if (!failIfNotFound_                               // product-not-found tolerant mode
0067         && !hColl.isValid()) {                         // and the product was not found
0068       if (notFoundWarn_ && !notFoundAlreadyWarned_) {  //warning logged only once
0069         edm::LogWarning("ProductNotFound") << tag_
0070                                            << " product "
0071                                               "of type '"
0072                                            << typeid(T).name() << "' was not found!";
0073         notFoundAlreadyWarned_ = true;
0074       }
0075       currentColl_ = &emptyColl_;
0076     } else {
0077       currentColl_ = &(*hColl);
0078     }
0079   }
0080 
0081   /** Accessor to a member of the collection retrieved by read method().
0082    * Considering h a CollHandle instance, h->f() is equivalent to (*h).f().
0083    */
0084   const T* operator->() const { return currentColl_; }
0085 
0086   /** Gets the collection retrieved by read() method. If the collection was
0087    * absent from the event an empty collection is returned.
0088    */
0089   const T& operator*() const { return *currentColl_; }
0090 
0091   edm::InputTag tag() const { return tag_; }
0092 
0093 private:
0094   //attribute(s)
0095 protected:
0096 private:
0097   /** tag identifying the data collecion
0098    */
0099   const edm::InputTag tag_;
0100 
0101   /* EDM "Token" that is used in the actual data retrieval */
0102   edm::EDGetTokenT<T> token_;
0103 
0104   /** Pointer to the last read collection, points to emptColl be default
0105    */
0106   const T* currentColl_;
0107 
0108   /** An empty collection used as default when the collection was not retrieved
0109    * from the event.
0110    */
0111   const T emptyColl_;
0112 
0113   /** Used to emit warn message in case of collection absence only once.
0114    */
0115   bool notFoundAlreadyWarned_;
0116 
0117   /** Switch for collection absence toleration.
0118    */
0119   bool failIfNotFound_;
0120 
0121   /** Switch for the warning in case of not found collection
0122    */
0123   bool notFoundWarn_;
0124 };
0125 
0126 #endif