--- UserCode/MitProd/TreeFiller/interface/BaseFiller.h 2008/07/07 16:14:01 1.7 +++ UserCode/MitProd/TreeFiller/interface/BaseFiller.h 2008/07/30 16:39:57 1.11 @@ -1,5 +1,5 @@ //-------------------------------------------------------------------------------------------------- -// $Id: BaseFiller.h,v 1.7 2008/07/07 16:14:01 loizides Exp $ +// $Id: BaseFiller.h,v 1.11 2008/07/30 16:39:57 loizides Exp $ // // BaseFiller // @@ -12,9 +12,14 @@ #define TREEFILLER_BASEFILLER_H #include "FWCore/Framework/interface/Event.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "DataFormats/Common/interface/Handle.h" #include "MitAna/DataUtil/interface/TreeWriter.h" +#include "MitProd/ObjectService/interface/ObjectService.h" +#include "MitProd/TreeFiller/interface/FillMitTree.h" +#include namespace mithep { @@ -32,10 +37,57 @@ namespace mithep protected: const edm::ParameterSet &Conf() const { return config_; } + void PrintErrorAndExit(const char *msg) const; + template + void GetProduct(const std::string name, edm::Handle &prod, + const edm::Event &event) const; + template + bool GetProductSafe(const std::string name, edm::Handle &prod, + const edm::Event &event) const; + + ObjectService *OS() { return FillMitTree::os(); } const std::string name_; //name of this filler const edm::ParameterSet config_; //parameter set for this filler const bool active_; //=1 if active }; } + +//-------------------------------------------------------------------------------------------------- +template +inline void mithep::BaseFiller::GetProduct(const std::string edmname, edm::Handle &prod, + const edm::Event &event) const +{ + // Try to access data collection from EDM file. We check if we really get just one + // product with the given name. If not we print an error and exit. + + try { + event.getByLabel(edm::InputTag(edmname),prod); + if (!prod.isValid()) + throw edm::Exception(edm::errors::Configuration, "BaseFiller::GetProduct()\n") + << "Cannot get collection with label " << edmname << std::endl; + } catch (...) { + edm::LogError("BaseFiller") << "Cannot get collection with label " + << edmname << std::endl; + PrintErrorAndExit(Form("Cannot get collection with label %s", edmname.c_str())); + } +} + +//-------------------------------------------------------------------------------------------------- +template +inline bool mithep::BaseFiller::GetProductSafe(const std::string edmname, edm::Handle &prod, + const edm::Event &event) const +{ + // Try to safely access data collection from EDM file. We check if we really get just one + // product with the given name. If not, we return false. + + try { + event.getByLabel(edm::InputTag(edmname),prod); + if (!prod.isValid()) + return false; + } catch (...) { + return false; + } + return true; +} #endif