Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-31 02:20:00

0001 // -*- C++ -*-
0002 //
0003 // Package:    HiMixingModule
0004 // Class:      HiMixingModule
0005 //
0006 /**\class HiMixingModule HiMixingModule.cc HeavyIonsAnalysis/HiMixingModule/src/HiMixingModule.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Yetkin Yilmaz
0015 //         Created:  Tue Feb 17 17:32:06 EST 2009
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 #include <iostream>
0023 #include <memory>
0024 #include <vector>
0025 
0026 // user include files
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/stream/EDProducer.h"
0029 
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/MakerMacros.h"
0032 
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 #include "FWCore/Framework/interface/Event.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/Utilities/interface/InputTag.h"
0037 
0038 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0039 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0040 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0041 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0042 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0043 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0044 #include "SimDataFormats/PileupSummaryInfo/interface/PileupMixingContent.h"
0045 
0046 #include "DataFormats/Provenance/interface/ProductID.h"
0047 #include "DataFormats/Common/interface/Handle.h"
0048 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0049 #include "FWCore/Utilities/interface/EDMException.h"
0050 #include "FWCore/Utilities/interface/Algorithms.h"
0051 #include "DataFormats/Common/interface/Handle.h"
0052 #include "DataFormats/Provenance/interface/Provenance.h"
0053 #include "DataFormats/Provenance/interface/ProductDescription.h"
0054 
0055 #include <string>
0056 
0057 using namespace std;
0058 
0059 //
0060 // class decleration
0061 //
0062 
0063 namespace edm {
0064 
0065   class HiMixingModule;
0066 
0067   class HiMixingWorkerBase {
0068   public:
0069     explicit HiMixingWorkerBase() { ; }
0070 
0071     HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label)
0072         : object_(object), tags_(tags), label_(label) {
0073       ;
0074     }
0075     virtual ~HiMixingWorkerBase() { ; }
0076     //   virtual void put(edm::Event &e) = 0;
0077     virtual void addSignals(edm::Event& e) = 0;
0078 
0079     std::string object_;
0080     std::vector<InputTag> tags_;
0081     std::string label_;
0082   };
0083 
0084   template <class T>
0085   class HiMixingWorker : public HiMixingWorkerBase {
0086   public:
0087     HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label)
0088         : HiMixingWorkerBase(object, tags, label) {
0089       ;
0090     }
0091     ~HiMixingWorker() override { ; }
0092     void addSignals(edm::Event& e) override {
0093       std::vector<Handle<std::vector<T> > > handles;
0094       bool get = true;
0095       for (size_t itag = 0; itag < tags_.size(); ++itag) {
0096         LogInfo("HiEmbedding") << "itag " << itag;
0097         LogInfo("HiEmbedding") << "label " << tags_[itag].label();
0098         LogInfo("HiEmbedding") << "instance " << tags_[itag].instance();
0099         Handle<std::vector<T> > hand;
0100         handles.push_back(hand);
0101         get = get && e.getByLabel(tags_[itag], handles[itag]);
0102         if (!get)
0103           LogWarning("Product inconsistency")
0104               << "One of the sub-events is missing the product with type " << object_ << ", instance "
0105               << tags_[itag].instance() << " whereas the other one is fine.";
0106       }
0107 
0108       if (get) {
0109         std::unique_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>());
0110         crFrame->addSignals(handles[0].product(), e.id());
0111         for (size_t itag = 1; itag < tags_.size(); ++itag) {
0112           std::vector<T>* product = const_cast<std::vector<T>*>(handles[itag].product());
0113           EncodedEventId id(0, itag);
0114           for (auto& item : *product) {
0115             item.setEventId(id);
0116           }
0117           crFrame->addPileups(*product);
0118         }
0119         e.put(std::move(crFrame), label_);
0120       }
0121     }
0122   };
0123 
0124   template <>
0125   void HiMixingWorker<HepMCProduct>::addSignals(edm::Event& e) {
0126     std::vector<Handle<HepMCProduct> > handles;
0127     bool get = true;
0128     for (size_t itag = 0; itag < tags_.size(); ++itag) {
0129       Handle<HepMCProduct> hand;
0130       handles.push_back(hand);
0131       get = get && e.getByLabel(tags_[itag], handles[itag]);
0132       if (!get)
0133         LogWarning("Product inconsistency")
0134             << "One of the sub-events is missing the product with type " << object_ << ", instance "
0135             << tags_[itag].instance() << " whereas the other one is fine.";
0136     }
0137 
0138     if (get) {
0139       std::unique_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>());
0140       crFrame->addSignals(handles[0].product(), e.id());
0141       for (size_t itag = 1; itag < tags_.size(); ++itag) {
0142         HepMCProduct* product = const_cast<HepMCProduct*>(handles[itag].product());
0143         crFrame->addPileups(*product);
0144       }
0145       e.put(std::move(crFrame), label_);
0146     }
0147   }
0148 
0149   class HiMixingModule : public edm::stream::EDProducer<> {
0150   public:
0151     explicit HiMixingModule(const edm::ParameterSet&);
0152     ~HiMixingModule() override;
0153 
0154   private:
0155     void produce(edm::Event&, const edm::EventSetup&) override;
0156     // ----------member data ---------------------------
0157 
0158     std::vector<HiMixingWorkerBase*> workers_;
0159   };
0160 
0161   //
0162   // constants, enums and typedefs
0163   //
0164 
0165   //
0166   // static data member definitions
0167   //
0168 
0169   //
0170   // constructors and destructor
0171   //
0172   HiMixingModule::HiMixingModule(const edm::ParameterSet& pset) {
0173     ParameterSet ps = pset.getParameter<ParameterSet>("mixObjects");
0174     std::vector<std::string> names = ps.getParameterNames();
0175     std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
0176     std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
0177 
0178     if (simtags.size() != gentags.size())
0179       LogError("MixingInput") << "Generator and Simulation input lists are not matching each other" << endl;
0180 
0181     for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
0182       ParameterSet pstag = ps.getParameter<ParameterSet>((*it));
0183       if (!pstag.exists("type"))
0184         continue;  //to allow replacement by empty pset
0185       std::string object = pstag.getParameter<std::string>("type");
0186       std::vector<InputTag> tags = pstag.getParameter<std::vector<InputTag> >("input");
0187 
0188       std::string signal;
0189       for (size_t itag = 0; itag < tags.size(); ++itag) {
0190         InputTag tag = tags[itag];
0191         std::vector<InputTag> inputs;
0192 
0193         for (size_t input = 0; input < simtags.size(); ++input) {
0194           if (object == "HepMCProduct")
0195             signal = gentags[input];
0196           else
0197             signal = simtags[input];
0198           inputs.push_back(InputTag(signal, tag.instance()));
0199         }
0200 
0201         std::string label = tag.label() + tag.instance();
0202         //   verifyRegistry(object,std::string(""),tag,label);
0203         if (object == "HepMCProduct") {
0204           workers_.push_back(new HiMixingWorker<HepMCProduct>(object, inputs, label));
0205           produces<CrossingFrame<HepMCProduct> >(label);
0206           consumes<HepMCProduct>(tag);
0207         } else if (object == "SimTrack") {
0208           workers_.push_back(new HiMixingWorker<SimTrack>(object, inputs, label));
0209           produces<CrossingFrame<SimTrack> >(label);
0210           consumes<std::vector<SimTrack> >(tag);
0211         } else if (object == "SimVertex") {
0212           workers_.push_back(new HiMixingWorker<SimVertex>(object, inputs, label));
0213           produces<CrossingFrame<SimVertex> >(label);
0214           consumes<std::vector<SimVertex> >(tag);
0215         } else if (object == "PSimHit") {
0216           workers_.push_back(new HiMixingWorker<PSimHit>(object, inputs, label));
0217           produces<CrossingFrame<PSimHit> >(label);
0218           consumes<std::vector<PSimHit> >(tag);
0219         } else if (object == "PCaloHit") {
0220           workers_.push_back(new HiMixingWorker<PCaloHit>(object, inputs, label));
0221           produces<CrossingFrame<PCaloHit> >(label);
0222           consumes<std::vector<PCaloHit> >(tag);
0223         } else
0224           LogInfo("Error") << "What the hell is this object?!";
0225 
0226         LogInfo("HiMixingModule") << "Will mix " << object << "s with InputTag= " << tag.encode() << ", label will be "
0227                                   << label;
0228       }
0229     }
0230 
0231     produces<PileupMixingContent>();
0232   }
0233 
0234   HiMixingModule::~HiMixingModule() {
0235     // do anything here that needs to be done at desctruction time
0236     // (e.g. close files, deallocate resources etc.)
0237   }
0238 
0239   //
0240   // member functions
0241   //
0242 
0243   // ------------ method called to produce the data  ------------
0244   void HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0245     using namespace edm;
0246 
0247     for (size_t i = 0; i < workers_.size(); ++i) {
0248       (workers_[i])->addSignals(iEvent);
0249     }
0250 
0251     std::unique_ptr<PileupMixingContent> PileupMixing_ = std::make_unique<PileupMixingContent>();
0252     iEvent.put(std::move(PileupMixing_));
0253   }
0254 
0255   //define this as a plug-in
0256   DEFINE_FWK_MODULE(HiMixingModule);
0257 
0258 }  // namespace edm