REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4VetoAnalysisProcess.cxx
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2020 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
65
66#include "TRestGeant4VetoAnalysisProcess.h"
67
68using namespace std;
69
71
72TRestGeant4VetoAnalysisProcess::TRestGeant4VetoAnalysisProcess() { Initialize(); }
73
74TRestGeant4VetoAnalysisProcess::TRestGeant4VetoAnalysisProcess(const char* configFilename) {
76 if (LoadConfigFromFile(configFilename)) {
78 }
79}
80
81TRestGeant4VetoAnalysisProcess::~TRestGeant4VetoAnalysisProcess() { delete fOutputEvent; }
82
86void TRestGeant4VetoAnalysisProcess::LoadDefaultConfig() { SetTitle("Default config"); }
87
93 SetSectionName(this->ClassName());
94 SetLibraryVersion(LIBRARY_VERSION);
95
96 fOutputEvent = new TRestGeant4Event();
97}
98
111void TRestGeant4VetoAnalysisProcess::LoadConfig(const string& configFilename, const string& name) {
112 if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
113}
114
119 // CAREFUL THIS METHOD IS CALLED TWICE!
120 fVetoVolumes.clear();
121 fVetoDetectorVolumes.clear();
122 fVetoDetectorBoundaryDirection.clear();
123 fVetoDetectorBoundaryPosition.clear();
124
125 if (fGeant4Metadata == nullptr) {
126 // maybe it was manually initialized
127 fGeant4Metadata = GetMetadata<TRestGeant4Metadata>();
128 }
129 if (fGeant4Metadata == nullptr) {
130 cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: Geant4 metadata not found" << endl;
131 exit(1);
132 }
133
134 const auto& geometryInfo = fGeant4Metadata->GetGeant4GeometryInfo();
135
136 fVetoVolumes = geometryInfo.GetAllPhysicalVolumesMatchingExpression(fVetoVolumesExpression);
137 if (fVetoVolumes.empty()) {
138 const auto logicalVolumes =
139 geometryInfo.GetAllLogicalVolumesMatchingExpression(fVetoVolumesExpression);
140 for (const auto& logicalVolume : logicalVolumes) {
141 for (const auto& physicalVolume : geometryInfo.GetAllPhysicalVolumesFromLogical(logicalVolume)) {
142 fVetoVolumes.push_back(geometryInfo.GetAlternativeNameFromGeant4PhysicalName(physicalVolume));
143 }
144 }
145 }
146 if (fVetoVolumes.empty()) {
147 cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: No veto volumes found" << endl;
148 exit(1);
149 }
150
151 // get detector volumes if requested
152 if (!fVetoDetectorsExpression.IsNull()) {
153 fVetoDetectorVolumes = geometryInfo.GetAllPhysicalVolumesMatchingExpression(fVetoDetectorsExpression);
154 if (fVetoDetectorVolumes.empty()) {
155 const auto logicalVolumes =
156 geometryInfo.GetAllLogicalVolumesMatchingExpression(fVetoDetectorsExpression);
157 for (const auto& logicalVolume : logicalVolumes) {
158 for (const auto& physicalVolume :
159 geometryInfo.GetAllPhysicalVolumesFromLogical(logicalVolume)) {
160 fVetoDetectorVolumes.push_back(
161 geometryInfo.GetAlternativeNameFromGeant4PhysicalName(physicalVolume));
162 }
163 }
164 }
165 if (fVetoDetectorVolumes.empty()) {
166 cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: No detector volumes found" << endl;
167 exit(1);
168 }
169 if (fVetoDetectorVolumes.size() != fVetoVolumes.size()) {
170 cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: Number of detector volumes "
171 << "does not match number of veto volumes" << endl;
172 exit(1);
173 }
174 }
175
176 for (unsigned int i = 0; i < fVetoDetectorVolumes.size(); i++) {
177 const auto& vetoName = fVetoVolumes[i];
178 const auto& vetoPosition = geometryInfo.GetPosition(vetoName);
179
180 const auto& vetoDetectorName = fVetoDetectorVolumes[i];
181 const auto& vetoDetectorPosition = geometryInfo.GetPosition(vetoDetectorName);
182
183 const auto distance = vetoDetectorPosition - vetoPosition;
184 const auto direction = distance.Unit();
185
186 fVetoDetectorBoundaryDirection[vetoName] = direction;
187 fVetoDetectorBoundaryPosition[vetoName] = vetoDetectorPosition - direction * fVetoDetectorOffsetSize;
188 }
189
191}
192
197 /*
198 fInputEvent = (TRestGeant4Event*)inputEvent;
199 *fOutputEvent = *((TRestGeant4Event*)inputEvent);
200
201 std::map<string, Double_t> volume_energy_map;
202
203 for (unsigned int i = 0; i < fVetoVolumeIds.size(); i++) {
204 int id = fVetoVolumeIds[i];
205 string volume_name = (string)fGeant4Metadata->GetActiveVolumeName(id);
206
207 Double_t energy = fOutputG4Event->GetEnergyDepositedInVolume(id);
208 volume_energy_map[volume_name] = energy;
209 }
210
211 Double_t energy_veto_max = 0;
212 for (const auto& pair : volume_energy_map) {
213 Double_t veto_energy = pair.second;
214 SetObservableValue(pair.first + "VolumeEDep", veto_energy);
215 if (veto_energy > energy_veto_max) {
216 energy_veto_max = veto_energy;
217 };
218 }
219 SetObservableValue("vetoAllEVetoMax", energy_veto_max);
220
221 // veto groups
222 for (const auto& pair : fVetoGroupVolumeNames) {
223 Double_t energy_veto_max_group = 0;
224 for (unsigned int i = 0; i < pair.second.size(); i++) {
225 string volume_name = pair.second[i];
226 Double_t veto_energy = volume_energy_map[volume_name];
227 if (veto_energy > energy_veto_max_group) {
228 energy_veto_max_group = veto_energy;
229 };
230 }
231 // convert to Upper + lower case (VetoGroupTopEVetoMax, ...)
232 string group_name;
233 for (auto it = pair.first.cbegin(); it != pair.first.cend(); ++it) {
234 if (it == pair.first.cbegin()) {
235 group_name += std::toupper(*it);
236 } else {
237 group_name += std::tolower(*it);
238 }
239 }
240 SetObservableValue("vetoGroup" + group_name + "EVetoMax", energy_veto_max_group);
241 }
242
243 //
244
245 for (const auto& quenching_factor : fQuenchingFactors) {
246 string quenching_factor_string = std::to_string(quenching_factor);
247 // replace "." in string by "_" because its gives very strange problems
248 quenching_factor_string =
249 quenching_factor_string.replace(quenching_factor_string.find("."), sizeof(".") - 1, "_");
250 volume_energy_map.clear();
251 for (int i = 0; i < fOutputG4Event->GetNumberOfTracks(); i++) {
252 auto track = fOutputG4Event->GetTrack(i);
253 string particle_name = (string)track.GetParticleName();
254 for (const auto& id : fVetoVolumeIds) {
255 string volume_name = (string)fGeant4Metadata->GetActiveVolumeName(id);
256
257 if (particle_name == "e-" || particle_name == "e+" || particle_name == "gamma") {
258 // no quenching factor
259 volume_energy_map[volume_name] += track.GetEnergyInVolume(id);
260 } else {
261 // apply quenching factor
262 volume_energy_map[volume_name] += quenching_factor * track.GetEnergyInVolume(id);
263 }
264 }
265 }
266
267 Double_t energy_veto_max = 0;
268 for (const auto& pair : volume_energy_map) {
269 Double_t veto_energy = pair.second;
270 SetObservableValue(pair.first + "VolumeEDep" + "Qf" + quenching_factor_string, veto_energy);
271 if (veto_energy > energy_veto_max) {
272 energy_veto_max = veto_energy;
273 };
274 }
275 SetObservableValue(string("vetoAllEVetoMax") + "Qf" + quenching_factor_string, energy_veto_max);
276
277 // veto groups
278 for (const auto& pair : fVetoGroupVolumeNames) {
279 Double_t energy_veto_max_group = 0;
280 for (unsigned int i = 0; i < pair.second.size(); i++) {
281 string volume_name = pair.second[i];
282 Double_t veto_energy = volume_energy_map[volume_name];
283 if (veto_energy > energy_veto_max_group) {
284 energy_veto_max_group = veto_energy;
285 };
286 }
287 // convert to Upper + lower case (VetoGroupTopEVetoMax, ...)
288 string group_name;
289 for (auto it = pair.first.cbegin(); it != pair.first.cend(); ++it) {
290 if (it == pair.first.cbegin()) {
291 group_name += std::toupper(*it);
292 } else {
293 group_name += std::tolower(*it);
294 }
295 }
296 SetObservableValue("vetoGroup" + group_name + "EVetoMax" + "Qf" + quenching_factor_string,
297 energy_veto_max_group);
298 }
299 }
300
301 return fOutputG4Event;
302 */
303 return fInputEvent;
304}
305
311 // Function to be executed once at the end of the process
312 // (after all events have been processed)
313
314 // Start by calling the EndProcess function of the abstract class.
315 // Comment this if you don't want it.
316 // TRestEventProcess::EndProcess();
317}
318
324 // word to identify active volume as veto (default = "veto" e.g. "vetoTop")
325 fVetoVolumesExpression = GetParameter("vetoVolumesExpression", fVetoVolumesExpression);
326 fVetoDetectorsExpression = GetParameter("vetoDetectorsExpression", fVetoDetectorsExpression);
327
328 fVetoDetectorOffsetSize = GetDblParameterWithUnits("vetoDetectorOffset", fVetoDetectorOffsetSize);
329 fVetoLightAttenuation = GetDblParameterWithUnits("vetoLightAttenuation", fVetoLightAttenuation);
330 fVetoQuenchingFactor = GetDblParameterWithUnits("quenchingFactor", fVetoQuenchingFactor);
331}
332
335
336 cout << "Veto volume expression: " << fVetoVolumesExpression << endl;
337 if (!fVetoDetectorsExpression.IsNull()) {
338 cout << "Veto detector expression: " << fVetoDetectorsExpression << endl;
339 cout << "Veto detector offset: " << fVetoDetectorOffsetSize << endl;
340 cout << "Veto light attenuation: " << fVetoLightAttenuation << endl;
341 } else {
342 cout << "Veto detector expression: not set" << endl;
343 }
344 cout << "Veto quenching factor: " << fVetoQuenchingFactor << endl;
345
346 RESTDebug << RESTendl;
347
348 if (fVetoVolumes.empty()) {
349 cout << "Process not initialized yet" << endl;
350 return;
351 }
352
353 cout << "Number of veto volumes: " << fVetoVolumes.size() << endl;
354 cout << "Number of veto detector volumes: " << fVetoDetectorVolumes.size() << endl;
355
356 const auto& geometryInfo = fGeant4Metadata->GetGeant4GeometryInfo();
357 for (unsigned int i = 0; i < fVetoVolumes.size(); i++) {
358 const auto& vetoName = fVetoVolumes[i];
359 const auto& vetoPosition = geometryInfo.GetPosition(vetoName);
360
361 cout << TString::Format(" - Veto volume: %d - name: '%s' - position: %s mm", i, vetoName.Data(),
362 VectorToString(vetoPosition).c_str())
363 << endl;
364
365 if (fVetoDetectorVolumes.empty()) {
366 continue;
367 }
368
369 const auto& vetoDetectorName = fVetoDetectorVolumes[i];
370 const auto& vetoDetectorPosition = geometryInfo.GetPosition(vetoDetectorName);
371
372 cout << TString::Format(" Veto detector name: '%s' - position: %s mm", vetoDetectorName.Data(),
373 VectorToString(vetoDetectorPosition).c_str())
374 << endl;
375
376 cout << TString::Format(" Boundary position: %s mm - direction: %s",
377 VectorToString(fVetoDetectorBoundaryPosition.at(vetoName)).c_str(),
378 VectorToString(fVetoDetectorBoundaryDirection.at(vetoName)).c_str());
379 }
380}
void BeginPrintProcess()
[name, cut range]
A base class for any REST event.
Definition: TRestEvent.h:38
An event class to store geant4 generated event information.
const TRestGeant4GeometryInfo & GetGeant4GeometryInfo() const
Returns an immutable reference to the geometry info.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void EndProcess() override
Function to include required actions after all events have been processed.
void InitFromConfigFile() override
Function to read input parameters from the RML TRestGeant4VetoAnalysisProcess metadata section.
void Initialize() override
Function to initialize input/output event members and define the section name.
void InitProcess() override
Process initialization.
endl_t RESTendl
Termination flag object for TRestStringOutput.
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
void SetLibraryVersion(TString version)
Set the library version of this metadata class.
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.