CMS 3D CMS Logo

HGCalTBMB.cc
Go to the documentation of this file.
6 
11 
12 #include "G4LogicalVolumeStore.hh"
13 #include "G4Step.hh"
14 #include "G4Track.hh"
15 
16 #include <TH1F.h>
17 
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
22 //#define EDM_ML_DEBUG
23 
24 class HGCalTBMB : public SimWatcher,
25  public Observer<const BeginOfTrack*>,
26  public Observer<const G4Step*>,
27  public Observer<const EndOfTrack*> {
28 public:
30  HGCalTBMB(const HGCalTBMB&) = delete; // stop default
31  const HGCalTBMB& operator=(const HGCalTBMB&) = delete; // ...
32  ~HGCalTBMB() override;
33 
34 private:
35  void update(const BeginOfTrack*) override;
36  void update(const G4Step*) override;
37  void update(const EndOfTrack*) override;
38 
39  bool stopAfter(const G4Step*);
40  int findVolume(const G4VTouchable* touch, bool stop) const;
41 
42  std::vector<std::string> listNames_;
44  double stopZ_;
45  unsigned int nList_;
46  std::vector<double> radLen_, intLen_, stepLen_;
47  std::vector<TH1D*> me100_, me200_, me300_;
48 };
49 
51  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("HGCalTBMB");
52  listNames_ = m_p.getParameter<std::vector<std::string> >("DetectorNames");
53  stopName_ = m_p.getParameter<std::string>("StopName");
54  stopZ_ = m_p.getParameter<double>("MaximumZ");
55  nList_ = listNames_.size();
56  edm::LogVerbatim("HGCSim") << "HGCalTBMB initialized for " << nList_ << " volumes";
57  for (unsigned int k = 0; k < nList_; ++k)
58  edm::LogVerbatim("HGCSim") << " [" << k << "] " << listNames_[k];
59  edm::LogVerbatim("HGCSim") << "Stop after " << stopZ_ << " or reaching volume " << stopName_;
60 
62  if (!tfile.isAvailable())
63  throw cms::Exception("BadConfig") << "TFileService unavailable: "
64  << "please add it to config file";
65  char name[20], title[80];
66  TH1D* hist;
67  for (unsigned int i = 0; i <= nList_; i++) {
68  std::string named = (i == nList_) ? "Total" : listNames_[i];
69  sprintf(name, "RadL%d", i);
70  sprintf(title, "MB(X0) for (%s)", named.c_str());
71  hist = tfile->make<TH1D>(name, title, 100000, 0.0, 100.0);
72  hist->Sumw2(true);
73  me100_.push_back(hist);
74  sprintf(name, "IntL%d", i);
75  sprintf(title, "MB(L0) for (%s)", named.c_str());
76  hist = tfile->make<TH1D>(name, title, 100000, 0.0, 10.0);
77  hist->Sumw2(true);
78  me200_.push_back(hist);
79  sprintf(name, "StepL%d", i);
80  sprintf(title, "MB(Step) for (%s)", named.c_str());
81  hist = tfile->make<TH1D>(name, title, 100000, 0.0, 50000.0);
82  hist->Sumw2(true);
83  me300_.push_back(hist);
84  }
85  edm::LogVerbatim("HGCSim") << "HGCalTBMB: Booking user histos done ===";
86 }
87 
89 
90 void HGCalTBMB::update(const BeginOfTrack* trk) {
91  radLen_ = std::vector<double>(nList_ + 1, 0);
92  intLen_ = std::vector<double>(nList_ + 1, 0);
93  stepLen_ = std::vector<double>(nList_ + 1, 0);
94 
95 #ifdef EDM_ML_DEBUG
96  const G4Track* aTrack = (*trk)(); // recover G4 pointer if wanted
97  const G4ThreeVector& mom = aTrack->GetMomentum();
98  double theEnergy = aTrack->GetTotalEnergy();
99  int theID = (int)(aTrack->GetDefinition()->GetPDGEncoding());
100  edm::LogVerbatim("HGCSim") << "MaterialBudgetHcalHistos: Track " << aTrack->GetTrackID() << " Code " << theID
101  << " Energy " << theEnergy / CLHEP::GeV << " GeV; Momentum " << mom;
102 #endif
103 }
104 
105 void HGCalTBMB::update(const G4Step* aStep) {
106  G4Material* material = aStep->GetPreStepPoint()->GetMaterial();
107  double step = aStep->GetStepLength();
108  double radl = material->GetRadlen();
109  double intl = material->GetNuclearInterLength();
110 
111  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
112  int indx = findVolume(touch, false);
113 
114  if (indx >= 0) {
115  stepLen_[indx] += step;
116  radLen_[indx] += (step / radl);
117  intLen_[indx] += (step / intl);
118  }
119  stepLen_[nList_] += step;
120  radLen_[nList_] += (step / radl);
121  intLen_[nList_] += (step / intl);
122 #ifdef EDM_ML_DEBUG
123  edm::LogVerbatim("HGCSim") << "HGCalTBMB::Step in " << touch->GetVolume(0)->GetLogicalVolume()->GetName() << " Index "
124  << indx << " Step " << step << " RadL " << step / radl << " IntL " << step / intl;
125 #endif
126 
127  if (stopAfter(aStep)) {
128  G4Track* track = aStep->GetTrack();
129  track->SetTrackStatus(fStopAndKill);
130  }
131 }
132 
133 void HGCalTBMB::update(const EndOfTrack* trk) {
134  for (unsigned int ii = 0; ii <= nList_; ++ii) {
135  me100_[ii]->Fill(radLen_[ii]);
136  me200_[ii]->Fill(intLen_[ii]);
137  me300_[ii]->Fill(stepLen_[ii]);
138 #ifdef EDM_ML_DEBUG
139  std::string name("Total");
140  if (ii < nList_)
141  name = listNames_[ii];
142  edm::LogVerbatim("HGCSim") << "HGCalTBMB::Volume[" << ii << "]: " << name << " == Step " << stepLen_[ii] << " RadL "
143  << radLen_[ii] << " IntL " << intLen_[ii];
144 #endif
145  }
146 }
147 
148 bool HGCalTBMB::stopAfter(const G4Step* aStep) {
149  bool flag(false);
150  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
151  G4ThreeVector hitPoint = aStep->GetPreStepPoint()->GetPosition();
152  if (aStep->GetPostStepPoint() != nullptr)
153  hitPoint = aStep->GetPostStepPoint()->GetPosition();
154  double zz = hitPoint.z();
155 
156  if ((findVolume(touch, true) == 0) || (zz > stopZ_))
157  flag = true;
158 #ifdef EDM_ML_DEBUG
159  edm::LogVerbatim("HGCSim") << " HGCalTBMB::Name " << touch->GetVolume(0)->GetName() << " z " << zz << " Flag" << flag;
160 #endif
161  return flag;
162 }
163 
164 int HGCalTBMB::findVolume(const G4VTouchable* touch, bool stop) const {
165  int ivol = -1;
166  int level = (touch->GetHistoryDepth()) + 1;
167  for (int ii = 0; ii < level; ii++) {
168  std::string name = touch->GetVolume(ii)->GetName();
169  if (stop) {
170  if (strcmp(name.c_str(), stopName_.c_str()) == 0)
171  ivol = 0;
172  } else {
173  for (unsigned int k = 0; k < nList_; ++k) {
174  if (strcmp(name.c_str(), listNames_[k].c_str()) == 0) {
175  ivol = k;
176  break;
177  }
178  }
179  }
180  if (ivol >= 0)
181  break;
182  }
183  return ivol;
184 }
185 
189 
Log< level::Info, true > LogVerbatim
const HGCalTBMB & operator=(const HGCalTBMB &)=delete
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
~HGCalTBMB() override
Definition: HGCalTBMB.cc:88
int findVolume(const G4VTouchable *touch, bool stop) const
Definition: HGCalTBMB.cc:164
bool stopAfter(const G4Step *)
Definition: HGCalTBMB.cc:148
double stopZ_
Definition: HGCalTBMB.cc:44
std::vector< double > intLen_
Definition: HGCalTBMB.cc:46
std::string stopName_
Definition: HGCalTBMB.cc:43
Definition: tfile.py:1
std::vector< TH1D * > me100_
Definition: HGCalTBMB.cc:47
unsigned int nList_
Definition: HGCalTBMB.cc:45
ii
Definition: cuy.py:589
std::vector< TH1D * > me300_
Definition: HGCalTBMB.cc:47
void update(const BeginOfTrack *) override
This routine will be called when the appropriate signal arrives.
Definition: HGCalTBMB.cc:90
HGCalTBMB(const edm::ParameterSet &)
Definition: HGCalTBMB.cc:50
std::vector< TH1D * > me200_
Definition: HGCalTBMB.cc:47
std::vector< double > radLen_
Definition: HGCalTBMB.cc:46
step
Definition: StallMonitor.cc:98
std::vector< std::string > listNames_
Definition: HGCalTBMB.cc:42
std::vector< double > stepLen_
Definition: HGCalTBMB.cc:46