CMS 3D CMS Logo

MaterialBudgetVolume.cc
Go to the documentation of this file.
5 
7 
15 
16 #include "G4LogicalVolumeStore.hh"
17 #include "G4Step.hh"
18 #include "G4Track.hh"
19 #include "G4TransportationManager.hh"
20 #include "DD4hep/Filter.h"
21 
22 #include <CLHEP/Units/GlobalPhysicalConstants.h>
23 #include <CLHEP/Units/SystemOfUnits.h>
24 
25 #include <cmath>
26 #include <iomanip>
27 #include <iostream>
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 #include <utility>
33 
34 //#define EDM_ML_DEBUG
35 
37  public Observer<const BeginOfRun*>,
38  public Observer<const BeginOfEvent*>,
39  public Observer<const BeginOfTrack*>,
40  public Observer<const G4Step*>,
41  public Observer<const EndOfTrack*> {
42 public:
44  MaterialBudgetVolume(const MaterialBudgetVolume&) = delete; // stop default
45  const MaterialBudgetVolume& operator=(const MaterialBudgetVolume&) = delete;
46  ~MaterialBudgetVolume() override {}
47 
48  void produce(edm::Event&, const edm::EventSetup&) override;
49 
50  struct MatInfo {
51  double stepL, radL, intL;
52  MatInfo(double s = 0, double r = 0, double l = 0) : stepL(s), radL(r), intL(l) {}
53  };
54 
55 private:
56  // observer classes
57  void update(const BeginOfRun* run) override;
58  void update(const BeginOfEvent* evt) override;
59  void update(const BeginOfTrack*) override;
60  void update(const EndOfTrack*) override;
61  void update(const G4Step* step) override;
62 
64  bool loadLV();
65  int findLV(const G4VTouchable*);
66  bool stopAfter(const G4Step*);
67 
68 private:
69  std::vector<std::string> lvNames_;
70  std::vector<int> lvLevel_;
72  double rMax_, zMax_;
73  bool init_;
74  std::map<int, std::pair<G4LogicalVolume*, int> > mapLV_;
75  std::vector<MatInfo> lengths_;
76  std::vector<MaterialInformation> store_;
77 };
78 
80  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("MaterialBudgetVolume");
81 
82  lvNames_ = m_p.getParameter<std::vector<std::string> >("lvNames");
83  lvLevel_ = m_p.getParameter<std::vector<int> >("lvLevels");
84  iaddLevel_ = (m_p.getParameter<bool>("useDD4hep")) ? 1 : 0;
85  rMax_ = m_p.getParameter<double>("rMax") * CLHEP::m;
86  zMax_ = m_p.getParameter<double>("zMax") * CLHEP::m;
87 
88  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: Studies Material budget for " << lvNames_.size()
89  << " volumes with addLevel " << iaddLevel_ << " and with rMax " << rMax_
90  << " mm; zMax " << zMax_ << " mm";
91  std::ostringstream st1;
92  for (unsigned int k = 0; k < lvNames_.size(); ++k)
93  st1 << " [" << k << "] " << lvNames_[k] << " at " << lvLevel_[k];
94  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: Volumes" << st1.str();
95 
96  produces<edm::MaterialInformationContainer>("MaterialInformation");
97 #ifdef EDM_ML_DEBUG
98  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: will produce MaterialInformationContainer";
99 #endif
100 }
101 
103  std::unique_ptr<edm::MaterialInformationContainer> matbg(new edm::MaterialInformationContainer);
104  endOfEvent(*matbg);
105  e.put(std::move(matbg), "MaterialInformation");
106 }
107 
109  init_ = loadLV();
110 
111 #ifdef EDM_ML_DEBUG
112  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Finds " << mapLV_.size()
113  << " logical volumes with return flag " << init_;
114 #endif
115 }
116 
118 #ifdef EDM_ML_DEBUG
119  int iev = (*evt)()->GetEventID();
120  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: =====> Begin event = " << iev << std::endl;
121 #endif
122  if (!init_)
123  init_ = loadLV();
124 
125  store_.clear();
126 }
127 
129  lengths_ = std::vector<MatInfo>(mapLV_.size());
130 
131 #ifdef EDM_ML_DEBUG
132  const G4Track* aTrack = (*trk)();
133  const G4ThreeVector& mom = aTrack->GetMomentum();
134  double theEnergy = aTrack->GetTotalEnergy();
135  int theID = static_cast<int>(aTrack->GetDefinition()->GetPDGEncoding());
136  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolumme: Track " << aTrack->GetTrackID() << " Code " << theID
137  << " Energy " << theEnergy / CLHEP::GeV << " GeV; Momentum " << mom / CLHEP::GeV
138  << " GeV";
139 #endif
140 }
141 
142 void MaterialBudgetVolume::update(const G4Step* aStep) {
143  G4Material* material = aStep->GetPreStepPoint()->GetMaterial();
144  double step = aStep->GetStepLength();
145  double radl = material->GetRadlen();
146  double intl = material->GetNuclearInterLength();
147  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
148  int index = findLV(touch);
149  if (index >= 0) {
150  lengths_[index].stepL += step;
151  lengths_[index].radL += (step / radl);
152  lengths_[index].intL += (step / intl);
153  }
154 #ifdef EDM_ML_DEBUG
155  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Step in "
156  << touch->GetVolume(0)->GetLogicalVolume()->GetName() << " Index " << index
157  << " Step " << step << " RadL " << step / radl << " IntL " << step / intl;
158 #endif
159 
160  //----- Stop tracking after selected position
161  if (stopAfter(aStep)) {
162  G4Track* track = aStep->GetTrack();
163  track->SetTrackStatus(fStopAndKill);
164  }
165 }
166 
168  const G4Track* aTrack = (*trk)();
169  int id = aTrack->GetTrackID();
170  double eta = aTrack->GetMomentumDirection().eta();
171  double phi = aTrack->GetMomentumDirection().phi();
172  for (unsigned int k = 0; k < lengths_.size(); ++k) {
173  MaterialInformation info(lvNames_[k], id, eta, phi, lengths_[k].stepL, lengths_[k].radL, lengths_[k].intL);
174  store_.emplace_back(info);
175 #ifdef EDM_ML_DEBUG
176  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Volume[" << k << "]: " << info;
177 #endif
178  }
179 }
180 
182 #ifdef EDM_ML_DEBUG
183  unsigned int kount(0);
184 #endif
185  for (const auto& element : store_) {
186  MaterialInformation info(element.vname(),
187  element.id(),
188  element.trackEta(),
189  element.trackPhi(),
190  element.stepLength(),
191  element.radiationLength(),
192  element.interactionLength());
193  matbg.push_back(info);
194 #ifdef EDM_ML_DEBUG
195  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume:: Info[" << kount << "] " << info;
196  ++kount;
197 #endif
198  }
199 }
200 
202  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
203  bool flag(false);
204  if (lvs != nullptr) {
205  std::vector<G4LogicalVolume*>::const_iterator lvcite;
206  for (unsigned int i = 0; i < lvNames_.size(); i++) {
207  G4LogicalVolume* lv = nullptr;
209  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
210  std::string namx(dd4hep::dd::noNamespace((*lvcite)->GetName()));
211  if (namx == name) {
212  lv = (*lvcite);
213  break;
214  }
215  }
216  if (lv != nullptr)
217  mapLV_[i] = std::make_pair(lv, (lvLevel_[i] + iaddLevel_));
218  }
219  flag = true;
220 #ifdef EDM_ML_DEBUG
221  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Finds " << mapLV_.size() << " logical volumes";
222  unsigned int k(0);
223  for (const auto& lvs : mapLV_) {
224  edm::LogVerbatim("MaterialBudget") << "Entry[" << k << "] " << lvs.first << ": (" << (lvs.second).first << ", "
225  << (lvs.second).second << ") : " << lvNames_[lvs.first];
226  ++k;
227  }
228 #endif
229  }
230  return flag;
231 }
232 
233 int MaterialBudgetVolume::findLV(const G4VTouchable* touch) {
234  int level(-1);
235  int levels = ((touch->GetHistoryDepth()) + 1);
236  for (const auto& lvs : mapLV_) {
237  if ((lvs.second).second <= levels) {
238  int ii = levels - (lvs.second).second;
239  if ((touch->GetVolume(ii)->GetLogicalVolume()) == (lvs.second).first) {
240  level = lvs.first;
241  break;
242  }
243  }
244  }
245 #ifdef EDM_ML_DEBUG
246  if (level < 0) {
247  edm::LogVerbatim("MaterialBudget") << "findLV: Gets " << level << " from " << levels << " levels in touchables";
248  for (int i = 0; i < levels; ++i)
249  edm::LogVerbatim("MaterialBudget") << "[" << (levels - i) << "] " << touch->GetVolume(i)->GetLogicalVolume()
250  << " : " << touch->GetVolume(i)->GetLogicalVolume()->GetName();
251  }
252 #endif
253  return level;
254 }
255 
256 bool MaterialBudgetVolume::stopAfter(const G4Step* aStep) {
257  bool flag(false);
258  if ((rMax_ > 0.0) && (zMax_ > 0.0)) {
259  G4ThreeVector hitPoint = aStep->GetPreStepPoint()->GetPosition();
260  double rr = hitPoint.perp();
261  double zz = std::abs(hitPoint.z());
262 
263  if (rr > rMax_ || zz > zMax_) {
264 #ifdef EDM_ML_DEBUG
265  edm::LogVerbatim("MaterialBudget") << " MaterialBudgetHcal::StopAfter R = " << rr << " and Z = " << zz;
266 #endif
267  flag = true;
268  }
269  }
270  return flag;
271 }
272 
276 
Log< level::Info, true > LogVerbatim
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
static const TGPicture * info(bool iBackgroundIsBlack)
Trktree trk
Definition: Trktree.cc:2
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
MaterialBudgetVolume(const edm::ParameterSet &p)
std::vector< MatInfo > lengths_
void produce(edm::Event &, const edm::EventSetup &) override
std::map< int, std::pair< G4LogicalVolume *, int > > mapLV_
U second(std::pair< T, U > const &p)
std::vector< MaterialInformation > store_
std::vector< std::string > lvNames_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int findLV(const G4VTouchable *)
std::vector< MaterialInformation > MaterialInformationContainer
const MaterialBudgetVolume & operator=(const MaterialBudgetVolume &)=delete
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
ii
Definition: cuy.py:589
std::vector< int > lvLevel_
void endOfEvent(edm::MaterialInformationContainer &matbg)
bool stopAfter(const G4Step *)
MatInfo(double s=0, double r=0, double l=0)
step
Definition: StallMonitor.cc:83
def move(src, dest)
Definition: eostools.py:511