CMS 3D CMS Logo

CollHandle.h

Go to the documentation of this file.
00001 /*
00002  * $Id: CollHandle.h,v 1.4 2008/07/03 14:29:00 pgras Exp $
00003  */
00004 
00005 #ifndef EcalDigis_CollHandle_h
00006 #define EcalDigis_CollHandle_h
00007 
00008 #include "FWCore/ParameterSet/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 was not found!";
00065         notFoundAlreadyWarned_ = true;
00066       }
00067       currentColl_ = &emptyColl_;
00068     } else {
00069       currentColl_ = &(*hColl);
00070     }
00071   }
00072 
00073   
00077   const T* operator->() const{ return currentColl_;}
00078 
00082   const T& operator*() const { return *currentColl_;}
00083   
00084 private:
00085 
00086   //attribute(s)
00087 protected:
00088 private:
00091   const edm::InputTag tag_;
00092 
00095   const T* currentColl_;
00096 
00100   const T emptyColl_;
00101 
00104   bool notFoundAlreadyWarned_;
00105 
00108   bool failIfNotFound_;
00109 
00112   bool notFoundWarn_;
00113 };
00114 
00115 #endif

Generated on Tue Jun 9 17:49:06 2009 for CMSSW by  doxygen 1.5.4