CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Validation/EcalDigis/src/CollHandle.h

Go to the documentation of this file.
00001 /*
00002  * $Id: CollHandle.h,v 1.6 2010/02/11 00:15:21 wmtan Exp $
00003  */
00004 
00005 #ifndef EcalDigis_CollHandle_h
00006 #define EcalDigis_CollHandle_h
00007 
00008 #include "FWCore/Utilities/interface/InputTag.h"
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00024 template<class T>
00025 class CollHandle {
00026   //constructor(s) and destructor(s)
00027 public:
00033   CollHandle(const edm::InputTag& tag,
00034              bool failIfNotFound = true,
00035              bool notFoundWarn = true): tag_(tag),
00036                                         currentColl_(&emptyColl_),
00037                                         notFoundAlreadyWarned_(false),
00038                                         failIfNotFound_(failIfNotFound),
00039                                         notFoundWarn_(notFoundWarn){}
00040   
00041   //method(s)
00042 public:
00051   void read(const edm::Event& event){
00052     //    try{
00053     edm::Handle<T> hColl;
00054     event.getByLabel(tag_, hColl);
00055   
00056     //If we must be tolerant to product absence, then
00057     //we must check validaty before calling Handle::operator*
00058     //to prevent exception throw:
00059     if(!failIfNotFound_     // product-not-found tolerant mode
00060        && !hColl.isValid()){// and the product was not found
00061       if(notFoundWarn_
00062          && !notFoundAlreadyWarned_){//warning logged only once
00063         edm::LogWarning("ProductNotFound") << tag_
00064                                            << " product "
00065           "of type '" << typeid(T).name() << "' was not found!";
00066         notFoundAlreadyWarned_ = true;
00067       }
00068       currentColl_ = &emptyColl_;
00069     } else {
00070       currentColl_ = &(*hColl);
00071     }
00072   }
00073 
00074   
00078   const T* operator->() const{ return currentColl_;}
00079 
00083   const T& operator*() const { return *currentColl_;}
00084 
00085   edm::InputTag tag() const { return tag_; }
00086   
00087 private:
00088 
00089   //attribute(s)
00090 protected:
00091 private:
00094   const edm::InputTag tag_;
00095 
00098   const T* currentColl_;
00099 
00103   const T emptyColl_;
00104 
00107   bool notFoundAlreadyWarned_;
00108 
00111   bool failIfNotFound_;
00112 
00115   bool notFoundWarn_;
00116 };
00117 
00118 #endif