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/GlobalSystemOfUnits.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 
67 private:
68  std::vector<std::string> lvNames_;
69  std::vector<int> lvLevel_;
71  bool init_;
72  std::map<int, std::pair<G4LogicalVolume*, int> > mapLV_;
73  std::vector<MatInfo> lengths_;
74  std::vector<MaterialInformation> store_;
75 };
76 
78  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("MaterialBudgetVolume");
79 
80  lvNames_ = m_p.getParameter<std::vector<std::string> >("lvNames");
81  lvLevel_ = m_p.getParameter<std::vector<int> >("lvLevels");
82  iaddLevel_ = (m_p.getParameter<bool>("useDD4hep")) ? 1 : 0;
83 
84  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: Studies Material budget for " << lvNames_.size()
85  << " volumes with addLevel " << iaddLevel_;
86  std::ostringstream st1;
87  for (unsigned int k = 0; k < lvNames_.size(); ++k)
88  st1 << " [" << k << "] " << lvNames_[k] << " at " << lvLevel_[k];
89  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: Volumes" << st1.str();
90 
91  produces<edm::MaterialInformationContainer>("MaterialInformation");
92 #ifdef EDM_ML_DEBUG
93  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: will produce MaterialInformationContainer";
94 #endif
95 }
96 
98  std::unique_ptr<edm::MaterialInformationContainer> matbg(new edm::MaterialInformationContainer);
99  endOfEvent(*matbg);
100  e.put(std::move(matbg), "MaterialInformation");
101 }
102 
104  init_ = loadLV();
105 
106 #ifdef EDM_ML_DEBUG
107  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Finds " << mapLV_.size()
108  << " logical volumes with return flag " << init_;
109 #endif
110 }
111 
113 #ifdef EDM_ML_DEBUG
114  int iev = (*evt)()->GetEventID();
115  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume: =====> Begin event = " << iev << std::endl;
116 #endif
117  if (!init_)
118  init_ = loadLV();
119 
120  store_.clear();
121 }
122 
124  lengths_ = std::vector<MatInfo>(mapLV_.size());
125 
126 #ifdef EDM_ML_DEBUG
127  const G4Track* aTrack = (*trk)();
128  const G4ThreeVector& mom = aTrack->GetMomentum();
129  double theEnergy = aTrack->GetTotalEnergy();
130  int theID = static_cast<int>(aTrack->GetDefinition()->GetPDGEncoding());
131  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolumme: Track " << aTrack->GetTrackID() << " Code " << theID
132  << " Energy " << theEnergy / CLHEP::GeV << " GeV; Momentum " << mom / CLHEP::GeV
133  << " GeV";
134 #endif
135 }
136 
137 void MaterialBudgetVolume::update(const G4Step* aStep) {
138  G4Material* material = aStep->GetPreStepPoint()->GetMaterial();
139  double step = aStep->GetStepLength();
140  double radl = material->GetRadlen();
141  double intl = material->GetNuclearInterLength();
142  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
143  int index = findLV(touch);
144  if (index >= 0) {
145  lengths_[index].stepL += step;
146  lengths_[index].radL += (step / radl);
147  lengths_[index].intL += (step / intl);
148  }
149 #ifdef EDM_ML_DEBUG
150  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Step in "
151  << touch->GetVolume(0)->GetLogicalVolume()->GetName() << " Index " << index
152  << " Step " << step << " RadL " << step / radl << " IntL " << step / intl;
153 #endif
154 }
155 
157  const G4Track* aTrack = (*trk)();
158  int id = aTrack->GetTrackID();
159  double eta = aTrack->GetMomentumDirection().eta();
160  double phi = aTrack->GetMomentumDirection().phi();
161  for (unsigned int k = 0; k < lengths_.size(); ++k) {
162  MaterialInformation info(lvNames_[k], id, eta, phi, lengths_[k].stepL, lengths_[k].radL, lengths_[k].intL);
163  store_.emplace_back(info);
164 #ifdef EDM_ML_DEBUG
165  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Volume[" << k << "]: " << info;
166 #endif
167  }
168 }
169 
171 #ifdef EDM_ML_DEBUG
172  unsigned int kount(0);
173 #endif
174  for (const auto& element : store_) {
175  MaterialInformation info(element.vname(),
176  element.id(),
177  element.trackEta(),
178  element.trackPhi(),
179  element.stepLength(),
180  element.radiationLength(),
181  element.interactionLength());
182  matbg.push_back(info);
183 #ifdef EDM_ML_DEBUG
184  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume:: Info[" << kount << "] " << info;
185  ++kount;
186 #endif
187  }
188 }
189 
191  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
192  bool flag(false);
193  if (lvs != nullptr) {
194  std::vector<G4LogicalVolume*>::const_iterator lvcite;
195  for (unsigned int i = 0; i < lvNames_.size(); i++) {
196  G4LogicalVolume* lv = nullptr;
198  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
199  std::string namx(dd4hep::dd::noNamespace((*lvcite)->GetName()));
200  if (namx == name) {
201  lv = (*lvcite);
202  break;
203  }
204  }
205  if (lv != nullptr)
206  mapLV_[i] = std::make_pair(lv, (lvLevel_[i] + iaddLevel_));
207  }
208  flag = true;
209 #ifdef EDM_ML_DEBUG
210  edm::LogVerbatim("MaterialBudget") << "MaterialBudgetVolume::Finds " << mapLV_.size() << " logical volumes";
211  unsigned int k(0);
212  for (const auto& lvs : mapLV_) {
213  edm::LogVerbatim("MaterialBudget") << "Entry[" << k << "] " << lvs.first << ": (" << (lvs.second).first << ", "
214  << (lvs.second).second << ") : " << lvNames_[lvs.first];
215  ++k;
216  }
217 #endif
218  }
219  return flag;
220 }
221 
222 int MaterialBudgetVolume::findLV(const G4VTouchable* touch) {
223  int level(-1);
224  int levels = ((touch->GetHistoryDepth()) + 1);
225  for (const auto& lvs : mapLV_) {
226  if ((lvs.second).second <= levels) {
227  int ii = levels - (lvs.second).second;
228  if ((touch->GetVolume(ii)->GetLogicalVolume()) == (lvs.second).first) {
229  level = lvs.first;
230  break;
231  }
232  }
233  }
234 #ifdef EDM_ML_DEBUG
235  if (level < 0) {
236  edm::LogVerbatim("MaterialBudget") << "findLV: Gets " << level << " from " << levels << " levels in touchables";
237  for (int i = 0; i < levels; ++i)
238  edm::LogVerbatim("MaterialBudget") << "[" << (levels - i) << "] " << touch->GetVolume(i)->GetLogicalVolume()
239  << " : " << touch->GetVolume(i)->GetLogicalVolume()->GetName();
240  }
241 #endif
242  return level;
243 }
244 
248 
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)
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_
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)
MatInfo(double s=0, double r=0, double l=0)
step
Definition: StallMonitor.cc:83
def move(src, dest)
Definition: eostools.py:511