Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-10 02:21:18

0001 // -*- C++ -*-
0002 //
0003 // Package:     Forward
0004 // Class  :     TotemTestGem
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:
0010 //         Created:  Tue May 16 10:14:34 CEST 2006
0011 //
0012 
0013 // system include files
0014 #include <cmath>
0015 #include <iomanip>
0016 #include <iostream>
0017 #include <memory>
0018 #include <string>
0019 #include <vector>
0020 
0021 // user include files
0022 
0023 #include "FWCore/Framework/interface/Event.h"
0024 #include "FWCore/Framework/interface/EventSetup.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0027 
0028 #include "SimDataFormats/Forward/interface/TotemTestHistoClass.h"
0029 
0030 #include "SimG4CMS/Forward/interface/TotemG4HitCollection.h"
0031 #include "SimG4CMS/Forward/interface/TotemG4Hit.h"
0032 
0033 #include "SimG4Core/Notification/interface/Observer.h"
0034 #include "SimG4Core/Notification/interface/BeginOfJob.h"
0035 #include "SimG4Core/Notification/interface/BeginOfEvent.h"
0036 #include "SimG4Core/Notification/interface/EndOfEvent.h"
0037 #include "SimG4Core/Watcher/interface/SimProducer.h"
0038 
0039 #include "G4SDManager.hh"
0040 #include "G4Step.hh"
0041 #include "G4HCofThisEvent.hh"
0042 
0043 #include <CLHEP/Units/SystemOfUnits.h>
0044 #include <CLHEP/Units/GlobalPhysicalConstants.h>
0045 
0046 class TotemTestGem : public SimProducer, public Observer<const BeginOfEvent*>, public Observer<const EndOfEvent*> {
0047 public:
0048   TotemTestGem(const edm::ParameterSet& p);
0049   ~TotemTestGem() override;
0050 
0051   void produce(edm::Event&, const edm::EventSetup&) override;
0052 
0053 protected:
0054   // observer classes
0055   void update(const BeginOfEvent* evt) override;
0056   void update(const EndOfEvent* evt) override;
0057 
0058 private:
0059   void clear();
0060   void fillEvent(TotemTestHistoClass&);
0061 
0062   //Keep parameters and internal memory
0063   std::vector<std::string> names;
0064   int evtnum;
0065   std::vector<TotemG4Hit*> hits;
0066 };
0067 
0068 //
0069 // constructors and destructor
0070 //
0071 
0072 TotemTestGem::TotemTestGem(const edm::ParameterSet& p) {
0073   edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("TotemTestGem");
0074   names = m_Anal.getParameter<std::vector<std::string> >("Names");
0075 
0076   edm::LogVerbatim("ForwardSim")
0077       << "TotemTestGem:: Initialised as observer of begin of job, begin/end events and of G4step";
0078 }
0079 
0080 TotemTestGem::~TotemTestGem() {}
0081 
0082 //
0083 // member functions
0084 //
0085 
0086 void TotemTestGem::produce(edm::Event& e, const edm::EventSetup&) {
0087   std::unique_ptr<TotemTestHistoClass> product(new TotemTestHistoClass);
0088   fillEvent(*product);
0089   e.put(std::move(product));
0090 }
0091 
0092 void TotemTestGem::update(const BeginOfEvent* evt) {
0093   int iev = (*evt)()->GetEventID();
0094   LogDebug("ForwardSim") << "TotemTestGem: Begin of event = " << iev;
0095   clear();
0096 }
0097 
0098 void TotemTestGem::update(const EndOfEvent* evt) {
0099   evtnum = (*evt)()->GetEventID();
0100   LogDebug("ForwardSim") << "TotemTestGem:: Fill event " << evtnum;
0101 
0102   // access to the G4 hit collections
0103   G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
0104 
0105   int nhit = 0;
0106   for (unsigned int in = 0; in < names.size(); in++) {
0107     int HCid = G4SDManager::GetSDMpointer()->GetCollectionID(names[in]);
0108     TotemG4HitCollection* theHC = (TotemG4HitCollection*)allHC->GetHC(HCid);
0109     LogDebug("ForwardSim") << "TotemTestGem :: Hit Collection for " << names[in] << " of ID " << HCid
0110                            << " is obtained at " << theHC;
0111 
0112     if (HCid >= 0 && theHC != nullptr) {
0113       int nentries = theHC->entries();
0114       LogDebug("ForwardSim") << "TotemTestGem :: " << names[in] << " with " << nentries << " entries";
0115       for (int ihit = 0; ihit < nentries; ihit++) {
0116         TotemG4Hit* aHit = (*theHC)[ihit];
0117         hits.push_back(aHit);
0118       }
0119       nhit += nentries;
0120     }
0121   }
0122 
0123   // Writing the data to the Tree
0124   LogDebug("ForwardSim") << "TotemTestGem:: --- after fillTree with " << nhit << " Hits";
0125 }
0126 
0127 void TotemTestGem::fillEvent(TotemTestHistoClass& product) {
0128   product.setEVT(evtnum);
0129 
0130   for (unsigned ihit = 0; ihit < hits.size(); ihit++) {
0131     TotemG4Hit* aHit = hits[ihit];
0132     int UID = aHit->getUnitID();
0133     int Ptype = aHit->getParticleType();
0134     int TID = aHit->getTrackID();
0135     int PID = aHit->getParentId();
0136     float ELoss = aHit->getEnergyLoss();
0137     float PABS = aHit->getPabs();
0138     float x = aHit->getEntry().x();
0139     float y = aHit->getEntry().y();
0140     float z = aHit->getEntry().z();
0141     float vx = aHit->getVx();
0142     float vy = aHit->getVy();
0143     float vz = aHit->getVz();
0144     product.fillHit(UID, Ptype, TID, PID, ELoss, PABS, vx, vy, vz, x, y, z);
0145   }
0146 }
0147 
0148 void TotemTestGem::clear() {
0149   evtnum = 0;
0150   hits.clear();
0151 }
0152 
0153 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
0154 #include "FWCore/PluginManager/interface/ModuleDef.h"
0155 
0156 DEFINE_SIMWATCHER(TotemTestGem);