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