REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4ToDetectorHitsProcess.cxx
1 /*************************************************************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 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 
62 #include "TRestGeant4ToDetectorHitsProcess.h"
63 
64 using namespace std;
65 
67 
72 
86  Initialize();
87 
88  if (LoadConfigFromFile(configFilename)) {
89  LoadDefaultConfig();
90  }
91 }
92 
97 
102  SetTitle("Default config");
103 
104  cout << "Geant4 to hits metadata not found. Loading default values" << endl;
105 }
106 
112  SetSectionName(this->ClassName());
113  SetLibraryVersion(LIBRARY_VERSION);
114 
115  fGeant4Event = nullptr;
116  fHitsEvent = new TRestDetectorHitsEvent();
117 }
118 
131 void TRestGeant4ToDetectorHitsProcess::LoadConfig(const string& configFilename, const string& name) {
132  if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
133 }
134 
140  fGeant4Metadata = GetMetadata<TRestGeant4Metadata>();
141 
142  for (const auto& userVolume : fVolumeSelection) {
143  if (fGeant4Metadata->GetActiveVolumeID(userVolume) >= 0) {
144  fVolumeId.push_back(fGeant4Metadata->GetActiveVolumeID(userVolume));
145  } else if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning)
146  cout << "TRestGeant4ToDetectorHitsProcess. volume name : " << userVolume
147  << " not found and will not be added." << endl;
148  }
149 
150  sort(fVolumeId.begin(), fVolumeId.end());
151  fVolumeId.erase(unique(fVolumeId.begin(), fVolumeId.end()), fVolumeId.end());
152 
153  for (size_t i = 0; i < fVolumeId.size(); i++) {
154  RESTDebug << "TRestGeant4ToDetectorHitsProcess. Volume id : " << fVolumeId[i]
155  << " name : " << fGeant4Metadata->GetActiveVolumeName(fVolumeId[i]) << RESTendl;
156  }
157 
158  RESTDebug << "Active volumes available in TRestGeant4Metadata" << RESTendl;
159  RESTDebug << "-------------------------------------------" << RESTendl;
160  for (unsigned int n = 0; n < fGeant4Metadata->GetNumberOfActiveVolumes(); n++) {
161  RESTDebug << "Volume id : " << n << " name : " << fGeant4Metadata->GetActiveVolumeName(n) << RESTendl;
162  }
163  RESTDebug << RESTendl;
164 
165  RESTDebug << "TRestGeant4HitsProcess volumes enabled in RML : ";
166  RESTDebug << "-------------------------------------------" << RESTendl;
167  if (fVolumeSelection.empty())
168  RESTDebug << "all" << RESTendl;
169  else {
170  for (const auto& volume : fVolumeSelection) {
171  RESTDebug << "" << RESTendl;
172  RESTDebug << " - " << volume << RESTendl;
173  }
174  RESTDebug << " " << RESTendl;
175  }
176 
177  if (!fVolumeSelection.empty() && fVolumeSelection.size() != fVolumeId.size())
178  RESTWarning << "TRestGeant4ToDetectorHitsProcess. Not all volumes were properly identified!"
179  << RESTendl;
180 
181  if (!fVolumeId.empty()) {
182  RESTDebug << "TRestGeant4HitsProcess volumes identified : ";
183  RESTDebug << "---------------------------------------" << RESTendl;
184  if (fVolumeSelection.empty())
185  RESTDebug << "all" << RESTendl;
186  else
187  for (const auto& volume : fVolumeSelection) {
188  RESTDebug << "" << RESTendl;
189  RESTDebug << " - " << volume << RESTendl;
190  }
191  RESTDebug << " " << RESTendl;
192  }
193 }
194 
199  fGeant4Event = (TRestGeant4Event*)inputEvent;
200 
201  fGeant4Event->InitializeReferences(GetRunInfo());
202 
203  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme) {
204  cout << "------ TRestGeant4ToDetectorHitsProcess --- Printing Input Event --- START ----" << endl;
205  fGeant4Event->PrintEvent();
206  cout << "------ TRestGeant4ToDetectorHitsProcess --- Printing Input Event ---- END ----" << endl;
207  GetChar();
208  }
209 
210  fHitsEvent->SetRunOrigin(fGeant4Event->GetRunOrigin());
211  fHitsEvent->SetSubRunOrigin(fGeant4Event->GetSubRunOrigin());
212  fHitsEvent->SetID(fGeant4Event->GetID());
213  fHitsEvent->SetSubID(fGeant4Event->GetSubID());
214  fHitsEvent->SetSubEventTag(fGeant4Event->GetSubEventTag());
215  fHitsEvent->SetTimeStamp(fGeant4Event->GetTimeStamp());
216  fHitsEvent->SetState(fGeant4Event->isOk());
217 
218  for (const auto& track : fGeant4Event->GetTracks()) {
219  const auto& hits = track.GetHits();
220  for (unsigned int i = 0; i < track.GetNumberOfHits(); i++) {
221  const auto energy = hits.GetEnergy(i);
222  if (energy <= 0) {
223  continue;
224  }
225  const TVector3& position = hits.GetPosition(i);
226  const double time = hits.GetTime(i);
227 
228  if (time < 0) {
229  cerr << "TRestGeant4ToDetectorHitsProcess. Negative time found. This should never happen"
230  << endl;
231  exit(1);
232  }
233  if (fVolumeId.empty()) {
234  // if no volume is selected, all hits are added
235  fHitsEvent->AddHit(position.X(), position.Y(), position.Z(), energy, time);
236  } else {
237  const string volumeName = hits.GetVolumeName(i).Data();
238  const auto volumeId = fGeant4Metadata->GetActiveVolumeID(volumeName);
239 
240  // cout << "volumeName : " << volumeName << " volumeId : " << volumeId << endl;
241  if (find(fVolumeId.begin(), fVolumeId.end(), volumeId) != fVolumeId.end()) {
242  const REST_HitType type = fHitTypes.at(volumeName);
243 
244  fHitsEvent->AddHit(position, energy, time, type);
245  }
246  }
247  }
248  }
249 
250  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
251  cout << "TRestGeant4ToDetectorHitsProcess. Hits added : " << fHitsEvent->GetNumberOfHits() << endl;
252  cout << "TRestGeant4ToDetectorHitsProcess. Hits total energy : " << fHitsEvent->GetTotalEnergy()
253  << endl;
254  }
255 
256  return fHitsEvent;
257 }
258 
264  // Attempt to access TRestGeant4Metadata
265  fGeant4Metadata = GetMetadata<TRestGeant4Metadata>();
266  if (fGeant4Metadata == nullptr) {
267  RESTWarning << "TRestGeant4ToDetectorHitsProcess. No TRestGeant4Metadata found in the input file"
268  << RESTendl;
269  }
270 
271  set<string> volumesToAdd;
272  TiXmlElement* volumeDefinition = GetElement("volume");
273  if (volumeDefinition == nullptr) {
274  volumeDefinition = GetElement("addVolume");
275  if (volumeDefinition != nullptr) {
276  RESTWarning << "TRestGeant4ToDetectorHitsProcess. 'addVolume' tag is deprecated. Please use "
277  "'volume' instead."
278  << RESTendl;
279  }
280  }
281  while (volumeDefinition != nullptr) {
282  const auto userVolume = GetFieldValue("name", volumeDefinition);
283  const auto typeName = GetFieldValue("type", volumeDefinition);
284  REST_HitType type = XYZ;
285  if (typeName == "veto") {
286  type = VETO;
287  }
288 
289  if (userVolume == "Not defined") {
290  RESTError << "TRestGeant4ToDetectorHitsProcess. No name defined for volume" << RESTendl;
291  }
292  if (fGeant4Metadata != nullptr) {
293  const auto& geometryInfo = fGeant4Metadata->GetGeant4GeometryInfo();
294 
295  auto physicalVolumes = geometryInfo.GetAllPhysicalVolumesMatchingExpression(userVolume);
296  if (physicalVolumes.empty()) {
297  const auto logicalVolumes = geometryInfo.GetAllLogicalVolumesMatchingExpression(userVolume);
298  for (const auto& logicalVolume : logicalVolumes) {
299  for (const auto& physicalVolume :
300  geometryInfo.GetAllPhysicalVolumesFromLogical(logicalVolume)) {
301  physicalVolumes.push_back(
302  geometryInfo.GetAlternativeNameFromGeant4PhysicalName(physicalVolume));
303  }
304  }
305  }
306  for (const auto& physicalVolume : physicalVolumes) {
307  volumesToAdd.insert(physicalVolume.Data());
308  fHitTypes[physicalVolume.Data()] = type;
309  }
310  } else {
311  volumesToAdd.insert(userVolume);
312  fHitTypes[userVolume] = type;
313  }
314 
315  volumeDefinition = GetNextElement(volumeDefinition);
316  }
317 
318  for (const auto& volume : volumesToAdd) {
319  if (find(fVolumeSelection.begin(), fVolumeSelection.end(), volume) == fVolumeSelection.end()) {
320  fVolumeSelection.emplace_back(volume);
321  }
322  }
323 }
324 
329  BeginPrintProcess();
330 
331  for (const auto& volume : fVolumeSelection) {
332  RESTMetadata << "Volume added : " << volume << RESTendl;
333  }
334 
335  EndPrintProcess();
336 }
A base class for any REST event.
Definition: TRestEvent.h:38
virtual void InitializeReferences(TRestRun *run)
Initialize dynamical references when loading the event from a root file.
Definition: TRestEvent.cxx:175
An event class to store geant4 generated event information.
A process to transform a TRestGeant4Event into a TRestDetectorHitsEvent.
void Initialize() override
Function to initialize input/output event members and define the section name.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
~TRestGeant4ToDetectorHitsProcess() override
Default destructor.
void InitProcess() override
Process initialization. This process accesses the information inside TRestGeant4Metadata to identify ...
void InitFromConfigFile() override
Function to read input parameters from the RML TRestGeant4ToDetectorHitsProcess metadata section.
void PrintMetadata() override
It prints on screen relevant data members from this class.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.