Monte Carlo LYSO
Geant4 simulation for the LYSO calorimeter prototype
detector.cc
Go to the documentation of this file.
1 
5 #include "detector.hh"
6 
7 MySensitiveDetector::MySensitiveDetector(G4String name, G4String hitsCollectionName) : G4VSensitiveDetector(name)
8 {
9  fEfficiencySetting = fIsNominalEfficiency;
10 
11  // Add the collection name to collectionName vector
12  collectionName.insert(hitsCollectionName);
13 
14  switch(fEfficiencySetting)
15  {
16  case fIsNominalEfficiency:
17  default:
18  fPDE = new G4PhysicsFreeVector(GS::pdeEnergies, GS::pdeValues);
19  break;
20  case fIsFixedEfficiency:
21  fFixedEfficiency = GS::meanPDE;
22  break;
23  case fIsRandomEfficiency:
25  break;
26  case fIsAssignedEfficiency:
28  break;
29  }
30 }
31 
32 
33 
34 void MySensitiveDetector::Initialize(G4HCofThisEvent *hce)
35 {
36  // Add fHitsCollection to the GHCofThisEvent container
37  fHitsCollection = new MyHitsCollection(SensitiveDetectorName, collectionName[0]);
38  G4int hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
39 
40  hce->AddHitsCollection(hcID, fHitsCollection);
41 }
42 
43 
44 
45 G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
46 {
47  // Access the useful objects
48  G4Track *track = aStep->GetTrack();
49  G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
50  const G4VTouchable *touchable = preStepPoint->GetTouchable();
51  G4double phEnergy = preStepPoint->GetTotalEnergy();
52 
53  // Here's implemented the PDE
54  switch(fEfficiencySetting)
55  {
56  case fIsNominalEfficiency:
57  if(G4UniformRand() > fPDE->Value(phEnergy)) return false;
58  break;
59  case fIsFixedEfficiency:
60  if(G4UniformRand() > fFixedEfficiency) return false;
61  break;
62  case fIsRandomEfficiency:
63  case fIsAssignedEfficiency:
64  G4int ch = touchable->GetCopyNumber(2);
65  G4bool isFront = (touchable->GetVolume(2)->GetTranslation().z() < GS::zScintillator);
66  if(isFront)
67  {
68  if(G4UniformRand() > fFrontEfficiency[ch]) return false;
69  }
70  else
71  {
72  if(G4UniformRand() > fBackEfficiency[ch]) return false;
73  }
74  break;
75  }
76 
77  // Save, stop and kill only optical photons
78  if(track->GetParticleDefinition()->GetPDGEncoding()==-22)
79  track->SetTrackStatus(fStopAndKill);
80  else
81  return false;
82 
83 
84  // Create a new MyHit object
85  MyHit *newHit = new MyHit();
86 
87  // Time of detection
88  newHit->SetDetectionTime(preStepPoint->GetGlobalTime());
89 
90  // Position of detector (Note that the position of the package is taken)
91  newHit->SetDetectorPosition(touchable->GetVolume(2)->GetTranslation());
92 
93  // Channel of detector
94  newHit->SetDetectorChannel(touchable->GetCopyNumber(2));
95 
96 
97  // Insert the hit
98  fHitsCollection->insert(newHit);
99 
100  return true;
101 }
102 
103 
104 
106 {
107  G4cout << "\n Randomization of efficiencies... \n" << G4endl;
108 
109  // Create the efficiencies file
110  std::ofstream file("random_efficiencies.txt");
111 
112  // Check if file is opened
113  if(!file.is_open())
114  {
115  G4cerr << "Can't open the file!" << G4endl;
116  return;
117  }
118 
119  file << "# Channel Eff Front Eff Back" << G4endl;
120 
121  // Generate and save efficiencies
122  for(G4int ch = 0; ch < GS::nOfSiPMs; ch++)
123  {
124  fFrontEfficiency[ch] = (fPDE->GetMaxValue()/2)*(1 + G4UniformRand());
125  fBackEfficiency[ch] = (fPDE->GetMaxValue()/2)*(1 + G4UniformRand());
126 
127  file << ch << "\t" << fFrontEfficiency[ch] << "\t" << fBackEfficiency[ch] << G4endl;
128  }
129 
130  // Close the file
131  file.close();
132 }
133 
134 
135 
137 {
138  // Open the file
139  std::ifstream file("random_efficiencies.txt");
140 
141  // Check if file is opened
142  if(!file.is_open())
143  {
144  G4cerr << "Can't open the file!" << G4endl;
145  return;
146  }
147 
148  G4String line;
149  G4int ch = 0;
150 
151  // Read data
152  while(std::getline(file, line))
153  {
154  // Ignore '#' lines
155  if(line[0] == '#')
156  continue;
157 
158  std::istringstream iss(line);
159  G4double effFront, effBack;
160 
161  // Parsing of the line
162  if(!(iss >> ch >> effFront >> effBack))
163  {
164  G4cerr << "Error in reading the file!" << G4endl;
165  break;
166  }
167 
168  // Assign values
169  fFrontEfficiency[ch] = effFront;
170  fBackEfficiency[ch] = effBack;
171  }
172 
173  // Close the file
174  file.close();
175 
176  G4cout << "\n Data read from random_efficiencies.txt successfully. \n" << G4endl;
177 }
Concrete class of G4VHit, representing a hit in the MySensitiveDetector.
Definition: hit.hh:20
void SetDetectorChannel(G4int ch)
Set the channel of the SiPM hit by the optical photon.
Definition: hit.hh:36
void SetDetectionTime(G4double t)
Set the detection time of the optical photon.
Definition: hit.hh:34
void SetDetectorPosition(G4ThreeVector xyz)
Set the position (center) of the SiPM hit by the optical photon.
Definition: hit.hh:35
G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist) override
For every optical photon that hits the SD this method instantiates a MyHit object,...
Definition: detector.cc:45
G4double fBackEfficiency[GS::nOfSiPMs]
Array of PDEs for back MPPCs.
Definition: detector.hh:71
void Initialize(G4HCofThisEvent *hce) override
Associates a new MyHitsCollection with a G4HCofThisEvent object at the beginning of each event.
Definition: detector.cc:34
void GetEfficienciesFromFile()
Reads and sets the efficiencies from the file.
Definition: detector.cc:136
MySensitiveDetector(G4String name, G4String hitsCollectionName)
Constructor of the class.
Definition: detector.cc:7
SetEfficiencies fEfficiencySetting
Type of SetEfficiencies.
Definition: detector.hh:67
MyHitsCollection * fHitsCollection
Pointer to the hits collection of the event.
Definition: detector.hh:64
G4double fFrontEfficiency[GS::nOfSiPMs]
Array of PDEs for front MPPCs.
Definition: detector.hh:70
void RandomizeEfficiencies()
Fixes random efficiencies for all MPPCs.
Definition: detector.cc:105
Declaration of the class MySensitiveDetector.
G4THitsCollection< MyHit > MyHitsCollection
Concrete hit collection class for MyHit.
Definition: hit.hh:54
constexpr G4double zScintillator
z-position of the scintillator crystal.
constexpr G4int nOfSiPMs
The number of SiPMs on a detector face.