Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // File: CFWriter.cc
0002 // Description:  see CFWriter.h
0003 // Author:  Emilia Becheva, LLR Palaiseau
0004 //
0005 //--------------------------------------------
0006 
0007 #include "SimGeneral/MixingModule/plugins/CFWriter.h"
0008 
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/Utilities/interface/EDMException.h"
0012 #include "FWCore/Utilities/interface/Algorithms.h"
0013 #include "FWCore/Utilities/interface/TypeID.h"
0014 #include "FWCore/ServiceRegistry/interface/Service.h"
0015 #include "DataFormats/Common/interface/Handle.h"
0016 #include "DataFormats/Provenance/interface/Provenance.h"
0017 #include "DataFormats/Provenance/interface/BranchDescription.h"
0018 #include "FWCore/Framework/interface/TriggerNamesService.h"
0019 
0020 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
0021 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0022 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0023 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
0024 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0025 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0026 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0027 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0028 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0029 
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/EventSetup.h"
0032 
0033 namespace edm {
0034   CFWriter::CFWriter(const edm::ParameterSet& iConfig)
0035       : flagSimTrack_(false),
0036         flagSimVertex_(false),
0037         flagHepMCProduct_(false),
0038         flagPCaloHit_(false),
0039         flagPSimHit_(false) {
0040     //register your products
0041     ParameterSet ps = iConfig.getParameter<ParameterSet>("mixObjects");
0042     std::vector<std::string> names = ps.getParameterNames();
0043     for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
0044       ParameterSet pset = ps.getParameter<ParameterSet>((*it));
0045       if (!pset.exists("type"))
0046         continue;  //to allow replacement by empty pset
0047 
0048       std::string object = pset.getParameter<std::string>("type");
0049       std::vector<InputTag> tags = pset.getParameter<std::vector<InputTag>>("input");
0050 
0051       //SimTracks
0052       if (object == "SimTrack") {
0053         flagSimTrack_ = true;
0054 
0055         InputTag tag;
0056         if (!tags.empty())
0057           tag = tags[0];
0058         std::string label;
0059         branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(), std::string(""), tag, label);
0060         produces<PCrossingFrame<SimTrack>>(label);
0061         consumes<std::vector<SimTrack>>(tag);
0062         LogInfo("MixingModule") << "Add PCrossingFrame<SimTrack> " << object << "s with InputTag= " << tag.encode()
0063                                 << ", label will be " << label;
0064 
0065       }
0066       //SimVertices
0067       else if (object == "SimVertex") {
0068         flagSimVertex_ = true;
0069 
0070         InputTag tag;
0071         if (!tags.empty())
0072           tag = tags[0];
0073         std::string label;
0074         branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(), std::string(""), tag, label);
0075         produces<PCrossingFrame<SimVertex>>(label);
0076         consumes<std::vector<SimVertex>>(tag);
0077         LogInfo("MixingModule") << "Add SimVertexContainer " << object << "s with InputTag= " << tag.encode()
0078                                 << ", label will be " << label;
0079 
0080       }
0081       // PCaloHit
0082       else if (object == "PCaloHit") {
0083         flagPCaloHit_ = true;
0084 
0085         std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
0086         for (unsigned int ii = 0; ii < subdets.size(); ii++) {
0087           InputTag tag;
0088           if (tags.size() == 1)
0089             tag = tags[0];
0090           else if (tags.size() > 1)
0091             tag = tags[ii];
0092           std::string label;
0093 
0094           branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(), subdets[ii], tag, label);
0095           produces<PCrossingFrame<PCaloHit>>(label);
0096           consumes<std::vector<PCaloHit>>(tag);
0097           LogInfo("MixingModule") << "Add PCrossingFrame<PCaloHit> " << object << "s with InputTag= " << tag.encode()
0098                                   << ", label will be " << label;
0099 
0100           // fill table with labels
0101           labCaloHit.push_back(label);
0102         }
0103       }
0104 
0105       // PSimHit
0106       else if (object == "PSimHit") {
0107         flagPSimHit_ = true;
0108 
0109         std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
0110         for (unsigned int ii = 0; ii < subdets.size(); ii++) {
0111           InputTag tag;
0112           if (tags.size() == 1)
0113             tag = tags[0];
0114 
0115           else if (tags.size() > 1)
0116             tag = tags[ii];
0117           std::string label;
0118 
0119           branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(), subdets[ii], tag, label);
0120           produces<PCrossingFrame<PSimHit>>(label);
0121           consumes<std::vector<PSimHit>>(tag);
0122           LogInfo("MixingModule") << "Add PSimHitContainer " << object << "s with InputTag= " << tag.encode()
0123                                   << ", label will be " << label;
0124 
0125           // fill table with labels
0126           labSimHit.push_back(label);
0127         }  //end for
0128       }
0129 
0130       // HepMCProduct
0131       else if (object == "HepMCProduct") {
0132         flagHepMCProduct_ = true;
0133 
0134         InputTag tag;
0135         if (!tags.empty())
0136           tag = tags[0];
0137         std::string label;
0138 
0139         branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(), std::string(""), tag, label);
0140         produces<PCrossingFrame<edm::HepMCProduct>>(label);
0141         consumes<HepMCProduct>(tag);
0142         LogInfo("MixingModule") << "Add HepMCProduct " << object << "s with InputTag= " << tag.encode()
0143                                 << ", label will be " << label;
0144       } else
0145         LogWarning("MixingModule") << "You did not mix a type of object(" << object << ").";
0146 
0147     }  //end for
0148   }
0149 
0150   void CFWriter::beginRun(const edm::Run& run, edm::EventSetup const& setup) {}
0151 
0152   CFWriter::~CFWriter() {}
0153 
0154   void CFWriter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0155     if (flagSimTrack_) {
0156       bool gotTracks;
0157       edm::Handle<CrossingFrame<SimTrack>> cf_simtrack;
0158       gotTracks = iEvent.getByLabel("mix", "g4SimHits", cf_simtrack);
0159 
0160       if (gotTracks) {
0161         PCrossingFrame<SimTrack>* PCFbis = new PCrossingFrame<SimTrack>(*cf_simtrack.product());
0162         std::unique_ptr<PCrossingFrame<SimTrack>> pOutTrack(PCFbis);
0163         iEvent.put(std::move(pOutTrack), "g4SimHits");
0164       } else {
0165         LogInfo("MixingModule") << " Please, check if the object <SimTrack> has been mixed by the MixingModule!";
0166       }
0167     }  //end if flagSimTrack_
0168 
0169     //SimVertex
0170     if (flagSimVertex_) {
0171       bool gotSimVertex;
0172       edm::Handle<CrossingFrame<SimVertex>> cf_simvtx;
0173       gotSimVertex = iEvent.getByLabel("mix", "g4SimHits", cf_simvtx);
0174 
0175       if (gotSimVertex) {
0176         PCrossingFrame<SimVertex>* PCFvtx = new PCrossingFrame<SimVertex>(*cf_simvtx.product());
0177         std::unique_ptr<PCrossingFrame<SimVertex>> pOutVertex(PCFvtx);
0178         iEvent.put(std::move(pOutVertex), "g4SimHits");
0179       } else {
0180         LogInfo("MixingModule") << " Please, check if the object <SimVertex> has been mixed by the MixingModule!";
0181       }
0182     }
0183 
0184     // PCaloHit
0185     if (flagPCaloHit_) {
0186       for (unsigned int ii = 0; ii < labCaloHit.size(); ii++) {
0187         bool gotPCaloHit;
0188         edm::Handle<CrossingFrame<PCaloHit>> cf_calohit;
0189         gotPCaloHit = iEvent.getByLabel("mix", labCaloHit[ii], cf_calohit);
0190 
0191         if (gotPCaloHit) {
0192           PCrossingFrame<PCaloHit>* PCFPhCaloHit = new PCrossingFrame<PCaloHit>(*cf_calohit.product());
0193           std::unique_ptr<PCrossingFrame<PCaloHit>> pOutHCalo(PCFPhCaloHit);
0194           iEvent.put(std::move(pOutHCalo), labCaloHit[ii]);
0195         } else {
0196           LogInfo("MixingModule") << " Please, check if the object <PCaloHit> " << labCaloHit[ii]
0197                                   << " has been mixed by the MixingModule!";
0198         }
0199       }
0200     }
0201 
0202     if (flagPSimHit_) {
0203       for (unsigned int ii = 0; ii < labSimHit.size(); ii++) {
0204         bool gotPSimHit;
0205         edm::Handle<CrossingFrame<PSimHit>> cf_simhit;
0206         gotPSimHit = iEvent.getByLabel("mix", labSimHit[ii], cf_simhit);
0207 
0208         if (gotPSimHit) {
0209           PCrossingFrame<PSimHit>* PCFSimHit = new PCrossingFrame<PSimHit>(*cf_simhit.product());
0210           std::unique_ptr<PCrossingFrame<PSimHit>> pOutSimHit(PCFSimHit);
0211           iEvent.put(std::move(pOutSimHit), labSimHit[ii]);
0212         } else {
0213           LogInfo("MixingModule") << " Please, check if the object <PSimHit> " << labSimHit[ii]
0214                                   << " has been mixed by the MixingModule!";
0215         }
0216       }
0217     }
0218 
0219     //HepMCProduct
0220     if (flagHepMCProduct_) {
0221       bool gotHepMCProduct;
0222       edm::Handle<CrossingFrame<edm::HepMCProduct>> cf_hepmc;
0223       gotHepMCProduct = iEvent.getByLabel("mix", "generatorSmeared", cf_hepmc);
0224       if (gotHepMCProduct) {
0225         PCrossingFrame<edm::HepMCProduct>* PCFHepMC = new PCrossingFrame<edm::HepMCProduct>(*cf_hepmc.product());
0226         std::unique_ptr<PCrossingFrame<edm::HepMCProduct>> pOuthepmcpr(PCFHepMC);
0227         iEvent.put(std::move(pOuthepmcpr), "generator");
0228       } else {
0229         LogInfo("MixingModule") << " Please, check if the object <HepMCProduct> has been mixed by the MixingModule!";
0230       }
0231 
0232     }  // end if flagHepMCProduct_
0233   }
0234 
0235   void CFWriter::branchesActivate(const std::string& friendlyName,
0236                                   std::string subdet,
0237                                   InputTag& tag,
0238                                   std::string& label) {
0239     label = tag.label() + tag.instance();
0240     wantedBranches_.push_back(friendlyName + '_' + tag.label() + '_' + tag.instance());
0241 
0242     //if useCurrentProcessOnly, we have to change the input tag
0243     if (useCurrentProcessOnly_) {
0244       const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
0245       tag = InputTag(tag.label(), tag.instance(), processName);
0246     }
0247   }
0248 
0249 }  // namespace edm