Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-11 03:12:55

0001 #ifndef UtilAlgos_NtpProducer_h
0002 #define UtilAlgos_NtpProducer_h
0003 /** \class NtpProducer
0004  *
0005  * Creates histograms defined in config file
0006  *
0007  * \author: Luca Lista, INFN
0008  *
0009  * Template parameters:
0010  * - C : Concrete candidate collection type
0011  *
0012  */
0013 #include "FWCore/Framework/interface/stream/EDProducer.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "DataFormats/Provenance/interface/RunLumiEventNumber.h"
0017 #include "FWCore/Utilities/interface/InputTag.h"
0018 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0019 
0020 template <typename C>
0021 class NtpProducer : public edm::stream::EDProducer<> {
0022 public:
0023   /// constructor from parameter set
0024   NtpProducer(const edm::ParameterSet&);
0025   /// destructor
0026   ~NtpProducer() override;
0027 
0028 protected:
0029   /// process an event
0030   void produce(edm::Event&, const edm::EventSetup&) override;
0031 
0032 private:
0033   /// label of the collection to be read in
0034   edm::EDGetTokenT<C> srcToken_;
0035   /// variable tags
0036   std::vector<std::pair<std::string, StringObjectFunction<typename C::value_type> > > tags_;
0037   bool lazyParser_;
0038   std::string prefix_;
0039   bool eventInfo_;
0040 };
0041 
0042 template <typename C>
0043 NtpProducer<C>::NtpProducer(const edm::ParameterSet& par)
0044     : srcToken_(consumes<C>(par.template getParameter<edm::InputTag>("src"))),
0045       lazyParser_(par.template getUntrackedParameter<bool>("lazyParser", false)),
0046       prefix_(par.template getUntrackedParameter<std::string>("prefix", "")),
0047       eventInfo_(par.template getUntrackedParameter<bool>("eventInfo", true)) {
0048   std::vector<edm::ParameterSet> variables = par.template getParameter<std::vector<edm::ParameterSet> >("variables");
0049   std::vector<edm::ParameterSet>::const_iterator q = variables.begin(), end = variables.end();
0050   if (eventInfo_) {
0051     produces<edm::EventNumber_t>(prefix_ + "EventNumber").setBranchAlias(prefix_ + "EventNumber");
0052     produces<unsigned int>(prefix_ + "RunNumber").setBranchAlias(prefix_ + "RunNumber");
0053     produces<unsigned int>(prefix_ + "LumiBlock").setBranchAlias(prefix_ + "Lumiblock");
0054   }
0055   for (; q != end; ++q) {
0056     std::string tag = prefix_ + q->getUntrackedParameter<std::string>("tag");
0057     StringObjectFunction<typename C::value_type> quantity(q->getUntrackedParameter<std::string>("quantity"),
0058                                                           lazyParser_);
0059     tags_.push_back(std::make_pair(tag, quantity));
0060     produces<std::vector<float> >(tag).setBranchAlias(tag);
0061   }
0062 }
0063 
0064 template <typename C>
0065 NtpProducer<C>::~NtpProducer() {}
0066 
0067 template <typename C>
0068 void NtpProducer<C>::produce(edm::Event& iEvent, const edm::EventSetup&) {
0069   edm::Handle<C> coll;
0070   iEvent.getByToken(srcToken_, coll);
0071   if (eventInfo_) {
0072     std::unique_ptr<edm::EventNumber_t> event(new edm::EventNumber_t);
0073     std::unique_ptr<unsigned int> run(new unsigned int);
0074     std::unique_ptr<unsigned int> lumi(new unsigned int);
0075     *event = iEvent.id().event();
0076     *run = iEvent.id().run();
0077     *lumi = iEvent.luminosityBlock();
0078     iEvent.put(std::move(event), prefix_ + "EventNumber");
0079     iEvent.put(std::move(run), prefix_ + "RunNumber");
0080     iEvent.put(std::move(lumi), prefix_ + "LumiBlock");
0081   }
0082   typename std::vector<std::pair<std::string, StringObjectFunction<typename C::value_type> > >::const_iterator
0083       q = tags_.begin(),
0084       end = tags_.end();
0085   for (; q != end; ++q) {
0086     std::unique_ptr<std::vector<float> > x(new std::vector<float>);
0087     x->reserve(coll->size());
0088     for (typename C::const_iterator elem = coll->begin(); elem != coll->end(); ++elem) {
0089       x->push_back(q->second(*elem));
0090     }
0091     iEvent.put(std::move(x), q->first);
0092   }
0093 }
0094 
0095 #endif