CMS 3D CMS Logo

SimG4FluxProducer.cc
Go to the documentation of this file.
1 #include "SimG4FluxProducer.h"
3 
4 #include "G4Track.hh"
5 #include "G4TouchableHistory.hh"
6 #include "G4TransportationManager.hh"
7 #include "CLHEP/Units/GlobalSystemOfUnits.h"
8 #include "CLHEP/Units/GlobalPhysicalConstants.h"
9 
10 #include <cmath>
11 #include <iostream>
12 #include <iomanip>
13 #include <memory>
14 #include <utility>
15 
16 //#define EDM_ML_DEBUG
17 
19 
20  edm::ParameterSet m_FP = p.getParameter<edm::ParameterSet>("SimG4FluxProducer");
21  LVNames_ = m_FP.getUntrackedParameter<std::vector<std::string> >("LVNames");
22  LVTypes_ = m_FP.getUntrackedParameter<std::vector<int> >("LVTypes");
23 
24  for (unsigned int k=0; k<LVNames_.size(); ++k) {
25  produces<ParticleFlux>(Form("%sParticleFlux",LVNames_[k].c_str()));
26 #ifdef EDM_ML_DEBUG
27  std::cout << "Collection name[" << k << "] ParticleFlux" << LVNames_[k]
28  << " and type " << LVTypes_[k] << std::endl;
29 #endif
30  }
31 }
32 
34 
36 
37  for (unsigned int k=0; k<LVNames_.size(); ++k) {
38  std::unique_ptr<ParticleFlux> pflux(new ParticleFlux);
39  endOfEvent(*pflux, k);
40  std::string name = LVNames_[k] + "ParticleFlux";
41  e.put(std::move(pflux),name);
42  }
43 }
44 
46 
47  topPV_ = getTopPV();
48  if (topPV_ == nullptr) {
49  edm::LogWarning("SimG4FluxProducer") << "Cannot find top level volume\n";
50  } else {
51  init_ = true;
52  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
53  for (auto lvcite : *lvs) {
54  findLV(lvcite);
55  }
56 
57 #ifdef EDM_ML_DEBUG
58  std::cout << "SimG4FluxProducer::Finds " << mapLV_.size() << " logical volumes\n";
59  unsigned int k(0);
60  for (const auto& lvs : mapLV_) {
61  std::cout << "Entry[" << k << "] " << lvs.first << ": ("
62  << (lvs.second).first << ", " << (lvs.second).second << ")\n";
63  ++k;
64  }
65 #endif
66  }
67 }
68 
70 
71  int iev = (*evt)()->GetEventID();
72  edm::LogInfo("ValidHGCal") << "SimG4FluxProducer: =====> Begin event = "
73  << iev << std::endl;
74  ++count_;
75  store_.clear();
76 }
77 
78 void SimG4FluxProducer::update(const G4Step * aStep) {
79 
80  if (aStep != nullptr) {
81 
82  G4TouchableHistory* touchable = (G4TouchableHistory*)aStep->GetPreStepPoint()->GetTouchable();
83  G4LogicalVolume* plv = (G4LogicalVolume*)touchable->GetVolume()->GetLogicalVolume();
84  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
85 // std::cout << plv->GetName() << " Flag " << (it != mapLV_.end()) << " step " << aStep->IsFirstStepInVolume() << ":" << aStep->IsLastStepInVolume() << std::endl;
86  if (it != mapLV_.end()) {
87  int type = LVTypes_[(it->second).first];
88  if ((type == 0 && aStep->IsFirstStepInVolume()) ||
89  (type == 1 && aStep->IsLastStepInVolume())) {
90  unsigned int copy = (unsigned int)(touchable->GetReplicaNumber(0));
91  std::pair<G4LogicalVolume*,unsigned int> key(plv,copy);
92  auto itr = store_.find(key);
93  if (itr == store_.end()) {
94  store_[key] = ParticleFlux((it->second).second,copy);
95  itr = store_.find(key);
96  }
97  G4Track* track = aStep->GetTrack();
98  int pdgid = track->GetDefinition()->GetPDGEncoding();
99  int vxtyp = (track->GetCreatorProcess() == nullptr) ? 0 : 1;
100  double time = (aStep->GetPostStepPoint()->GetGlobalTime());
101  const double mmTocm(0.1), MeVToGeV(0.001);
102  ParticleFlux::flux flx(pdgid,vxtyp,time);
103  flx.vertex = math::GlobalPoint(mmTocm*track->GetVertexPosition().x(),
104  mmTocm*track->GetVertexPosition().y(),
105  mmTocm*track->GetVertexPosition().z());
106  flx.hitPoint = math::GlobalPoint(mmTocm*track->GetPosition().x(),
107  mmTocm*track->GetPosition().y(),
108  mmTocm*track->GetPosition().z());
109  flx.momentum = math::GlobalVector(MeVToGeV*track->GetMomentum().x(),
110  MeVToGeV*track->GetMomentum().y(),
111  MeVToGeV*track->GetMomentum().z());
112  (itr->second).addFlux(flx);
113 #ifdef EDM_ML_DEBUG
114  std::cout << "SimG4FluxProducer: Element " << (it->second).first << ":"
115  << (it->second).second << ":" << copy << " ID " << pdgid
116  << " VxType " << vxtyp << " TOF " << time << " Hit Point "
117  << flx.hitPoint << " p " << flx.momentum << " Vertex "
118  << flx.vertex << std::endl;
119 #endif
120  }//if(Step ok)
121  }//if( it != map.end() )
122  }//if (aStep != NULL)
123 
124 }
125 
126 void SimG4FluxProducer::endOfEvent(ParticleFlux& flux, unsigned int k) {
127 
128  bool done(false);
129  for (const auto& element : store_) {
130  G4LogicalVolume* lv = (element.first).first;
131  auto it = mapLV_.find(lv);
132  if (it != mapLV_.end()) {
133  if ((it->second).first == k) {
134  flux = element.second;
135  done = true;
136 #ifdef EDM_ML_DEBUG
137  std::cout << "SimG4FluxProducer[" << k << "] Flux " << flux.getName()
138  << ":" << flux.getId() << " with " << flux.getComponents()
139  << " elements" << std::endl;
140  std::vector<ParticleFlux::flux> fluxes = flux.getFlux() ;
141  unsigned int k(0);
142  for (auto element : fluxes) {
143  std::cout << "Flux[" << k << "] PDGId " << element.pdgId << " VT "
144  << element.vxType << " ToF " << element.tof << " Vertex "
145  << element.vertex << " Hit " << element.hitPoint << " p "
146  << element.momentum << std::endl;
147  ++k;
148  }
149 #endif
150  }
151  }
152  if (done) break;
153  }
154 }
155 
156 G4VPhysicalVolume * SimG4FluxProducer::getTopPV() {
157  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
158 }
159 
160 std::map<G4LogicalVolume*,std::pair<unsigned int,std::string>>::iterator SimG4FluxProducer::findLV(G4LogicalVolume * plv) {
161  auto itr = mapLV_.find(plv);
162  if (itr == mapLV_.end()) {
163  std::string name = plv->GetName();
164  for (unsigned int k=0; k<LVNames_.size(); ++k) {
165  if (name.find(LVNames_[k]) != std::string::npos) {
166  mapLV_[plv] = std::pair<unsigned int,std::string>(k,name);
167  itr = mapLV_.find(plv);
168  break;
169  }
170  }
171  }
172  return itr;
173 }
174 
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
SimG4FluxProducer(const edm::ParameterSet &p)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
std::map< std::pair< G4LogicalVolume *, unsigned int >, ParticleFlux > store_
~SimG4FluxProducer() override
def copy(args, dbName)
void produce(edm::Event &, const edm::EventSetup &) override
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
G4VPhysicalVolume * topPV_
std::vector< int > LVTypes_
unsigned int getComponents() const
Definition: ParticleFlux.h:28
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator findLV(G4LogicalVolume *plv)
int getId() const
Definition: ParticleFlux.h:27
math::GlobalVector momentum
Definition: ParticleFlux.h:22
math::GlobalPoint vertex
Definition: ParticleFlux.h:21
G4VPhysicalVolume * getTopPV()
std::vector< std::string > LVNames_
int k[5][pyjets_maxn]
void endOfEvent(ParticleFlux &pflx, unsigned int k)
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalVector
vector in glovbal coordinate system
Definition: Vector3D.h:27
std::string const & getName() const
Definition: ParticleFlux.h:26
math::GlobalPoint hitPoint
Definition: ParticleFlux.h:21
std::vector< ParticleFlux::flux > getFlux() const &
Definition: ParticleFlux.h:29
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalPoint
point in global coordinate system
Definition: Point3D.h:17
def move(src, dest)
Definition: eostools.py:510