Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:36

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 "FWCore/Framework/interface/ConstProductRegistry.h"
0052 #include "FWCore/ServiceRegistry/interface/Service.h"
0053 #include "DataFormats/Common/interface/Handle.h"
0054 #include "DataFormats/Provenance/interface/Provenance.h"
0055 #include "DataFormats/Provenance/interface/BranchDescription.h"
0056 
0057 #include <string>
0058 
0059 using namespace std;
0060 
0061 //
0062 // class decleration
0063 //
0064 
0065 namespace edm {
0066 
0067   class HiMixingModule;
0068 
0069   class HiMixingWorkerBase {
0070   public:
0071     explicit HiMixingWorkerBase() { ; }
0072 
0073     HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label)
0074         : object_(object), tags_(tags), label_(label) {
0075       ;
0076     }
0077     virtual ~HiMixingWorkerBase() { ; }
0078     //   virtual void put(edm::Event &e) = 0;
0079     virtual void addSignals(edm::Event& e) = 0;
0080 
0081     std::string object_;
0082     std::vector<InputTag> tags_;
0083     std::string label_;
0084   };
0085 
0086   template <class T>
0087   class HiMixingWorker : public HiMixingWorkerBase {
0088   public:
0089     HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label)
0090         : HiMixingWorkerBase(object, tags, label) {
0091       ;
0092     }
0093     ~HiMixingWorker() override { ; }
0094     void addSignals(edm::Event& e) override {
0095       std::vector<Handle<std::vector<T> > > handles;
0096       bool get = true;
0097       for (size_t itag = 0; itag < tags_.size(); ++itag) {
0098         LogInfo("HiEmbedding") << "itag " << itag;
0099         LogInfo("HiEmbedding") << "label " << tags_[itag].label();
0100         LogInfo("HiEmbedding") << "instance " << tags_[itag].instance();
0101         Handle<std::vector<T> > hand;
0102         handles.push_back(hand);
0103         get = get && e.getByLabel(tags_[itag], handles[itag]);
0104         if (!get)
0105           LogWarning("Product inconsistency")
0106               << "One of the sub-events is missing the product with type " << object_ << ", instance "
0107               << tags_[itag].instance() << " whereas the other one is fine.";
0108       }
0109 
0110       if (get) {
0111         std::unique_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>());
0112         crFrame->addSignals(handles[0].product(), e.id());
0113         for (size_t itag = 1; itag < tags_.size(); ++itag) {
0114           std::vector<T>* product = const_cast<std::vector<T>*>(handles[itag].product());
0115           EncodedEventId id(0, itag);
0116           for (auto& item : *product) {
0117             item.setEventId(id);
0118           }
0119           crFrame->addPileups(*product);
0120         }
0121         e.put(std::move(crFrame), label_);
0122       }
0123     }
0124   };
0125 
0126   template <>
0127   void HiMixingWorker<HepMCProduct>::addSignals(edm::Event& e) {
0128     std::vector<Handle<HepMCProduct> > handles;
0129     bool get = true;
0130     for (size_t itag = 0; itag < tags_.size(); ++itag) {
0131       Handle<HepMCProduct> hand;
0132       handles.push_back(hand);
0133       get = get && e.getByLabel(tags_[itag], handles[itag]);
0134       if (!get)
0135         LogWarning("Product inconsistency")
0136             << "One of the sub-events is missing the product with type " << object_ << ", instance "
0137             << tags_[itag].instance() << " whereas the other one is fine.";
0138     }
0139 
0140     if (get) {
0141       std::unique_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>());
0142       crFrame->addSignals(handles[0].product(), e.id());
0143       for (size_t itag = 1; itag < tags_.size(); ++itag) {
0144         HepMCProduct* product = const_cast<HepMCProduct*>(handles[itag].product());
0145         crFrame->addPileups(*product);
0146       }
0147       e.put(std::move(crFrame), label_);
0148     }
0149   }
0150 
0151   class HiMixingModule : public edm::stream::EDProducer<> {
0152   public:
0153     explicit HiMixingModule(const edm::ParameterSet&);
0154     ~HiMixingModule() override;
0155 
0156   private:
0157     //virtual void beginJob() override {}
0158     void produce(edm::Event&, const edm::EventSetup&) override;
0159     //virtual void endJob() override {}
0160     bool verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label);
0161     // ----------member data ---------------------------
0162 
0163     std::vector<HiMixingWorkerBase*> workers_;
0164   };
0165 
0166   //
0167   // constants, enums and typedefs
0168   //
0169 
0170   //
0171   // static data member definitions
0172   //
0173 
0174   //
0175   // constructors and destructor
0176   //
0177   HiMixingModule::HiMixingModule(const edm::ParameterSet& pset) {
0178     ParameterSet ps = pset.getParameter<ParameterSet>("mixObjects");
0179     std::vector<std::string> names = ps.getParameterNames();
0180     std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
0181     std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
0182 
0183     if (simtags.size() != gentags.size())
0184       LogError("MixingInput") << "Generator and Simulation input lists are not matching each other" << endl;
0185 
0186     for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
0187       ParameterSet pstag = ps.getParameter<ParameterSet>((*it));
0188       if (!pstag.exists("type"))
0189         continue;  //to allow replacement by empty pset
0190       std::string object = pstag.getParameter<std::string>("type");
0191       std::vector<InputTag> tags = pstag.getParameter<std::vector<InputTag> >("input");
0192 
0193       std::string signal;
0194       for (size_t itag = 0; itag < tags.size(); ++itag) {
0195         InputTag tag = tags[itag];
0196         std::vector<InputTag> inputs;
0197 
0198         for (size_t input = 0; input < simtags.size(); ++input) {
0199           if (object == "HepMCProduct")
0200             signal = gentags[input];
0201           else
0202             signal = simtags[input];
0203           inputs.push_back(InputTag(signal, tag.instance()));
0204         }
0205 
0206         std::string label = tag.label() + tag.instance();
0207         //   verifyRegistry(object,std::string(""),tag,label);
0208         if (object == "HepMCProduct") {
0209           workers_.push_back(new HiMixingWorker<HepMCProduct>(object, inputs, label));
0210           produces<CrossingFrame<HepMCProduct> >(label);
0211           consumes<HepMCProduct>(tag);
0212         } else if (object == "SimTrack") {
0213           workers_.push_back(new HiMixingWorker<SimTrack>(object, inputs, label));
0214           produces<CrossingFrame<SimTrack> >(label);
0215           consumes<std::vector<SimTrack> >(tag);
0216         } else if (object == "SimVertex") {
0217           workers_.push_back(new HiMixingWorker<SimVertex>(object, inputs, label));
0218           produces<CrossingFrame<SimVertex> >(label);
0219           consumes<std::vector<SimVertex> >(tag);
0220         } else if (object == "PSimHit") {
0221           workers_.push_back(new HiMixingWorker<PSimHit>(object, inputs, label));
0222           produces<CrossingFrame<PSimHit> >(label);
0223           consumes<std::vector<PSimHit> >(tag);
0224         } else if (object == "PCaloHit") {
0225           workers_.push_back(new HiMixingWorker<PCaloHit>(object, inputs, label));
0226           produces<CrossingFrame<PCaloHit> >(label);
0227           consumes<std::vector<PCaloHit> >(tag);
0228         } else
0229           LogInfo("Error") << "What the hell is this object?!";
0230 
0231         LogInfo("HiMixingModule") << "Will mix " << object << "s with InputTag= " << tag.encode() << ", label will be "
0232                                   << label;
0233       }
0234     }
0235 
0236     produces<PileupMixingContent>();
0237   }
0238 
0239   HiMixingModule::~HiMixingModule() {
0240     // do anything here that needs to be done at desctruction time
0241     // (e.g. close files, deallocate resources etc.)
0242   }
0243 
0244   //
0245   // member functions
0246   //
0247 
0248   // ------------ method called to produce the data  ------------
0249   void HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0250     using namespace edm;
0251 
0252     for (size_t i = 0; i < workers_.size(); ++i) {
0253       (workers_[i])->addSignals(iEvent);
0254     }
0255 
0256     std::unique_ptr<PileupMixingContent> PileupMixing_ = std::make_unique<PileupMixingContent>();
0257     iEvent.put(std::move(PileupMixing_));
0258   }
0259 
0260   bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label) {
0261     // verify that the given product exists in the product registry
0262     // and create the label to be given to the CrossingFrame
0263 
0264     edm::Service<edm::ConstProductRegistry> reg;
0265     // Loop over provenance of products in registry.
0266     std::string lookfor;
0267     if (object == "HepMCProduct")
0268       lookfor = "edm::" + object;  //exception for HepMCProduct
0269     else if (object == "edm::HepMCProduct")
0270       lookfor = object;
0271     else
0272       lookfor = "std::vector<" + object + ">";
0273     bool found = false;
0274     for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
0275          it != reg->productList().end();
0276          ++it) {
0277       // See FWCore/Framework/interface/BranchDescription.h
0278       // BranchDescription contains all the information for the product.
0279       edm::BranchDescription desc = it->second;
0280       if (desc.className() == lookfor && desc.moduleLabel() == tag.label() &&
0281           desc.productInstanceName() == tag.instance()) {
0282         label = desc.moduleLabel() + desc.productInstanceName();
0283         found = true;
0284         /*
0285      wantedBranches_.push_back(desc.friendlyClassName() + '_' +
0286                    desc.moduleLabel() + '_' +
0287                    desc.productInstanceName());
0288      */
0289         break;
0290       }
0291     }
0292     if (!found) {
0293       LogWarning("MixingModule") << "!!!!!!!!!Could not find in registry requested object: " << object << " with "
0294                                  << tag << ".\nWill NOT be considered for mixing!!!!!!!!!";
0295       return false;
0296     }
0297 
0298     return true;
0299   }
0300 
0301   //define this as a plug-in
0302   DEFINE_FWK_MODULE(HiMixingModule);
0303 
0304 }  // namespace edm