Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:39

0001 // system include files
0002 #include <vector>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 #include "FWCore/Utilities/interface/EDGetToken.h"
0009 #include "DataFormats/Common/interface/ValidHandle.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 
0015 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0016 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0017 
0018 #include "SimDataFormats/Track/interface/SimTrack.h"
0019 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0020 #include "SimDataFormats/Vertex/interface/SimVertex.h"
0021 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0022 
0023 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0024 #include "HepMC/GenEvent.h"
0025 
0026 class SimTrackSimVertexDumper : public edm::one::EDAnalyzer<> {
0027 public:
0028   explicit SimTrackSimVertexDumper(const edm::ParameterSet&);
0029   ~SimTrackSimVertexDumper() override{};
0030 
0031   void analyze(const edm::Event&, const edm::EventSetup&) override;
0032   void beginJob() override{};
0033   void endJob() override{};
0034 
0035   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0036 
0037 private:
0038   edm::EDGetTokenT<edm::HepMCProduct> hepmcToken_;
0039   edm::EDGetTokenT<edm::SimTrackContainer> simTrackToken_;
0040   edm::EDGetTokenT<edm::SimVertexContainer> simVertexToken_;
0041   bool dumpHepMC_;
0042 };
0043 
0044 SimTrackSimVertexDumper::SimTrackSimVertexDumper(const edm::ParameterSet& iConfig)
0045     : hepmcToken_(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("moduleLabelHepMC"))),
0046       simTrackToken_(consumes<edm::SimTrackContainer>(iConfig.getParameter<edm::InputTag>("moduleLabelTk"))),
0047       simVertexToken_(consumes<edm::SimVertexContainer>(iConfig.getParameter<edm::InputTag>("moduleLabelVtx"))),
0048       dumpHepMC_(iConfig.getUntrackedParameter<bool>("dumpHepMC")) {}
0049 
0050 //
0051 // member functions
0052 //
0053 
0054 // ------------ method called to produce the data  ------------
0055 void SimTrackSimVertexDumper::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0056   using namespace HepMC;
0057 
0058   std::vector<SimTrack> theSimTracks;
0059   std::vector<SimVertex> theSimVertexes;
0060 
0061   auto MCEvt = edm::makeValid(iEvent.getHandle(hepmcToken_));
0062   const HepMC::GenEvent* evt = MCEvt->GetEvent();
0063 
0064   auto SimTk = edm::makeValid(iEvent.getHandle(simTrackToken_));
0065   auto SimVtx = edm::makeValid(iEvent.getHandle(simVertexToken_));
0066 
0067   theSimTracks.insert(theSimTracks.end(), SimTk->begin(), SimTk->end());
0068   theSimVertexes.insert(theSimVertexes.end(), SimVtx->begin(), SimVtx->end());
0069 
0070   edm::LogPrint("DumpTkVtx") << "\n SimVertex / SimTrack structure dump \n";
0071   edm::LogPrint("DumpTkVtx") << " SimVertex in the event = " << theSimVertexes.size();
0072   edm::LogPrint("DumpTkVtx") << " SimTracks in the event = " << theSimTracks.size();
0073   edm::LogPrint("DumpTkVtx") << "\n";
0074   for (unsigned int isimvtx = 0; isimvtx < theSimVertexes.size(); isimvtx++) {
0075     edm::LogPrint("DumpTkVtx") << "SimVertex " << isimvtx << " = " << theSimVertexes[isimvtx] << "\n";
0076     for (unsigned int isimtk = 0; isimtk < theSimTracks.size(); isimtk++) {
0077       if (theSimTracks[isimtk].vertIndex() >= 0 && std::abs(theSimTracks[isimtk].vertIndex()) == (int)isimvtx) {
0078         edm::LogPrint("DumpTkVtx") << "  SimTrack " << isimtk << " = " << theSimTracks[isimtk]
0079                                    << " Track Id = " << theSimTracks[isimtk].trackId();
0080 
0081         // for debugging purposes
0082         if (dumpHepMC_) {
0083           if (theSimTracks[isimtk].genpartIndex() != -1) {
0084             HepMC::GenParticle* part = evt->barcode_to_particle(theSimTracks[isimtk].genpartIndex());
0085             if (part) {
0086               edm::LogPrint("DumpTkVtx") << "  ---> Corresponding to HepMC particle " << *part;
0087             } else {
0088               edm::LogPrint("DumpTkVtx") << " ---> Corresponding HepMC particle to barcode "
0089                                          << theSimTracks[isimtk].genpartIndex() << " not in selected event ";
0090             }
0091           }
0092         }
0093       }
0094     }
0095     edm::LogPrint("DumpTkVtx") << "\n";
0096   }
0097 
0098   for (std::vector<SimTrack>::iterator isimtk = theSimTracks.begin(); isimtk != theSimTracks.end(); ++isimtk) {
0099     if (isimtk->noVertex()) {
0100       edm::LogPrint("DumpTkVtx") << "SimTrack without an associated Vertex = " << *isimtk;
0101     }
0102   }
0103 
0104   return;
0105 }
0106 
0107 void SimTrackSimVertexDumper::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0108   edm::ParameterSetDescription desc;
0109   desc.add<edm::InputTag>("moduleLabelHepMC", edm::InputTag("generatorSmeared"))
0110       ->setComment("Input generated HepMC event after vtx smearing");
0111   desc.add<edm::InputTag>("moduleLabelTk", edm::InputTag("g4SimHits"))
0112       ->setComment("Module for input SimTrack collection");
0113   desc.add<edm::InputTag>("moduleLabelVtx", edm::InputTag("g4SimHits"))
0114       ->setComment("Module for input SimVertex collection");
0115   desc.addUntracked<bool>("dumpHepMC", false);
0116   descriptions.add("simTrackSimVertexDumper", desc);
0117 }
0118 
0119 #include "FWCore/Framework/interface/MakerMacros.h"
0120 
0121 //define this as a plug-in
0122 DEFINE_FWK_MODULE(SimTrackSimVertexDumper);