CMS 3D CMS Logo

HFGflash.cc
Go to the documentation of this file.
2 
3 #include "G4VPhysicalVolume.hh"
4 #include "G4Step.hh"
5 #include "G4Track.hh"
6 #include "G4Navigator.hh"
7 #include "G4NavigationHistory.hh"
8 #include "CLHEP/Units/PhysicalConstants.h"
9 #include "CLHEP/Units/SystemOfUnits.h"
10 
11 #include "Randomize.hh"
12 #include "G4TransportationManager.hh"
13 #include "G4VPhysicalVolume.hh"
14 #include "G4LogicalVolume.hh"
15 #include "G4VSensitiveDetector.hh"
16 #include "G4EventManager.hh"
17 #include "G4SteppingManager.hh"
18 #include "G4FastTrack.hh"
19 #include "G4ParticleTable.hh"
20 
21 #include "CLHEP/GenericFunctions/IncompleteGamma.hh"
22 
26 
29 
30 #include "CLHEP/Units/GlobalPhysicalConstants.h"
31 #include "CLHEP/Units/GlobalSystemOfUnits.h"
32 
33 #include <cmath>
34 
35 //#define DebugLog
36 
38 
39  edm::ParameterSet m_HF = p.getParameter<edm::ParameterSet>("HFGflash");
40  theBField = m_HF.getUntrackedParameter<double>("BField", 3.8);
41  theWatcherOn = m_HF.getUntrackedParameter<bool>("WatcherOn",true);
42  theFillHisto = m_HF.getUntrackedParameter<bool>("FillHisto",true);
43  edm::LogInfo("HFShower") << "HFGFlash:: Set B-Field to " << theBField
44  << ", WatcherOn to " << theWatcherOn
45  << " and FillHisto to " << theFillHisto;
46 
48  theGflashStep = new G4Step();
49  theGflashNavigator = new G4Navigator();
50  // theGflashNavigator = 0;
51  theGflashTouchableHandle = new G4TouchableHistory();
52 
53 #ifdef DebugLog
54  if (theFillHisto) {
56  if ( tfile.isAvailable() ) {
57  TFileDirectory showerDir = tfile->mkdir("GflashEMShowerProfile");
58 
59  em_incE = showerDir.make<TH1F>("em_incE","Incoming energy (GeV)",500,0,500.);
60  em_ssp_rho = showerDir.make<TH1F>("em_ssp_rho","Shower starting position;#rho (cm);Number of Events",100,100.0,200.0);
61  em_ssp_z = showerDir.make<TH1F>("em_ssp_z","Shower starting position;z (cm);Number of Events",2000,0.0,2000.0);
62  em_long = showerDir.make<TH1F>("em_long","Longitudinal Profile;Radiation Length;Number of Spots",800,800.0,1600.0);
63  em_lateral = showerDir.make<TH1F>("em_lateral","Lateral Profile;Radiation Length;Moliere Radius",100,0.0,5.0);
64  em_2d = showerDir.make<TH2F>("em_2d","Lateral Profile vs. Shower Depth;Radiation Length;Moliere Radius",800,800.0,1600.0,100,0.0,5.0);
65  em_long_sd = showerDir.make<TH1F>("em_long_sd","Longitudinal Profile in Sensitive Detector;Radiation Length;Number of Spots",800,800.0,1600.0);
66  em_lateral_sd = showerDir.make<TH1F>("em_lateral_sd","Lateral Profile vs. Shower Depth in Sensitive Detector;Radiation Length;Moliere Radius",100,0.0,5.0);
67  em_2d_sd = showerDir.make<TH2F>("em_2d_sd","Lateral Profile vs. Shower Depth in Sensitive Detector;Radiation Length;Moliere Radius",800,800.0,1600.0,100,0.0,5.0);
68  em_ze = showerDir.make<TH2F>("em_ze","Profile vs. Energy;Radiation Length;Moliere Radius",800,800.0,1600.0,1000,0.0,1.0);
69  em_ratio = showerDir.make<TH2F>("em_ratio","Profile vs. Energy;Radiation Length;Moliere Radius",800,800.0,1600.0,1000,0.0,100.0);
70  em_ratio_selected = showerDir.make<TH2F>("em_ratio_selected","Profile vs. Energy;Radiation Length;Moliere Radius",800,800.0,1600.0,1000,0.0,100.0);
71  em_nSpots_sd = showerDir.make<TH1F>("em_nSpots_sd","Number of Gflash Spots in Sensitive Detector;Number of Spots;Number of Events",100,0.0,100);
72  em_ze_ratio = showerDir.make<TH1F>("em_ze_ratio","Ratio of Energy and Z Position",1000,0.0,0.001);
73  } else {
74  theFillHisto = false;
75  edm::LogInfo("HFShower") << "HFGFlash::No file is available for saving"
76  << " histos so the flag is set to false";
77  }
78  }
79 #endif
81 
82 }
83 
85  delete theHelix;
86  delete theGflashStep;
87  delete theGflashNavigator;
88 }
89 
90 std::vector<HFGflash::Hit> HFGflash::gfParameterization(const G4Step * aStep, bool onlyLong) {
91  double tempZCalo = 26; // Gflash::Z[jCalorimeter]
92  double hfcriticalEnergy = 0.021; // Gflash::criticalEnergy
93 
94  std::vector<HFGflash::Hit> hit;
95  HFGflash::Hit oneHit;
96 
97  auto const preStepPoint = aStep->GetPreStepPoint();
98  auto const track = aStep->GetTrack();
99 
100  // This part of code is copied from the original GFlash Fortran code.
101  // reference : hep-ex/0001020v1
102 
103  const G4double energyCutoff = 1;
104  const G4int maxNumberOfSpots = 10000000;
105 
106  G4ThreeVector showerStartingPosition = track->GetPosition()/cm;
107  G4ThreeVector showerMomentum = preStepPoint->GetMomentum()/GeV;
109 
110  const G4double invgev = 1.0/CLHEP::GeV;
111  G4double energy = preStepPoint->GetTotalEnergy()*invgev; // energy left in GeV
112  G4double logEinc = std::log(energy);
113 
114  G4double y = energy / hfcriticalEnergy; // y = E/Ec, criticalEnergy is in GeV
115  G4double logY = std::log(y);
116 
117 
118  G4double nSpots = 93.0 * std::log(tempZCalo) * energy; // total number of spot due linearization
119  if(energy < 1.6) nSpots = 140.4 * std::log(tempZCalo) * std::pow(energy,0.876);
120 
121  // // implementing magnetic field effects
122  double charge = track->GetStep()->GetPreStepPoint()->GetCharge();
123  theHelix->initializeTrajectory(showerMomentum,showerStartingPosition,charge,theBField);
124 
125  G4double pathLength0 = theHelix->getPathLengthAtZ(showerStartingPosition.getZ());
126  G4double pathLength = pathLength0; // this will grow along the shower development
127 
128  //--- 2.2 Fix intrinsic properties of em. showers.
129 
130  G4double fluctuatedTmax = std::log(logY - 0.7157);
131  G4double fluctuatedAlpha= std::log(0.7996 +(0.4581 + 1.8628/tempZCalo)*logY);
132 
133  G4double sigmaTmax = 1.0/( -1.4 + 1.26 * logY);
134  G4double sigmaAlpha = 1.0/( -0.58 + 0.86 * logY);
135  G4double rho = 0.705 - 0.023 * logY;
136  G4double sqrtPL = std::sqrt((1.0+rho)/2.0);
137  G4double sqrtLE = std::sqrt((1.0-rho)/2.0);
138 
139  G4double norm1 = G4RandGauss::shoot();
140  G4double norm2 = G4RandGauss::shoot();
141  G4double tempTmax = fluctuatedTmax + sigmaTmax*(sqrtPL*norm1 + sqrtLE*norm2);
142  G4double tempAlpha = fluctuatedAlpha + sigmaAlpha*(sqrtPL*norm1 - sqrtLE*norm2);
143 
144  // tmax, alpha, beta : parameters of gamma distribution
145  G4double tmax = std::exp(tempTmax);
146  G4double alpha = std::exp(tempAlpha);
147  G4double beta = (alpha - 1.0)/tmax;
148 
149  if (!alpha) return hit;
150  if (!beta) return hit;
151  if (alpha < 0.00001) return hit;
152  if (beta < 0.00001) return hit;
153 
154  // spot fluctuations are added to tmax, alpha, beta
155  G4double averageTmax = logY-0.858;
156  G4double averageAlpha = 0.21+(0.492+2.38/tempZCalo)*logY;
157  G4double spotTmax = averageTmax * (0.698 + .00212*tempZCalo);
158  G4double spotAlpha= averageAlpha * (0.639 + .00334*tempZCalo);
159  G4double spotBeta = (spotAlpha-1.0)/spotTmax;
160 
161  if (!spotAlpha) return hit;
162  if (!spotBeta) return hit;
163  if (spotAlpha < 0.00001) return hit;
164  if (spotBeta < 0.00001) return hit;
165 
166 #ifdef DebugLog
167  LogDebug("HFShower") << "Incoming energy = " << energy << " Position (rho,z) = (" << showerStartingPosition.rho() << ", " << showerStartingPosition.z() << ")";
168 
169  if(theFillHisto) {
170  em_incE->Fill(energy);
171  em_ssp_rho->Fill(showerStartingPosition.rho());
172  em_ssp_z->Fill(std::abs(showerStartingPosition.z()));
173  }
174 #endif
175  // parameters for lateral distribution and fluctuation
176  G4double z1=0.0251+0.00319*logEinc;
177  G4double z2=0.1162-0.000381*tempZCalo;
178 
179  G4double k1=0.659 - 0.00309 * tempZCalo;
180  G4double k2=0.645;
181  G4double k3=-2.59;
182  G4double k4=0.3585+ 0.0421*logEinc;
183 
184  G4double p1=2.623 -0.00094*tempZCalo;
185  G4double p2=0.401 +0.00187*tempZCalo;
186  G4double p3=1.313 -0.0686*logEinc;
187 
188  // // @@@ dwjang, intial tuning by comparing 20-150GeV TB data
189  // // the width of energy response is not yet tuned.
190  const G4double e25Scale = 1.03551;
191  z1 *= 9.76972e-01 - 3.85026e-01 * std::tanh(1.82790e+00*std::log(energy) - 3.66237e+00);
192  p1 *= 0.96;
193 
194  G4double stepLengthLeft = 10000;
195  G4int nSpots_sd = 0; // count total number of spots in SD
196  G4double zInX0 = 0.0; // shower depth in X0 unit
197  G4double deltaZInX0 = 0.0; // segment of depth in X0 unit
198  G4double deltaZ = 0.0; // segment of depth in cm
199  G4double stepLengthLeftInX0 = 0.0; // step length left in X0 unit
200 
201  const G4double divisionStepInX0 = 0.1; //step size in X0 unit
202 
203  Genfun::IncompleteGamma gammaDist;
204 
205  G4double energyInGamma = 0.0; // energy in a specific depth(z) according to Gamma distribution
206  G4double preEnergyInGamma = 0.0; // energy calculated in a previous depth
207  G4double sigmaInGamma = 0.; // sigma of energy in a specific depth(z) according to Gamma distribution
208  G4double preSigmaInGamma = 0.0; // sigma of energy in a previous depth
209 
210  //energy segment in Gamma distribution of shower in each step
211  G4double deltaEnergy =0.0 ; // energy in deltaZ
212  G4int spotCounter = 0; // keep track of number of spots generated
213 
214  //step increment along the shower direction
215  G4double deltaStep = 0.0;
216 
217  // Uniqueness of G4Step is important otherwise hits won't be created.
218  G4double timeGlobal = preStepPoint->GetGlobalTime();
219 
220  // this needs to be deleted manually at the end of this loop.
221  // theGflashNavigator = new G4Navigator();
222  theGflashNavigator->SetWorldVolume(G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume());
223 
224  // // loop for longitudinal integration
225 
226 #ifdef DebugLog
227  LogDebug("HFShower") << " Energy = " << energy << " Step Length Left = " << stepLengthLeft;
228 #endif
229  while(energy > 0.0 && stepLengthLeft > 0.0) {
230  stepLengthLeftInX0 = stepLengthLeft / Gflash::radLength[jCalorimeter];
231 
232  if ( stepLengthLeftInX0 < divisionStepInX0 ) {
233  deltaZInX0 = stepLengthLeftInX0;
234  deltaZ = deltaZInX0 * Gflash::radLength[jCalorimeter];
235  stepLengthLeft = 0.0;
236  } else {
237  deltaZInX0 = divisionStepInX0;
238  deltaZ = deltaZInX0 * Gflash::radLength[jCalorimeter];
239  stepLengthLeft -= deltaZ;
240  }
241 
242  zInX0 += deltaZInX0;
243 
244 #ifdef DebugLog
245  LogDebug("HFShower") << " zInX0 = " << zInX0 << " spotBeta*zInX0 = " << spotBeta*zInX0;
246 #endif
247  if ((!zInX0) || (! (spotBeta*zInX0 != 0) ) || (zInX0 < 0.01) ||
248  (spotBeta*zInX0 < 0.00001) || (! (zInX0*beta != 0) ) || (zInX0*beta < 0.00001))
249  return hit;
250 
251  G4int nSpotsInStep = 0;
252 
253 #ifdef DebugLog
254  LogDebug("HFShower") << " Energy - Energy Cut off = " << energy - energyCutoff;
255 #endif
256 
257  if ( energy > energyCutoff ) {
258  preEnergyInGamma = energyInGamma;
259  gammaDist.a().setValue(alpha); //alpha
260 
261  energyInGamma = gammaDist(beta*zInX0); //beta
262  G4double energyInDeltaZ = energyInGamma - preEnergyInGamma;
263  deltaEnergy = std::min(energy,energy*energyInDeltaZ);
264 
265  preSigmaInGamma = sigmaInGamma;
266  gammaDist.a().setValue(spotAlpha); //alpha spot
267  sigmaInGamma = gammaDist(spotBeta*zInX0); //beta spot
268  nSpotsInStep = std::max(1,int(nSpots * (sigmaInGamma - preSigmaInGamma)));
269  } else {
270  deltaEnergy = energy;
271  preSigmaInGamma = sigmaInGamma;
272  nSpotsInStep = std::max(1,int(nSpots * (1.0 - preSigmaInGamma)));
273  }
274 
275  if ( deltaEnergy > energy || (energy-deltaEnergy) < energyCutoff ) deltaEnergy = energy;
276 
277  energy -= deltaEnergy;
278 
279  if ( spotCounter+nSpotsInStep > maxNumberOfSpots ) {
280  nSpotsInStep = maxNumberOfSpots - spotCounter;
281  if (nSpotsInStep < 1) nSpotsInStep = 1;
282  }
283 
284 
285  // // It begins with 0.5 of deltaZ and then icreases by 1 deltaZ
286  deltaStep += 0.5*deltaZ;
287  pathLength += deltaStep;
288  deltaStep = 0.5*deltaZ;
289 
290 
291  //lateral shape and fluctuations for homogenous calo.
292  G4double tScale = tmax *alpha/(alpha-1.0) * (1.0 - std::exp(-fluctuatedAlpha));
293  G4double tau = std::min(10.0,(zInX0 - 0.5*deltaZInX0)/tScale);
294  G4double rCore = z1 + z2 * tau;
295  G4double rTail = k1 *( std::exp(k3*(tau-k2)) + std::exp(k4*(tau-k2))); // @@ check RT3 sign
296  G4double p23 = (p2 - tau)/p3;
297  G4double probabilityWeight = p1 * std::exp( p23 - std::exp(p23) );
298 
299 
300  // Deposition of spots according to lateral distr.
301  // Apply absolute energy scale
302  // Convert into MeV unit
303  G4double emSpotEnergy = deltaEnergy / (nSpotsInStep * e25Scale * GeV);
304 
305 #ifdef DebugLog
306  LogDebug("HFShower") << " nSpotsInStep = " << nSpotsInStep;
307 #endif
308 
309  for (G4int ispot = 0 ; ispot < nSpotsInStep ; ispot++) {
310  spotCounter++;
311  G4double u1 = G4UniformRand();
312  G4double u2 = G4UniformRand();
313  G4double rInRM = 0.0;
314 
315  if (u1 < probabilityWeight) {
316  rInRM = rCore * std::sqrt( u2/(1.0-u2) );
317  } else {
318  rInRM = rTail * std::sqrt( u2/(1.0-u2) );
319  }
320 
321  G4double rShower = rInRM * Gflash::rMoliere[jCalorimeter];
322 
323  //Uniform & random rotation of spot along the azimuthal angle
324  G4double azimuthalAngle = twopi*G4UniformRand();
325 
326  //Compute global position of generated spots with taking into account magnetic field
327  //Divide deltaZ into nSpotsInStep and give a spot a global position
328  G4double incrementPath = (deltaZ/nSpotsInStep)*(ispot+0.5 - 0.5*nSpotsInStep);
329 
330  // trajectoryPoint give a spot an imaginary point along the shower development
331  GflashTrajectoryPoint trajectoryPoint;
332  theHelix->getGflashTrajectoryPoint(trajectoryPoint,pathLength+incrementPath);
333 
334  G4ThreeVector SpotPosition0 = trajectoryPoint.getPosition() + rShower*std::cos(azimuthalAngle)*trajectoryPoint.getOrthogonalUnitVector() + rShower*std::sin(azimuthalAngle)*trajectoryPoint.getCrossUnitVector();
335 
337  // Convert into mm unit
338  SpotPosition0 *= cm;
339 
340  //---------------------------------------------------
341  // fill a fake step to send it to hit maker
342  //---------------------------------------------------
343 
344  // to make a different time for each fake step. (0.03 nsec is corresponding to 1cm step size)
345  timeGlobal += 0.0001*nanosecond;
346 
347  //fill equivalent changes to a (fake) step associated with a spot
348 
349  G4double zInX0_spot = std::abs(pathLength+incrementPath - pathLength0)/Gflash::radLength[jCalorimeter];
350 
351 #ifdef DebugLog
352  LogDebug("HFShower") << "zInX0_spot,emSpotEnergy/GeV =" << zInX0_spot << " , " << emSpotEnergy/GeV << "emSpotEnergy/GeV =" << emSpotEnergy/GeV;
353 #endif
354 
355  if (zInX0_spot <= 0) continue;
356  if (emSpotEnergy <= 0) continue;
357  if (rShower/Gflash::rMoliere[jCalorimeter] <= 0) continue;
358 
359  oneHit.depth = 1;
360 
361 #ifdef DebugLog
362  if (theFillHisto) {
363  em_long->Fill(SpotPosition0.z()/cm,emSpotEnergy*invgev);
364  em_lateral->Fill(rShower/Gflash::rMoliere[jCalorimeter],emSpotEnergy*invgev);
365  em_2d->Fill(SpotPosition0.z()/cm,rShower/Gflash::rMoliere[jCalorimeter],emSpotEnergy*invgev);
366  }
367 #endif
368 
370  //if(SpotPosition0 == 0) continue;
371 
372  double energyratio = emSpotEnergy/(preStepPoint->GetTotalEnergy()/(nSpots*e25Scale));
373 
374  if (emSpotEnergy/GeV < 0.0001) continue;
375  if (energyratio > 80) continue;
376 
377  double zshift =0;
378  if(SpotPosition0.z() > 0) zshift = 18;
379  if(SpotPosition0.z() < 0) zshift = -18;
380 
381 
382  G4ThreeVector gfshift(0,0,zshift*(pow(100,0.1)/pow(energy,0.1)));
383 
384  G4ThreeVector SpotPosition = gfshift + SpotPosition0;
385 
386  double LengthWeight = std::fabs(std::pow(SpotPosition0.z()/11370,1));
387  if (G4UniformRand()> 0.0021 * energyratio * LengthWeight) continue;
388 
389  oneHit.position = SpotPosition;
390  oneHit.time = timeGlobal;
391  oneHit.edep = emSpotEnergy*invgev;
392  hit.push_back(oneHit);
393  nSpots_sd++;
394 
395  } // end of for spot iteration
396 
397  } // end of while for longitudinal integration
398 #ifdef DebugLog
399  if (theFillHisto) {
400  em_nSpots_sd->Fill(nSpots_sd);
401  }
402 #endif
403  // delete theGflashNavigator;
404  return hit;
405 }
#define LogDebug(id)
virtual ~HFGflash()
Definition: HFGflash.cc:84
T getParameter(std::string const &) const
G4Step * theGflashStep
Definition: HFGflash.h:50
T getUntrackedParameter(std::string const &, T const &) const
TH2F * em_ze
Definition: HFGflash.h:69
G4Navigator * theGflashNavigator
Definition: HFGflash.h:51
std::vector< Hit > gfParameterization(const G4Step *aStep, bool onlyLong=false)
Definition: HFGflash.cc:90
float alpha
Definition: AMPTWrapper.h:95
const double GeV
Definition: MathUtil.h:16
double getPathLengthAtZ(double z) const
Gflash3Vector getCrossUnitVector()
TH1F * em_ssp_z
Definition: HFGflash.h:67
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Gflash::CalorimeterNumber jCalorimeter
Definition: HFGflash.h:54
G4double theBField
Definition: HFGflash.h:58
bool theFillHisto
Definition: HFGflash.h:57
const double rMoliere[kNumberCalorimeter]
static const float invgev
Definition: TimingSD.cc:35
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
TH2F * em_ratio_selected
Definition: HFGflash.h:69
bool isAvailable() const
Definition: Service.h:46
G4TouchableHandle theGflashTouchableHandle
Definition: HFGflash.h:52
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double edep
Definition: HFGflash.h:41
T * make(const Args &...args) const
make new ROOT object
T min(T a, T b)
Definition: MathUtil.h:58
bool theWatcherOn
Definition: HFGflash.h:56
double p2[4]
Definition: TauolaWrapper.h:90
TH2F * em_ratio
Definition: HFGflash.h:69
TH1F * em_ze_ratio
Definition: HFGflash.h:68
HFGflash(edm::ParameterSet const &p)
Definition: HFGflash.cc:37
Gflash3Vector getOrthogonalUnitVector()
void getGflashTrajectoryPoint(GflashTrajectoryPoint &point, double s) const
static const double tmax[3]
TH1F * em_ssp_rho
Definition: HFGflash.h:67
const double radLength[kNumberCalorimeter]
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
double time
Definition: HFGflash.h:40
TH1F * em_lateral_sd
Definition: HFGflash.h:68
TH1F * em_nSpots_sd
Definition: HFGflash.h:68
TH1F * em_lateral
Definition: HFGflash.h:67
TH1F * em_long
Definition: HFGflash.h:67
Gflash3Vector & getPosition()
double p1[4]
Definition: TauolaWrapper.h:89
G4ThreeVector position
Definition: HFGflash.h:38
TH1F * em_incE
Definition: HFGflash.h:67
GflashTrajectory * theHelix
Definition: HFGflash.h:49
TH1F * em_long_sd
Definition: HFGflash.h:68
TH2F * em_2d
Definition: HFGflash.h:69
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
double p3[4]
Definition: TauolaWrapper.h:91
TH2F * em_2d_sd
Definition: HFGflash.h:69
void initializeTrajectory(const HepGeom::Vector3D< double > &, const HepGeom::Point3D< double > &, double q, double Field)