REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4AnalysisProcess.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
194
195#include "TRestGeant4AnalysisProcess.h"
196
197using namespace std;
198
200
205
219 Initialize();
220
221 if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
222}
223
228
232void TRestGeant4AnalysisProcess::LoadDefaultConfig() { SetTitle("Default config"); }
233
239 fG4Metadata = nullptr;
240 SetSectionName(this->ClassName());
241 SetLibraryVersion(LIBRARY_VERSION);
242
243 fInputG4Event = nullptr;
245}
246
259void TRestGeant4AnalysisProcess::LoadConfig(const string& configFilename, const string& name) {
260 if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
261}
262
269 fG4Metadata = GetMetadata<TRestGeant4Metadata>();
270
271 std::vector<string> fObservables;
272 fObservables = TRestEventProcess::ReadObservables();
273
274 if (fPerProcessSensitiveEnergy) {
275 fObservables.emplace_back("PerProcessPhotoelectric");
276 fObservables.emplace_back("PerProcessCompton");
277 fObservables.emplace_back("PerProcessElectronicIoni");
278 fObservables.emplace_back("PerProcessAlphaIoni");
279 fObservables.emplace_back("PerProcessIonIoni");
280 fObservables.emplace_back("PerProcessHadronicIoni");
281 fObservables.emplace_back("PerProcessProtonIoni");
282 fObservables.emplace_back("PerProcessMsc");
283 fObservables.emplace_back("PerProcessHadronElastic");
284 fObservables.emplace_back("PerProcessNeutronElastic");
285 }
286 for (unsigned int i = 0; i < fObservables.size(); i++) {
287 if (fObservables[i].find("VolumeEDep") != string::npos) {
288 TString volName = fObservables[i].substr(0, fObservables[i].length() - 10).c_str();
289
290 Int_t volId = fG4Metadata->GetActiveVolumeID(volName);
291 if (volId >= 0) {
292 fEnergyInObservables.push_back(fObservables[i]);
293 fVolumeID.push_back(volId);
294 }
295
296 if (volId == -1) {
297 cout << endl;
298 cout << "??????????????????????????????????????????????????" << endl;
299 cout << "REST warning : TRestGeant4AnalysisProcess." << endl;
300 cout << "------------------------------------------" << endl;
301 cout << endl;
302 cout << " Volume " << volName << " is not an active volume" << endl;
303 cout << endl;
304 cout << "List of active volumes : " << endl;
305 cout << "------------------------ " << endl;
306
307 for (unsigned int n = 0; n < fG4Metadata->GetNumberOfActiveVolumes(); n++)
308 cout << "Volume " << n << " : " << fG4Metadata->GetActiveVolumeName(n) << endl;
309 cout << "??????????????????????????????????????????????????" << endl;
310 cout << endl;
311 }
312 }
313
314 if (fObservables[i].find("MeanPos") != string::npos) {
315 TString volName2 = fObservables[i].substr(0, fObservables[i].length() - 8).c_str();
316 std::string dirId = fObservables[i].substr(fObservables[i].length() - 1, 1);
317
318 Int_t volId2 = fG4Metadata->GetActiveVolumeID(volName2);
319 if (volId2 >= 0) {
320 fMeanPosObservables.push_back(fObservables[i]);
321 fVolumeID2.push_back(volId2);
322 fDirID.push_back(dirId);
323 }
324
325 if (volId2 == -1) {
326 cout << endl;
327 cout << "??????????????????????????????????????????????????" << endl;
328 cout << "REST warning : TRestGeant4AnalysisProcess." << endl;
329 cout << "------------------------------------------" << endl;
330 cout << endl;
331 cout << " Volume " << volName2 << " is not an active volume" << endl;
332 cout << endl;
333 cout << "List of active volumes : " << endl;
334 cout << "------------------------ " << endl;
335
336 for (unsigned int n = 0; n < fG4Metadata->GetNumberOfActiveVolumes(); n++)
337 cout << "Volume " << n << " : " << fG4Metadata->GetActiveVolumeName(n) << endl;
338 cout << "??????????????????????????????????????????????????" << endl;
339 cout << endl;
340 }
341
342 if ((dirId != "X") && (dirId != "Y") && (dirId != "Z")) {
343 cout << endl;
344 cout << "??????????????????????????????????????????????????" << endl;
345 cout << "REST warning : TRestGeant4AnalysisProcess." << endl;
346 cout << "------------------------------------------" << endl;
347 cout << endl;
348 cout << " Direction " << dirId << " is not valid" << endl;
349 cout << " Only X, Y or Z accepted" << endl;
350 cout << endl;
351 }
352 }
353 if (fObservables[i].find("Process") != string::npos) {
354 Int_t ls = 0;
355 if (fObservables[i].find("RadiactiveDecay") != string::npos) ls = 15;
356 if (fObservables[i].find("Photoelectric") != string::npos) ls = 13;
357 if (fObservables[i].find("PhotonNuclear") != string::npos) ls = 13;
358 if (fObservables[i].find("Bremstralung") != string::npos) ls = 12;
359 if (fObservables[i].find("NInelastic") != string::npos) ls = 10;
360 if (fObservables[i].find("HadElastic") != string::npos) ls = 10;
361 if (fObservables[i].find("NCapture") != string::npos) ls = 8;
362 if (fObservables[i].find("Compton") != string::npos) ls = 7;
363 if (fObservables[i].find("Neutron") != string::npos) ls = 7;
364 if (fObservables[i].find("Alpha") != string::npos) ls = 5;
365 if (fObservables[i].find("Argon") != string::npos) ls = 5;
366 if (fObservables[i].find("Xenon") != string::npos) ls = 5;
367 if (fObservables[i].find("Neon") != string::npos) ls = 4;
368
369 TString processName = fObservables[i].substr(fObservables[i].length() - (ls + 7), ls).c_str();
370 TString volName3 = fObservables[i].substr(0, fObservables[i].length() - (ls + 7)).c_str();
371 Int_t volId3 = fG4Metadata->GetActiveVolumeID(volName3);
372
373 if (volId3 >= 0) {
374 fProcessObservables.push_back(fObservables[i]);
375 fVolumeID3.push_back(volId3);
376 fProcessName.emplace_back(processName.Data());
377 }
378 }
379 if (fObservables[i].find("TracksCounter") != string::npos) {
380 TString particleName = fObservables[i].substr(0, fObservables[i].length() - 13).c_str();
381 fTrackCounterObservables.push_back(fObservables[i]);
382 fParticleTrackCounter.emplace_back(particleName.Data());
383 }
384
385 if (fObservables[i].find("TracksEDep") != string::npos) {
386 TString particleName = fObservables[i].substr(0, fObservables[i].length() - 10).c_str();
387 fTracksEDepObservables.push_back(fObservables[i]);
388 fParticleTrackEdep.emplace_back(particleName.Data());
389 }
390 }
391}
392
397 fInputG4Event = (TRestGeant4Event*)inputEvent;
398 *fOutputG4Event = *((TRestGeant4Event*)inputEvent);
399
400 Double_t energy = fOutputG4Event->GetSensitiveVolumeEnergy();
401
403 cout << "----------------------------" << endl;
404 cout << "TRestGeant4Event : " << fOutputG4Event->GetID() << endl;
405 cout << "Sensitive volume Energy : " << energy << endl;
406 cout << "Total energy : " << fOutputG4Event->GetTotalDepositedEnergy() << endl;
407 }
408
409 Double_t xOrigin = fOutputG4Event->GetPrimaryEventOrigin().X();
410 SetObservableValue("xOriginPrimary", xOrigin);
411
412 Double_t yOrigin = fOutputG4Event->GetPrimaryEventOrigin().Y();
413 SetObservableValue("yOriginPrimary", yOrigin);
414
415 Double_t zOrigin = fOutputG4Event->GetPrimaryEventOrigin().Z();
416 SetObservableValue("zOriginPrimary", zOrigin);
417
418 Double_t xDirection = fOutputG4Event->GetPrimaryEventDirection(0).X();
419 SetObservableValue("xDirectionPrimary", xDirection);
420
421 Double_t yDirection = fOutputG4Event->GetPrimaryEventDirection(0).Y();
422 SetObservableValue("yDirectionPrimary", yDirection);
423
424 Double_t zDirection = fOutputG4Event->GetPrimaryEventDirection(0).Z();
425 SetObservableValue("zDirectionPrimary", zDirection);
426
427 TVector3 v(xDirection, yDirection, zDirection);
428 SetObservableValue("thetaPrimary", v.Theta());
429 SetObservableValue("phiPrimary", v.Phi());
430
431 Double_t energyPrimary = fOutputG4Event->GetPrimaryEventEnergy(0);
432 SetObservableValue("energyPrimary", energyPrimary);
433
434 Double_t energyTotal = fOutputG4Event->GetTotalDepositedEnergy();
435 SetObservableValue("totalEdep", energyTotal);
436
437 Double_t size = fOutputG4Event->GetBoundingBoxSize();
438 SetObservableValue("boundingSize", size);
439
440 // process names as named by Geant4
441 // processes present here will be added to the list of observables which can be used to see if the event
442 // contains the process of interest.
443 vector<string> processNames = {"phot", "compt"};
444 for (auto& processName : processNames) {
445 Int_t containsProcess = 0;
446 if (fOutputG4Event->ContainsProcess(fG4Metadata->GetGeant4PhysicsInfo().GetProcessID(processName))) {
447 containsProcess = 1;
448 }
449
450 if (!processName.empty()) {
451 processName[0] = toupper(processName[0]);
452 SetObservableValue("containsProcess" + processName, containsProcess);
453 }
454 }
455
456 for (unsigned int n = 0; n < fParticleTrackCounter.size(); n++) {
457 Int_t nT = fOutputG4Event->GetNumberOfTracksForParticle(fParticleTrackCounter[n]);
458 string obsName = fTrackCounterObservables[n];
459 SetObservableValue(obsName, nT);
460 }
461
462 for (unsigned int n = 0; n < fTracksEDepObservables.size(); n++) {
463 Double_t energy = fOutputG4Event->GetEnergyDepositedByParticle(fParticleTrackEdep[n]);
464 string obsName = fTracksEDepObservables[n];
465 SetObservableValue(obsName, energy);
466 }
467
468 for (unsigned int n = 0; n < fEnergyInObservables.size(); n++) {
469 Double_t en = fOutputG4Event->GetEnergyDepositedInVolume(fVolumeID[n]);
470 string obsName = fEnergyInObservables[n];
471 SetObservableValue(obsName, en);
472 }
473
474 for (unsigned int n = 0; n < fMeanPosObservables.size(); n++) {
475 string obsName = fMeanPosObservables[n];
476
477 Double_t mpos = 0;
478 if (fDirID[n] == (TString) "X")
479 mpos = fOutputG4Event->GetMeanPositionInVolume(fVolumeID2[n]).X();
480
481 else if (fDirID[n] == (TString) "Y")
482 mpos = fOutputG4Event->GetMeanPositionInVolume(fVolumeID2[n]).Y();
483
484 else if (fDirID[n] == (TString) "Z")
485 mpos = fOutputG4Event->GetMeanPositionInVolume(fVolumeID2[n]).Z();
486
487 SetObservableValue(obsName, mpos);
488 }
489
491 cout << "G4 Tracks : " << fOutputG4Event->GetNumberOfTracks() << endl;
492 cout << "----------------------------" << endl;
493 }
494
495 return fOutputG4Event;
496}
497
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
Definition: TRestEvent.h:38
A pure analysis process to extract information from a TRestGeant4Event.
void Initialize() override
Function to initialize input/output event members and define the section name.
std::vector< std::string > fEnergyInObservables
It stores the name of observables xxxVolumeEDep related to the energy deposition in volume xxx.
std::vector< std::string > fDirID
void InitProcess() override
Process initialization. Observable names are interpreted and auxiliar observable members,...
TRestGeant4Event * fInputG4Event
A pointer to the specific TRestGeant4Event input.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
TRestGeant4Metadata * fG4Metadata
A pointer to the simulation metadata information accessible to TRestRun.
~TRestGeant4AnalysisProcess()
Default destructor.
std::vector< std::string > fTracksEDepObservables
A std::vector storing the observable name xxxTracksEDep for a given xxx particle.
std::vector< std::string > fParticleTrackCounter
A std::vector storing the xxx particle name extracted from xxxTracksCounter.
std::vector< std::string > fProcessObservables
A std::vector storing the name of observables related to processes in a particular active volume.
std::vector< Int_t > fVolumeID2
A std::vector storing the active volume ids corresponding mean position observable xxxMeanPosX,...
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
std::vector< Int_t > fVolumeID
A std::vector storing the active volume ids of observables xxxVolumeEDep.
TRestGeant4Event * fOutputG4Event
A pointer to the specific TRestGeant4Event output.
std::vector< std::string > fTrackCounterObservables
A std::vector storing the observable name xxxTracksCounter for a given xxx particle.
std::vector< Int_t > fVolumeID3
A std::vector storing the active volume ids corresponding process observable .
TRestGeant4AnalysisProcess()
Default constructor.
std::vector< std::string > fMeanPosObservables
It stores the name of observables (xxxMeanPosX,Y,Z) related to mean hits position in volume xxx.
std::vector< std::string > fProcessName
A std::vector storing the name of processes.
void EndProcess() override
Function to include required actions after all events have been processed.
std::vector< std::string > fParticleTrackEdep
A std::vector storing the xxx particle name extracted from xxxTracksEDep.
An event class to store geant4 generated event information.
Double_t GetBoundingBoxSize()
This method returns the event size as the size of the bounding box enclosing all hits.
const TRestGeant4PhysicsInfo & GetGeant4PhysicsInfo() const
Returns an immutable reference to the physics info.
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.
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
void SetSectionName(std::string sName)
set the section name, clear the section content
@ REST_Debug
+show the defined debug messages