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