Monte Carlo LYSO
Geant4 simulation for the LYSO calorimeter prototype
summary.cc
Go to the documentation of this file.
1 
6 #include "summary.hh"
7 
8 G4int beamType = 0, modeType = 0;
9 G4bool boolLightGuide = false;
10 
11 
12 
13 G4String extract_value(const G4String& line, const G4String& keyword)
14 {
15  // Find the position of the keyword in the line
16  size_t start = line.find(keyword);
17 
18  // If the keyword is found, extract the value following it
19  if(start != G4String::npos)
20  {
21  start += keyword.length();
22  G4String value = line.substr(start);
23  return value;
24  }
25 
26  // Return an empty string if the keyword is not found
27  return "";
28 }
29 
30 
31 
32 void MC_summary(G4String macrofile, G4int seed, G4double duration, const G4String& output_filename)
33 {
34  // Open the output file in append mode
35  std::ofstream outfile(output_filename, std::ios::app);
36 
37  // Print general information about the simulation (seed, date, username, duration)
38  outfile << "MonteCarlo serial number (Seed): " << seed << G4endl;
39  outfile << G4endl;
40  std::time_t now = std::time(0);
41  std::tm* current_time = std::localtime(&now);
42  outfile << "Date: " << std::asctime(current_time);
43  outfile << "User Name: " << getlogin() << G4endl;
44  outfile << "Duration of the simulation: " << duration << " s" << G4endl;
45  outfile << G4endl;
46 
47  // Open the run macro file in read mode
48  std::ifstream run_file(macrofile);
49  if(!run_file)
50  {
51  G4cerr << "Can't open run macro file!" << G4endl;
52  return;
53  }
54 
55  G4String line;
56 
57  // Scan the run macro file
58  while(std::getline(run_file, line))
59  {
60  // Trim leading whitespaces and tabs from each line
61  line = line.substr(line.find_first_not_of(" \t"));
62 
63  // Find all the settings and write them on the output file
64  if(line.find("/MC_LYSO/Mode") != G4String::npos)
65  {
66  modeType = std::stoi(extract_value(line, "/MC_LYSO/Mode"));
67  switch(modeType)
68  {
69  case 10:
70  default:
71  outfile << "Mode: Standard - Pointlike beam" << G4endl;
72  break;
73  case 11:
74  outfile << "Mode: Standard - Spread beam" << G4endl;
75  break;
76  case 12:
77  outfile << "Mode: Standard - Circle beam" << G4endl;
78  break;
79  case 20:
80  outfile << "Mode: 176Lu decay" << G4endl;
81  break;
82  case 21:
83  outfile << "Mode: 176Lu decay - Fixed position" << G4endl;
84  break;
85  case 22:
86  outfile << "Mode: 176Lu decay - Si Trigger" << G4endl;
87  break;
88  case 30:
89  outfile << "Mode: Cosmic rays" << G4endl;
90  break;
91  case 40:
92  outfile << "Mode: LED system" << G4endl;
93  break;
94  }
95  }
96  else if(modeType == 11 && line.find("/MC_LYSO/myGun/radiusSpread") != G4String::npos)
97  {
98  G4String radius_value = extract_value(line, "/MC_LYSO/myGun/radiusSpread");
99  if(!radius_value.empty())
100  {
101  outfile << "RadiusSpread: " << radius_value << G4endl;
102  }
103  }
104  else if(modeType == 12 && line.find("/MC_LYSO/myGun/radiusCircle") != G4String::npos)
105  {
106  G4String radius_value = extract_value(line, "/MC_LYSO/myGun/radiusCircle");
107  if(!radius_value.empty())
108  {
109  outfile << "RadiusCircle: " << radius_value << G4endl;
110  }
111  }
112  else if((modeType == 10 || modeType == 11 || modeType == 12) && line.find("/MC_LYSO/myGun/meanEnergy") != G4String::npos)
113  {
114  G4String energy_value = extract_value(line, "/MC_LYSO/myGun/meanEnergy");
115  if(!energy_value.empty())
116  {
117  outfile << "Energy: " << energy_value << G4endl;
118  }
119  }
120  else if((modeType == 10 || modeType == 11 || modeType == 12) && line.find("/MC_LYSO/myGun/sigmaEnergy") != G4String::npos)
121  {
122  G4String sigma_value = extract_value(line, "/MC_LYSO/myGun/sigmaEnergy");
123  if(!sigma_value.empty())
124  {
125  outfile << "SigmaEnergy: " << sigma_value << G4endl;
126  }
127  }
128  else if(modeType == 21 && line.find("/MC_LYSO/myGun/posLuDecay") != G4String::npos)
129  {
130  G4String posdecay_value = extract_value(line, "/MC_LYSO/myGun/posLuDecay");
131  if(!posdecay_value.empty())
132  {
133  outfile << "176Lu isotope position: " << posdecay_value << G4endl;
134  }
135  }
136  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/FrontOrBack F") != G4String::npos)
137  {
138  outfile << "LED of Front detector" << G4endl;
139  }
140  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/FrontOrBack B") != G4String::npos)
141  {
142  outfile << "LED of Back detector" << G4endl;
143  }
144  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/switchOnLED u") != G4String::npos)
145  {
146  outfile << "LED turned on: up" << G4endl;
147  }
148  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/switchOnLED d") != G4String::npos)
149  {
150  outfile << "LED turned on: down" << G4endl;
151  }
152  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/switchOnLED r") != G4String::npos)
153  {
154  outfile << "LED turned on: right" << G4endl;
155  }
156  else if(modeType == 40 && line.find("/MC_LYSO/myGun/LED-System/switchOnLED l") != G4String::npos)
157  {
158  outfile << "LED turned on: left" << G4endl;
159  }
160  else if(line.find("/run/beamOn") != G4String::npos)
161  {
162  G4String events_value = extract_value(line, "/run/beamOn");
163  if(!events_value.empty())
164  {
165  outfile << "Number of events: " << events_value << G4endl;
166  }
167  }
168  }
169 
170  run_file.close();
171 
172  // Open the "construction.mac" file in read mode
173  std::ifstream construction_file("construction.mac");
174  if(!construction_file)
175  {
176  G4cerr << "Can't open file 'construction.mac'" << G4endl;
177  return;
178  }
179 
180  // Scan the "construction.mac" file
181  while(std::getline(construction_file, line))
182  {
183  line = line.substr(line.find_first_not_of(" \t"));
184 
185  if(line.find("/MC_LYSO/myConstruction/isOpticalGrease true") != G4String::npos)
186  {
187  outfile << "Optical Grease: ON" << G4endl;
188  }
189  else if(line.find("/MC_LYSO/myConstruction/isOpticalGrease false") != G4String::npos)
190  {
191  outfile << "Optical Grease: OFF" << G4endl;
192  }
193  else if(line.find("/MC_LYSO/myConstruction/isLightGuide true") != G4String::npos)
194  {
195  outfile << "Light Guide: ON" << G4endl;
196  boolLightGuide = true;
197  }
198  else if(line.find("/MC_LYSO/myConstruction/isLightGuide false") != G4String::npos)
199  {
200  outfile << "Light Guide: OFF" << G4endl;
201  boolLightGuide = false;
202  }
203  else if(boolLightGuide && line.find("/MC_LYSO/myConstruction/MaterialOfLightGuide") != G4String::npos)
204  {
205  G4String material_value = extract_value(line, "/MC_LYSO/myConstruction/MaterialOfLightGuide");
206  if(!material_value.empty())
207  {
208  outfile << "Material of Light Guide: " << material_value << G4endl;
209  }
210  }
211  else if(line.find("/MC_LYSO/myConstruction/isPCB true") != G4String::npos)
212  {
213  outfile << "PCB: ON" << G4endl;
214  }
215  else if(line.find("/MC_LYSO/myConstruction/isPCB false") != G4String::npos)
216  {
217  outfile << "PCB: OFF" << G4endl;
218  }
219  else if(line.find("/MC_LYSO/myConstruction/isEndcap true") != G4String::npos)
220  {
221  outfile << "Endcap: ON" << G4endl;
222  }
223  else if(line.find("/MC_LYSO/myConstruction/isEndcap false") != G4String::npos)
224  {
225  outfile << "Endcap: OFF" << G4endl;
226  }
227  else if(line.find("/MC_LYSO/myConstruction/isCosmicRaysDetectors true") != G4String::npos)
228  {
229  outfile << "Cosmic rays detectors: ON" << G4endl;
230  }
231  else if(line.find("/MC_LYSO/myConstruction/isCosmicRaysDetectors false") != G4String::npos)
232  {
233  outfile << "Cosmic rays detectors: OFF" << G4endl;
234  }
235  }
236 
237  construction_file.close();
238 
239  outfile << G4endl;
240  outfile << "########################################################" << G4endl;
241  outfile << G4endl;
242 }
void MC_summary(G4String macrofile, G4int seed, G4double duration, const G4String &output_filename)
Writes the summary of the Monte Carlo to a text file, updating it at the end of every simulation.
Definition: summary.cc:32
G4String extract_value(const G4String &line, const G4String &keyword)
Auxiliary function called by MC_summary() to extract settings from the macro files used for the Monte...
Definition: summary.cc:13
Declaration of the function MC_summary() (and the auxiliary function extract_value())