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
64using namespace std;
65
67
72
86 Initialize();
87
88 if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
89}
90
95
100 SetTitle("Default config");
101
102 cout << "G4 to hits metadata not found. Loading default values" << endl;
103}
104
110 SetSectionName(this->ClassName());
111 SetLibraryVersion(LIBRARY_VERSION);
112
113 fG4Event = nullptr;
115}
116
129void TRestGeant4ToDetectorHitsProcess::LoadConfig(const string& configFilename, const string& name) {
130 if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
131}
132
138 fG4Metadata = GetMetadata<TRestGeant4Metadata>();
139
140 for (unsigned int n = 0; n < fVolumeSelection.size(); n++) {
143 } else if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning)
144 cout << "TRestGeant4ToDetectorHitsProcess. volume name : " << fVolumeSelection[n]
145 << " not found and will not be added." << endl;
146 }
147
148 sort(fVolumeId.begin(), fVolumeId.end());
149 fVolumeId.erase(unique(fVolumeId.begin(), fVolumeId.end()), fVolumeId.end());
150
151 RESTDebug << "Active volumes available in TRestGeant4Metadata" << RESTendl;
152 RESTDebug << "-------------------------------------------" << RESTendl;
153 for (unsigned int n = 0; n < fG4Metadata->GetNumberOfActiveVolumes(); n++) {
154 RESTDebug << "Volume id : " << n << " name : " << fG4Metadata->GetActiveVolumeName(n) << RESTendl;
155 }
156 RESTDebug << RESTendl;
157
158 RESTDebug << "TRestGeant4HitsProcess volumes enabled in RML : ";
159 RESTDebug << "-------------------------------------------" << RESTendl;
160 if (fVolumeSelection.size() == 0)
161 RESTDebug << "all" << RESTendl;
162 else {
163 for (unsigned int n = 0; n < fVolumeSelection.size(); n++) {
164 RESTDebug << "" << RESTendl;
165 RESTDebug << " - " << fVolumeSelection[n] << RESTendl;
166 }
167 RESTDebug << " " << RESTendl;
168 }
169
170 if (fVolumeSelection.size() > 0 && fVolumeSelection.size() != fVolumeId.size())
171 RESTWarning << "TRestGeant4ToDetectorHitsProcess. Not all volumes were properly identified!"
172 << RESTendl;
173
174 if (fVolumeId.size() > 0) {
175 RESTDebug << "TRestGeant4HitsProcess volumes identified : ";
176 RESTDebug << "---------------------------------------" << RESTendl;
177 if (fVolumeSelection.size() == 0)
178 RESTDebug << "all" << RESTendl;
179 else
180 for (unsigned int n = 0; n < fVolumeSelection.size(); n++) {
181 RESTDebug << "" << RESTendl;
182 RESTDebug << " - " << fVolumeSelection[n] << RESTendl;
183 }
184 RESTDebug << " " << RESTendl;
185 }
186}
187
192 fG4Event = (TRestGeant4Event*)inputEvent;
193
195 cout << "------ TRestGeant4ToDetectorHitsProcess --- Printing Input Event --- START ----" << endl;
196 fG4Event->PrintEvent();
197 cout << "------ TRestGeant4ToDetectorHitsProcess --- Printing Input Event ---- END ----" << endl;
198 GetChar();
199 }
200
201 fHitsEvent->SetRunOrigin(fG4Event->GetRunOrigin());
202 fHitsEvent->SetSubRunOrigin(fG4Event->GetSubRunOrigin());
203 fHitsEvent->SetID(fG4Event->GetID());
204 fHitsEvent->SetSubID(fG4Event->GetSubID());
205 fHitsEvent->SetSubEventTag(fG4Event->GetSubEventTag());
206 fHitsEvent->SetTimeStamp(fG4Event->GetTimeStamp());
207 fHitsEvent->SetState(fG4Event->isOk());
208
209 for (unsigned int i = 0; i < fG4Event->GetNumberOfTracks(); i++) {
210 const auto& track = fG4Event->GetTrack(i);
211 const auto& hits = track.GetHits();
212 for (unsigned int j = 0; j < track.GetNumberOfHits(); j++) {
213 const auto energy = hits.GetEnergy(j);
214 for (const auto& volumeID : fVolumeId) {
215 if (hits.GetVolumeId(j) == volumeID && energy > 0) {
216 fHitsEvent->AddHit(hits.GetX(j), hits.GetY(j), hits.GetZ(j), energy);
217 }
218 }
219 if (fVolumeId.empty() && energy > 0) {
220 fHitsEvent->AddHit(hits.GetX(j), hits.GetY(j), hits.GetZ(j), energy);
221 }
222 }
223 }
224
226 cout << "TRestGeant4ToDetectorHitsProcess. Hits added : " << fHitsEvent->GetNumberOfHits() << endl;
227 cout << "TRestGeant4ToDetectorHitsProcess. Hits total energy : " << fHitsEvent->GetEnergy() << endl;
228 }
229
230 return fHitsEvent;
231}
232
238 size_t position = 0;
239 string addVolumeDefinition;
240
241 while ((addVolumeDefinition = GetKEYDefinition("addVolume", position)) != "")
242 fVolumeSelection.push_back(GetFieldValue("name", addVolumeDefinition));
243}
244
250
251 for (unsigned int n = 0; n < fVolumeSelection.size(); n++) {
252 RESTMetadata << "Volume added : " << fVolumeSelection[n] << RESTendl;
253 }
254
255 EndPrintProcess();
256}
void AddHit(Double_t x, Double_t y, Double_t z, Double_t en, Double_t t=0, REST_HitType type=XYZ)
Adds a new hit to this event.
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.
unsigned int GetNumberOfActiveVolumes() const
Returns the number of active volumes, or geometry volumes that have been selected for data storage.
Int_t GetActiveVolumeID(TString name)
Returns the id of an active volume giving as parameter its name.
TString GetActiveVolumeName(Int_t n) const
Returns a std::string with the name of the active volume with index n.
A process to transform a TRestGeant4Event into a TRestDetectorHitsEvent.
TRestGeant4Metadata * fG4Metadata
A pointer to the Geant4 simulation conditions stored in TRestGeant4Metadata.
void Initialize() override
Function to initialize input/output event members and define the section name.
std::vector< TString > fVolumeSelection
The geometry volume names to be transferred to TRestDetectorHitsEvent.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
TRestDetectorHitsEvent * fHitsEvent
A pointer to the output TRestDetectorHitsEvent.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
void InitProcess() override
Process initialization. This process accesses the information inside TRestGeant4Metadata to identify ...
TRestGeant4Event * fG4Event
A pointer to the input TRestGeant4Event.
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.
std::vector< Int_t > fVolumeId
The volume ids from the volumes selected for transfer to TRestDetectorHitsEvent.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
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.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
std::string GetFieldValue(std::string parName, TiXmlElement *e)
Returns the field value of an xml element which has the specified name.
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string GetKEYDefinition(std::string keyName)
Gets the first key definition for keyName found inside buffer starting at fromPosition.
@ 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.