CMS 3D CMS Logo

HFShowerParam.cc
Go to the documentation of this file.
1 // File: HFShowerParam.cc
3 // Description: Parametrized version of HF hits
5 
11 
15 
16 #include "G4VPhysicalVolume.hh"
17 #include "G4Step.hh"
18 #include "G4Track.hh"
19 #include "G4NavigationHistory.hh"
20 #include "Randomize.hh"
21 
22 #include "CLHEP/Units/GlobalPhysicalConstants.h"
23 #include "CLHEP/Units/GlobalSystemOfUnits.h"
24 
25 #include<iostream>
26 
27 //#define DebugLog
28 //#define plotDebug
29 //#define mkdebug
30 
32  edm::ParameterSet const & p) : showerLibrary(nullptr),
33  fibre(nullptr), gflash(nullptr),
34  fillHisto(false) {
35  edm::ParameterSet m_HF = p.getParameter<edm::ParameterSet>("HFShower");
36  pePerGeV = m_HF.getParameter<double>("PEPerGeV");
37  trackEM = m_HF.getParameter<bool>("TrackEM");
38  bool useShowerLibrary = m_HF.getParameter<bool>("UseShowerLibrary");
39  bool useGflash = m_HF.getParameter<bool>("UseHFGflash");
40  edMin = m_HF.getParameter<double>("EminLibrary");
41  onlyLong = m_HF.getParameter<bool>("OnlyLong");
42  ref_index = m_HF.getParameter<double>("RefIndex");
43  double lambdaMean = m_HF.getParameter<double>("LambdaMean");
44  aperture = cos(asin(m_HF.getParameter<double>("Aperture")));
45  applyFidCut = m_HF.getParameter<bool>("ApplyFiducialCut");
46  parametrizeLast = m_HF.getUntrackedParameter<bool>("ParametrizeLast",false);
47  edm::LogVerbatim("HFShower") << "HFShowerParam::Use of shower library is set to "
48  << useShowerLibrary << " Use of Gflash is set to "
49  << useGflash << " P.E. per GeV " << pePerGeV
50  << ", ref. index of fibre " << ref_index
51  << ", Track EM Flag " << trackEM << ", edMin "
52  << edMin << " GeV, use of Short fibre info in"
53  << " shower library set to " << !(onlyLong)
54  << ", use of parametrization for last part set to "
55  << parametrizeLast << ", Mean lambda " << lambdaMean
56  << ", aperture (cutoff) " << aperture
57  << ", Application of Fiducial Cut " << applyFidCut;
58 
59 #ifdef plotDebug
61  if (tfile.isAvailable()) {
62  fillHisto = true;
63  LogDebug("HFShower") << "HFShowerParam::Save histos in directory "
64  << "ProfileFromParam";
65  TFileDirectory showerDir = tfile->mkdir("ProfileFromParam");
66  hzvem = showerDir.make<TH1F>("hzvem", "Longitudinal Profile (EM Part);Number of PE",330,0.0,1650.0);
67  hzvhad = showerDir.make<TH1F>("hzvhad","Longitudinal Profile (Had Part);Number of PE",330,0.0,1650.0);
68  em_2d_1 = showerDir.make<TH2F>("em_2d_1","Lateral Profile vs. Shower Depth;cm;Events",800,800.0,1600.0,100,50.0,150.0);
69  em_long_1 = showerDir.make<TH1F>("em_long_1","Longitudinal Profile;Radiation Length;Number of Spots",800,800.0,1600.0);
70  em_long_1_tuned = showerDir.make<TH1F>("em_long_1_tuned","Longitudinal Profile;Radiation Length;Number of Spots",800,800.0,1600.0);
71  em_lateral_1 = showerDir.make<TH1F>("em_lateral_1","Lateral Profile;cm;Events",100,50.0,150.0);
72  em_2d_2 = showerDir.make<TH2F>("em_2d_2","Lateral Profile vs. Shower Depth;cm;Events",800,800.0,1600.0,100,50.0,150.0);
73  em_long_2 = showerDir.make<TH1F>("em_long_2","Longitudinal Profile;Radiation Length;Number of Spots",800,800.0,1600.0);
74  em_lateral_2 = showerDir.make<TH1F>("em_lateral_2","Lateral Profile;cm;Events",100,50.0,150.0);
75  em_long_gflash = showerDir.make<TH1F>("em_long_gflash","Longitudinal Profile From GFlash;cm;Number of Spots",800,800.0,1600.0);
76  em_long_sl = showerDir.make<TH1F>("em_long_sl","Longitudinal Profile From Shower Library;cm;Number of Spots",800,800.0,1600.0);
77  } else {
78  fillHisto = false;
79  edm::LogVerbatim("HFShower") << "HFShowerParam::No file is available for "
80  << "saving histos so the flag is set to false";
81  }
82 #endif
83 
84  if (useShowerLibrary) showerLibrary = new HFShowerLibrary(name,cpv,p);
85  if (useGflash) gflash = new HFGflash(p);
86  fibre = new HFFibre(name, cpv, p);
87  attLMeanInv = fibre->attLength(lambdaMean);
88  edm::LogVerbatim("HFShower") << "att. length used for (lambda=" << lambdaMean
89  << ") = " << 1/(attLMeanInv*cm) << " cm";
90 }
91 
93  delete fibre;
94  delete gflash;
95  delete showerLibrary;
96 }
97 
99  if (showerLibrary) showerLibrary->initRun(nullptr, hcons);
100  if (fibre) fibre->initRun(hcons);
101 }
102 
103 std::vector<HFShowerParam::Hit> HFShowerParam::getHits(const G4Step * aStep,
104  double weight,
105  bool& isKilled) {
106  auto const preStepPoint = aStep->GetPreStepPoint();
107  auto const track = aStep->GetTrack();
109  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
110  double zv = std::abs(hitPoint.z()) - gpar[4] - 0.5*gpar[1];
111  G4ThreeVector localPoint = G4ThreeVector(hitPoint.x(),hitPoint.y(),zv);
112 
113  double pin = (preStepPoint->GetTotalEnergy())/GeV;
114  double zint = hitPoint.z();
115  double zz = std::abs(zint) - gpar[4];
116 
117 #ifdef DebugLog
118  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits "
119  << track->GetDefinition()->GetParticleName()
120  << " of energy " << pin << " GeV"
121  << " Pos x,y,z = " << hitPoint.x() << ","
122  << hitPoint.y() << "," << zint << " (" << zz << ","
123  << localPoint.z() << ", "
124  << (localPoint.z()+0.5*gpar[1]) << ") Local "
125  << localPoint;
126 #endif
127  std::vector<HFShowerParam::Hit> hits;
129  hit.position = hitPoint;
130 
131  // look for other charged particles
132  bool other = false;
133  double pBeta = track->GetDynamicParticle()->GetTotalMomentum() / track->GetDynamicParticle()->GetTotalEnergy();
134  double dirz = (track->GetDynamicParticle()->GetMomentumDirection()).z();
135  if (hitPoint.z() < 0) dirz *= -1.;
136 #ifdef DebugLog
137  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits Momentum "
138  <<track->GetDynamicParticle()->GetMomentumDirection()
139  << " HitPoint " << hitPoint << " dirz " << dirz;
140 #endif
141  if (!isEM && track->GetDefinition()->GetPDGCharge() != 0 && pBeta > (1/ref_index) &&
142  aStep->GetTotalEnergyDeposit() > 0.) { other = true; }
143 
144  // take only e+-/gamma/or special particles
145  if (isEM || other) {
146  // Leave out the last part
147  double edep = 0.;
148  if ((!trackEM) && ((zz<(gpar[1]-gpar[2])) || parametrizeLast) && (!other)){
149  edep = pin;
150  isKilled = true;
151  } else if ((track->GetDefinition()->GetPDGCharge() != 0) &&
152  (pBeta > (1/ref_index)) && (dirz > aperture)) {
153  edep = (aStep->GetTotalEnergyDeposit())/GeV;
154  }
155  std::string path = "ShowerLibrary";
156 #ifdef DebugLog
157  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits edep = " << edep
158  << " weight " << weight << " final " <<edep*weight
159  << ", Kill = " << kill << ", pin = " << pin
160  << ", edMin = " << edMin << " Other " << other;
161 #endif
162  edep *= weight;
163  if (edep > 0) {
164  if ((showerLibrary || gflash) && isKilled && pin > edMin && (!other)) {
165  if (showerLibrary) {
166  std::vector<HFShowerLibrary::Hit> hitSL = showerLibrary->getHits(aStep,isKilled,weight,onlyLong);
167  for (unsigned int i=0; i<hitSL.size(); i++) {
168  bool ok = true;
169 #ifdef DebugLog
170  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits applyFidCut = " << applyFidCut;
171 #endif
172  if (applyFidCut) { // @@ For showerlibrary no z-cut for Short (no z)
173  int npmt = HFFibreFiducial:: PMTNumber(hitSL[i].position);
174  if (npmt <= 0) ok = false;
175  }
176  if (ok) {
177  hit.position = hitSL[i].position;
178  hit.depth = hitSL[i].depth;
179  hit.time = hitSL[i].time;
180  hit.edep = 1;
181  hits.push_back(hit);
182 #ifdef plotDebug
183  if (fillHisto) {
184  double zv = std::abs(hit.position.z()) - gpar[4];
185  hzvem->Fill(zv);
186  em_long_sl->Fill(hit.position.z()/cm);
187  double sq = sqrt(pow(hit.position.x()/cm,2)+pow(hit.position.y()/cm,2));
188  double zp = hit.position.z()/cm;
189  if (hit.depth == 1) {
190  em_2d_1->Fill(zp, sq);
191  em_lateral_1->Fill(sq);
192  em_long_1->Fill(zp);
193  } else if (hit.depth == 2) {
194  em_2d_2->Fill(zp, sq);
195  em_lateral_2->Fill(sq);
196  em_long_2->Fill(zp);
197  }
198  }
199 #endif
200 #ifdef DebugLog
201  edm::LogVerbatim("HFShower") << "HFShowerParam: Hit at depth "
202  << hit.depth << " with edep " << hit.edep
203  << " Time " << hit.time;
204 #endif
205  }
206  }
207  } else { // GFlash clusters with known z
208  std::vector<HFGflash::Hit>hitSL=gflash->gfParameterization(aStep, onlyLong);
209  for (unsigned int i=0; i<hitSL.size(); ++i) {
210  bool ok = true;
211  G4ThreeVector pe_effect(hitSL[i].position.x(), hitSL[i].position.y(),
212  hitSL[i].position.z());
213  double zv = std::abs(pe_effect.z()) - gpar[4];
214  //depth
215  int depth = 1;
216  int npmt = 0;
217  if (zv < 0. || zv > gpar[1]) {
218 #ifdef mkdebug
219  std::cout<<"-#Zcut-HFShowerParam::getHits:z="<<zv<<",m="<<gpar[1]<<std::endl;
220 #endif
221  ok = false;
222  }
223  if (ok && applyFidCut) {
224  npmt = HFFibreFiducial:: PMTNumber(pe_effect);
225 #ifdef DebugLog
226  edm::LogVerbatim("HFShower") << "HFShowerParam::getHits:#PMT= "
227  << npmt << ",z = " << zv;
228 #endif
229  if (npmt <= 0) {
230 #ifdef DebugLog
231  edm::LogVerbatim("HFShower") << "-#PMT=0 cut-HFShowerParam::"
232  << "getHits: npmt = " << npmt;
233 #endif
234  ok = false;
235  } else if (npmt > 24) { // a short fibre
236  if (zv > gpar[0]) {
237  depth = 2;
238  } else {
239 #ifdef DebugLog
240  edm::LogVerbatim("HFShower") << "-SHORT cut-HFShowerParam::"
241  << "getHits:zMin=" << gpar[0];
242 #endif
243  ok = false;
244  }
245  }
246 #ifdef DebugLog
247  edm::LogVerbatim("HFShower") << "HFShowerParam: npmt " << npmt
248  << " zv " << std::abs(pe_effect.z())
249  << ":" << gpar[4] << ":" << zv << ":"
250  << gpar[0] << " ok " << ok << " depth "
251  << depth;
252 #endif
253  } else {
254  if (G4UniformRand() > 0.5) depth = 2;
255  if (depth == 2 && zv < gpar[0]) ok = false;
256  }
257  //attenuation
258  double dist = fibre->zShift(localPoint,depth,0); // distance to PMT
259  double r1 = G4UniformRand();
260 #ifdef DebugLog
261  edm::LogVerbatim("HFShower") << "HFShowerParam:Distance to PMT (" <<npmt
262  << ") " << dist << ", exclusion flag "
263  << (r1 > exp(-attLMeanInv*zv));
264 #endif
265  if (r1 > exp(-attLMeanInv*dist)) ok = false;
266  if (ok) {
267  double r2 = G4UniformRand();
268 #ifdef DebugLog
269  edm::LogVerbatim("HFShower") << "HFShowerParam:Extra exclusion "
270  << r2 << ">" << weight << " "
271  << (r2 > weight);
272 #endif
273  if (r2 < weight) {
274  double time = fibre->tShift(localPoint,depth,0);
275 
276  hit.position = hitSL[i].position;
277  hit.depth = depth;
278  hit.time = time + hitSL[i].time;
279  hit.edep = 1;
280  hits.push_back(hit);
281 #ifdef plotDebug
282  if (fillHisto) {
283  em_long_gflash->Fill(pe_effect.z()/cm, hitSL[i].edep);
284  hzvem->Fill(zv);
285  double sq = sqrt(pow(hit.position.x()/cm,2)+pow(hit.position.y()/cm,2));
286  double zp = hit.position.z()/cm;
287  if (hit.depth == 1) {
288  em_2d_1->Fill(zp, sq);
289  em_lateral_1->Fill(s);
290  em_long_1->Fill(zp);
291  } else if (hit.depth == 2) {
292  em_2d_2->Fill(zp, sq);
293  em_lateral_2->Fill(sq);
294  em_long_2->Fill(zp);
295  }
296  }
297 #endif
298 #ifdef DebugLog
299  edm::LogVerbatim("HFShower") << "HFShowerParam: Hit at depth "
300  << hit.depth << " with edep "
301  << hit.edep << " Time " << hit.time;
302 #endif
303  }
304  }
305  }
306  }
307  } else {
308  path = "Rest";
309  edep *= pePerGeV;
310  double tSlice = (aStep->GetPostStepPoint()->GetGlobalTime());
311  double time = fibre->tShift(localPoint,1,0); // remaining part
312  bool ok = true;
313  if (applyFidCut) { // @@ For showerlibrary no z-cut for Short (no z)
314  int npmt = HFFibreFiducial:: PMTNumber(hitPoint);
315  if (npmt <= 0) ok = false;
316  }
317 #ifdef DebugLog
318  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits hitPoint " << hitPoint << " flag " << ok;
319 #endif
320  if (ok) {
321  hit.depth = 1;
322  hit.time = tSlice+time;
323  hit.edep = edep;
324  hits.push_back(hit);
325 #ifdef DebugLog
326  edm::LogVerbatim("HFShower") << "HFShowerParam: Hit at depth 1 with edep "
327  << edep << " Time " << tSlice << ":" << time
328  << ":" << hit.time;
329 #endif
330 #ifdef plotDebug
331  double zv = std::abs(hitPoint.z()) - gpar[4];
332  if (fillHisto) {
333  hzvhad->Fill(zv);
334  }
335 #endif
336  if (zz >= gpar[0]) {
337  time = fibre->tShift(localPoint,2,0);
338  hit.depth = 2;
339  hit.time = tSlice+time;
340  hits.push_back(hit);
341 #ifdef DebugLog
342  edm::LogVerbatim("HFShower") <<"HFShowerParam: Hit at depth 2 with edep "
343  << edep << " Time " << tSlice << ":" << time
344  << hit.time;
345 #endif
346 #ifdef plotDebug
347  if (fillHisto) {
348  hzvhad->Fill(zv);
349  }
350 #endif
351  }
352  }
353  }
354 #ifdef DebugLog
355  for (unsigned int ii=0; ii<hits.size(); ++ii) {
356  double zv = std::abs(hits[ii].position.z());
357  if (zv > 12790) edm::LogVerbatim("HFShower")<< "HFShowerParam: Abnormal hit along "
358  << path << " in "
359  << preStepPoint->GetPhysicalVolume()->GetLogicalVolume()->GetName()
360  << " at " << hits[ii].position << " zz "
361  << zv << " Edep " << edep << " due to "
362  <<track->GetDefinition()->GetParticleName()
363  << " time " << hit.time;
364  }
365  edm::LogVerbatim("HFShower") << "HFShowerParam: getHits kill (" << kill
366  << ") track " << track->GetTrackID()
367  << " at " << hitPoint
368  << " and deposit " << edep << " " << hits.size()
369  << " times" << " ZZ " << zz << " " << gpar[0];
370 #endif
371  }
372  }
373  return hits;
374 }
375 
376 std::vector<double> HFShowerParam::getDDDArray(const std::string & str,
377  const DDsvalues_type & sv)
378 {
379 #ifdef DebugLog
380  LogDebug("HFShower") << "HFShowerParam:getDDDArray called for " << str;
381 #endif
382  DDValue value(str);
383  if (DDfetch(&sv,value))
384  {
385 #ifdef DebugLog
386  LogDebug("HFShower") << value;
387 #endif
388  const std::vector<double> & fvec = value.doubles();
389  int nval = fvec.size();
390  if (nval < 2) {
391  edm::LogError("HFShower") << "HFShowerParam : # of " << str
392  << " bins " << nval << " < 2 ==> illegal";
393  throw cms::Exception("Unknown", "HFShowerParam") << "nval < 2 for array "
394  << str << "\n";
395  }
396  return fvec;
397  } else {
398  edm::LogError("HFShower") << "HFShowerParam : cannot get array " << str;
399  throw cms::Exception("Unknown", "HFShowerParam") << "cannot get array "
400  << str << "\n";
401  }
402 }
#define LogDebug(id)
double tShift(const G4ThreeVector &point, int depth, int fromEndAbs=0)
Definition: HFFibre.cc:115
T getParameter(std::string const &) const
HFShowerParam(const std::string &name, const DDCompactView &cpv, edm::ParameterSet const &p)
double attLMeanInv
Definition: HFShowerParam.h:54
T getUntrackedParameter(std::string const &, T const &) const
std::vector< Hit > gfParameterization(const G4Step *aStep, bool onlyLong=false)
Definition: HFGflash.cc:90
std::vector< Hit > getHits(const G4Step *aStep, bool &ok, double weight, bool onlyLong=false)
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:140
bool parametrizeLast
Definition: HFShowerParam.h:55
const double GeV
Definition: MathUtil.h:16
void initRun(const HcalDDDSimConstants *)
Definition: HFFibre.cc:82
virtual ~HFShowerParam()
double attLength(double lambda)
Definition: HFFibre.cc:97
void initRun(const HcalDDDSimConstants *)
std::vector< double > gpar
Definition: HFShowerParam.h:57
double zShift(const G4ThreeVector &point, int depth, int fromEndAbs=0)
Definition: HFFibre.cc:127
Definition: weight.py:1
HFShowerLibrary * showerLibrary
Definition: HFShowerParam.h:51
#define nullptr
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:81
TH1F * em_lateral_2
Definition: HFShowerParam.h:59
HFFibre * fibre
Definition: HFShowerParam.h:52
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
bool isAvailable() const
Definition: Service.h:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< Hit > getHits(const G4Step *aStep, double weight, bool &isKilled)
T * make(const Args &...args) const
make new ROOT object
HFGflash * gflash
Definition: HFShowerParam.h:53
std::vector< double > getDDDArray(const std::string &, const DDsvalues_type &)
ii
Definition: cuy.py:590
G4ThreeVector position
Definition: HFShowerParam.h:38
int PMTNumber(const G4ThreeVector &pe_effect)
double ref_index
Definition: HFShowerParam.h:54
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
TH1F * em_long_gflash
Definition: HFShowerParam.h:60
TH1F * em_long_sl
Definition: HFShowerParam.h:61
TH1F * em_long_1
Definition: HFShowerParam.h:59
TH1F * em_long_1_tuned
Definition: HFShowerParam.h:60
static int position[264][3]
Definition: ReadPGInfo.cc:509
static bool isGammaElectronPositron(int pdgCode)
std::vector< std::pair< unsigned int, DDValue > > DDsvalues_type
Definition: DDsvalues.h:12
TH1F * em_lateral_1
Definition: HFShowerParam.h:59
TH1F * em_long_2
Definition: HFShowerParam.h:59
#define str(s)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
void initRun(G4ParticleTable *, const HcalDDDSimConstants *)