CMS 3D CMS Logo

MaterialBudget.cc
Go to the documentation of this file.
2 
7 
11 
12 #include "G4LogicalVolumeStore.hh"
13 #include "G4Step.hh"
14 #include "G4Track.hh"
15 
16 #include <iostream>
17 //#define DebugLog
18 
20 
21  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("MaterialBudget");
22  detTypes = m_p.getParameter<std::vector<std::string> >("DetectorTypes");
23  constituents = m_p.getParameter<std::vector<int> >("Constituents");
24  stackOrder = m_p.getParameter<std::vector<int> >("StackOrder");
25  detNames = m_p.getParameter<std::vector<std::string> >("DetectorNames");
26  detLevels = m_p.getParameter<std::vector<int> >("DetectorLevels");
27  etaRegions = m_p.getParameter<std::vector<double> >("EtaBoundaries");
28  regionTypes = m_p.getParameter<std::vector<int> >("RegionTypes");
29  boundaries = m_p.getParameter<std::vector<double> >("Boundaries");
30  edm::LogInfo("MaterialBudget") << "MaterialBudget initialized for "
31  << detTypes.size() << " detector types";
32  unsigned int nc = 0;
33  for (unsigned int ii=0; ii<detTypes.size(); ++ii) {
34  edm::LogInfo("MaterialBudget") << "Type[" << ii << "] : " << detTypes[ii]
35  << " with " << constituents[ii] <<", order "
36  << stackOrder[ii] << " constituents --> ";
37  for (int kk=0; kk<constituents[ii]; ++kk) {
38  std::string name = "Unknown"; int level = -1;
39  if (nc < detNames.size()) {
40  name = detNames[nc]; level = detLevels[nc]; ++nc;
41  }
42  edm::LogInfo("MaterialBudget") << " Constituent[" << kk << "]: "
43  << name << " at level " << level;
44  }
45  }
46  edm::LogInfo("MaterialBudget") << "MaterialBudget Stop condition for "
47  << etaRegions.size() << " eta regions";
48  for (unsigned int ii=0; ii<etaRegions.size(); ++ii) {
49  edm::LogInfo("MaterialBudget") << "Region[" << ii << "] : Eta < "
50  << etaRegions[ii] << " boundary type "
51  << regionTypes[ii] << " limit "
52  << boundaries[ii];
53  }
54  book(m_p);
55 }
56 
58 }
59 
61 
62  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
63  std::vector<G4LogicalVolume *>::const_iterator lvcite;
64 
65  unsigned int kount=detNames.size();
66  for (unsigned int ii=0; ii<kount; ++ii)
67  logVolumes.push_back(nullptr);
68 
69  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
70  for (unsigned int ii=0; ii<detNames.size(); ++ii) {
71  if (strcmp(detNames[ii].c_str(),(*lvcite)->GetName().c_str()) == 0) {
72  logVolumes[ii] = (*lvcite);
73  kount--;
74  break;
75  }
76  }
77  if (kount <= 0) break;
78  }
79  edm::LogInfo("MaterialBudget") << "MaterialBudget::Finds "
80  << detNames.size()-kount << " out of "
81  << detNames.size() << " LV addresses";
82  for (unsigned int ii=0; ii<detNames.size(); ++ii) {
83  std::string name("Unknown");
84  if (logVolumes[ii] != nullptr) name = logVolumes[ii]->GetName();
85  edm::LogInfo("MaterialBudget") << "LV[" << ii << "] : " << detNames[ii]
86  << " Address " << logVolumes[ii] << " | "
87  << name;
88  }
89 
90  for (unsigned int ii=0; ii<(detTypes.size()+1); ++ii) {
91  stepLen.push_back(0); radLen.push_back(0); intLen.push_back(0);
92  }
93  stackOrder.push_back(0);
94 }
95 
97 
98  for (unsigned int ii=0; ii<(detTypes.size()+1); ++ii) {
99  stepLen[ii] = 0; radLen[ii] = 0; intLen[ii] = 0;
100  }
101 
102  const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
103  const G4ThreeVector& mom = aTrack->GetMomentum() ;
104  if (mom.theta() != 0 ) {
105  eta = mom.eta();
106  } else {
107  eta = -99;
108  }
109  phi = mom.phi();
110  stepT = 0;
111 
112 #ifdef DebugLog
113  double theEnergy = aTrack->GetTotalEnergy();
114  int theID = (int)(aTrack->GetDefinition()->GetPDGEncoding());
115  edm::LogInfo("MaterialBudget") << "MaterialBudgetHcalHistos: Track "
116  << aTrack->GetTrackID() << " Code " << theID
117  << " Energy " <<theEnergy/CLHEP::GeV
118  << " GeV; Eta " << eta << " Phi "
119  << phi/CLHEP::deg << " PT "
120  << mom.perp()/CLHEP::GeV << " GeV *****";
121 #endif
122 }
123 
124 void MaterialBudget::update(const G4Step* aStep) {
125 
126  //---------- each step
127  G4Material * material = aStep->GetPreStepPoint()->GetMaterial();
128  double step = aStep->GetStepLength();
129  double radl = material->GetRadlen();
130  double intl = material->GetNuclearInterLength();
131 
132  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
133  unsigned int indx = detTypes.size();
134  unsigned int nc = 0;
135  for (unsigned int ii=0; ii<detTypes.size(); ++ii) {
136  for (int kk=0; kk<constituents[ii]; ++kk) {
137  if (detLevels[nc+kk] <= (int)((touch->GetHistoryDepth())+1)) {
138  int jj = (int)((touch->GetHistoryDepth())+1)-detLevels[nc+kk];
139  if ((touch->GetVolume(jj)->GetLogicalVolume()) == logVolumes[nc+kk]) {
140  indx = ii;
141  break;
142  }
143  }
144  }
145  nc += (unsigned int)(constituents[ii]);
146  if (indx == ii) break;
147  }
148 
149  stepT += step;
150  stepLen[indx] = stepT;
151  radLen[indx] += (step/radl);
152  intLen[indx] += (step/intl);
153 #ifdef DebugLog
154  edm::LogInfo("MaterialBudget") << "MaterialBudget::Step in "
155  << touch->GetVolume(0)->GetLogicalVolume()->GetName()
156  << " Index " << indx <<" Step " << step
157  << " RadL " << step/radl << " IntL "
158  << step/intl;
159 #endif
160  //----- Stop tracking after selected position
161  if (stopAfter(aStep)) {
162  G4Track* track = aStep->GetTrack();
163  track->SetTrackStatus( fStopAndKill );
164  }
165 }
166 
167 
169 
170  for (unsigned int ii=0; ii<detTypes.size(); ++ii) {
171  for (unsigned int jj=0; jj<=detTypes.size(); ++jj) {
172  if (stackOrder[jj] == (int)(ii+1)) {
173  for (unsigned int kk=0; kk<=detTypes.size(); ++kk) {
174  if (stackOrder[kk] == (int)(ii)) {
175  radLen[jj] += radLen[kk];
176  intLen[jj] += intLen[kk];
177 #ifdef DebugLog
178  edm::LogInfo("MaterialBudget") << "MaterialBudget::Add "
179  << kk << ":" << stackOrder[kk]
180  << " to " << jj <<":"
181  << stackOrder[jj] <<" RadL "
182  << radLen[kk] << " : " << radLen[jj]
183  << " IntL " << intLen[kk] << " : "
184  << intLen[jj] <<" StepL "
185  << stepLen[kk]<< " : "
186  << stepLen[jj];
187 #endif
188  break;
189  }
190  }
191  break;
192  }
193  }
194  }
195 
196  for (unsigned int ii=0; ii<=detTypes.size(); ++ii) {
197  me100[ii]->Fill(eta, radLen[ii]);
198  me200[ii]->Fill(eta, intLen[ii]);
199  me300[ii]->Fill(eta, stepLen[ii]);
200  me400[ii]->Fill(phi, radLen[ii]);
201  me500[ii]->Fill(phi, intLen[ii]);
202  me600[ii]->Fill(phi, stepLen[ii]);
203 #ifdef DebugLog
204  std::string name("Unknown");
205  if (ii < detTypes.size()) name = detTypes[ii];
206  edm::LogInfo("MaterialBudget") << "MaterialBudget::Volume[" << ii
207  << "]: " << name << " eta " << eta
208  << " == Step " << stepLen[ii] << " RadL "
209  << radLen[ii] << " IntL " << intLen[ii];
210 #endif
211  }
212 }
213 
215 
216  // Book histograms
218 
219  if ( !tfile.isAvailable() )
220  throw cms::Exception("BadConfig") << "TFileService unavailable: "
221  << "please add it to config file";
222 
223  int binEta = m_p.getUntrackedParameter<int>("NBinEta", 320);
224  int binPhi = m_p.getUntrackedParameter<int>("NBinPhi", 180);
225  double minEta = m_p.getUntrackedParameter<double>("MinEta",-8.0);
226  double maxEta = m_p.getUntrackedParameter<double>("MaxEta", 8.0);
227  double maxPhi = CLHEP::pi;
228  edm::LogInfo("MaterialBudget") << "MaterialBudget: Booking user "
229  << "histos === with " << binEta << " bins "
230  << "in eta from " << minEta << " to "
231  << maxEta << " and " << binPhi << " bins "
232  << "in phi from " << -maxPhi << " to "
233  << maxPhi;
234 
235  char name[20], title[80];
236  std::string named;
237  for (unsigned int i=0; i<=detTypes.size(); i++) {
238  if (i >= detTypes.size()) named = "Unknown";
239  else named = detTypes[i];
240  sprintf(name, "%d", i+100);
241  sprintf(title, "MB(X0) prof Eta in %s", named.c_str());
242  me100.push_back(tfile->make<TProfile>(name,title, binEta, minEta, maxEta));
243  sprintf(name, "%d", i+200);
244  sprintf(title, "MB(L0) prof Eta in %s", named.c_str());
245  me200.push_back(tfile->make<TProfile>(name,title, binEta, minEta, maxEta));
246  sprintf(name, "%d", i+300);
247  sprintf(title, "MB(Step) prof Eta in %s", named.c_str());
248  me300.push_back(tfile->make<TProfile>(name,title, binEta, minEta, maxEta));
249  sprintf(name, "%d", i+400);
250  sprintf(title, "MB(X0) prof Phi in %s", named.c_str());
251  me400.push_back(tfile->make<TProfile>(name,title, binPhi,-maxPhi, maxPhi));
252  sprintf(name, "%d", i+500);
253  sprintf(title, "MB(L0) prof Phi in %s", named.c_str());
254  me500.push_back(tfile->make<TProfile>(name,title, binPhi,-maxPhi, maxPhi));
255  sprintf(name, "%d", i+600);
256  sprintf(title, "MB(Step) prof Phi in %s", named.c_str());
257  me600.push_back(tfile->make<TProfile>(name,title, binPhi,-maxPhi, maxPhi));
258  }
259 
260  edm::LogInfo("MaterialBudget") << "MaterialBudget: Booking user "
261  << "histos done ===";
262 
263 }
264 
265 bool MaterialBudget::stopAfter(const G4Step* aStep) {
266 
267  G4ThreeVector hitPoint = aStep->GetPreStepPoint()->GetPosition();
268  if (aStep->GetPostStepPoint() != nullptr)
269  hitPoint = aStep->GetPostStepPoint()->GetPosition();
270  double rr = hitPoint.perp();
271  double zz = std::abs(hitPoint.z());
272 
273  bool flag(false);
274  for (unsigned int ii=0; ii<etaRegions.size(); ++ii) {
275 #ifdef DebugLog
276  edm::LogInfo("MaterialBudget") << " MaterialBudget::Eta " << eta
277  << " in Region[" << ii << "] with "
278  << etaRegions[ii] << " type "
279  << regionTypes[ii] << "|" << boundaries[ii];
280 #endif
281  if (fabs(eta) < etaRegions[ii]) {
282  if (regionTypes[ii] == 0) {
283  if (rr >= boundaries[ii]-0.001) flag = true;
284  } else {
285  if (zz >= boundaries[ii]-0.001) flag = true;
286  }
287 #ifdef DebugLog
288  if (flag)
289  edm::LogInfo("MaterialBudget") <<" MaterialBudget::Stop after R = "
290  << rr << " and Z = " << zz;
291 #endif
292  break;
293  }
294  }
295 #ifdef DebugLog
296  edm::LogInfo("MaterialBudget") <<" MaterialBudget:: R = " << rr
297  << " and Z = " << zz << " Flag " << flag;
298 #endif
299  return flag;
300 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
const double GeV
Definition: MathUtil.h:16
std::vector< TProfile * > me300
std::vector< int > detLevels
std::vector< double > intLen
std::vector< std::string > detNames
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double maxEta
std::vector< int > regionTypes
std::vector< double > stepLen
const Double_t pi
~MaterialBudget() override
MaterialBudget(const edm::ParameterSet &)
std::vector< TProfile * > me600
std::vector< TProfile * > me100
void book(const edm::ParameterSet &)
void update(const BeginOfRun *) override
This routine will be called when the appropriate signal arrives.
bool isAvailable() const
Definition: Service.h:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ii
Definition: cuy.py:588
bool stopAfter(const G4Step *)
std::vector< TProfile * > me400
std::vector< TProfile * > me200
std::vector< TProfile * > me500
std::vector< double > boundaries
std::vector< std::string > detTypes
std::vector< double > etaRegions
std::vector< int > stackOrder
step
std::vector< double > radLen
std::vector< G4LogicalVolume * > logVolumes
std::vector< int > constituents