GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSScoringManager.cpp
Go to the documentation of this file.
1 /*
2  * GGSScoringManager.cpp
3  *
4  * Created on: 02 Aug 2013
5  * Author: Nicola Mori
6  */
7 
13 #include "utils/GGSSmartLog.h"
14 
15 #include "G4LogicalVolumeStore.hh"
16 #include "G4SDManager.hh"
17 
18 #include <iostream>
19 
20 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
21 
23  static GGSScoringManager instance;
24  return instance;
25 }
26 
27 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
28 
29 GGSScoringManager::GGSScoringManager() : _messenger(new GGSScoringManagerMessenger) {}
30 
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 
33 GGSScoringManager::~GGSScoringManager() { delete _messenger; }
34 
35 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
36 
37 void GGSScoringManager::RegisterSDClass(const G4String &className, G4VSensitiveDetector *(*sdBuilder)(G4String)) {
38  static const std::string routineName("GGSScoringManager::RegisterSDClass");
39  std::pair<SDBuildersMap::iterator, bool> insertResult =
40  _buildersMap.insert(std::pair<G4String, G4VSensitiveDetector *(*)(G4String)>(className, sdBuilder));
41  if (!insertResult.second) {
42  COUT(WARNING) << "Sensitive detector class " << className << " is already present." << ENDL;
43  }
44 }
45 
46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
47 
48 void GGSScoringManager::AddSDToLogVol(const G4String &sdClassName, const G4String &logVolNameAndParams) {
49  static const std::string routineName("GGSScoringManager::AddSDToLogVol");
50 
51  G4VSensitiveDetector *sd = nullptr;
52 
53  // Attach the sensitive detector to all the logical volumes
54  G4String logVolName = logVolNameAndParams.substr(0, logVolNameAndParams.find_first_of(' '));
55  auto separatorPos = logVolNameAndParams.find_first_of(' ');
56  G4String params;
57  if (separatorPos != std::string::npos) {
58  params = logVolNameAndParams.substr(logVolNameAndParams.find_first_of(' ') + 1);
59  }
60  for (G4LogicalVolumeStore::const_iterator logVol = G4LogicalVolumeStore::GetInstance()->begin();
61  logVol != G4LogicalVolumeStore::GetInstance()->end(); logVol++) {
62  if ((*logVol)->GetName() == logVolName) {
63  // Retrieve the sd builder
64  if (!sd) {
65  SDBuildersMap::iterator builder = _buildersMap.find(sdClassName);
66  if (builder == _buildersMap.end()) {
67  throw std::runtime_error(routineName + ": builder for sensitive detector class " + sdClassName +
68  " is not present.");
69  }
70  // Build the sd
71  G4String sdName = sdClassName;
72  if (params != "") {
73  sdName.append('.').append(params);
74  }
75  sd = builder->second(logVolName + "." + sdName);
76  }
77  // Add sd to logvol
78  AddSDToLogVol(sd, *logVol);
79  }
80  }
81 
82  if (!sd) {
83  throw std::runtime_error(routineName + ": logical volume " + logVolName + " not present. Sensitive detector " +
84  sdClassName + " not added.");
85  }
86 }
87 
88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
89 
90 void GGSScoringManager::AddSDToLogVol(G4VSensitiveDetector *sd, G4LogicalVolume *logVol) {
91 
92  // Add sd to logical volume
93  if (logVol->GetSensitiveDetector() == NULL) {
94  // Case 1: logical has not a sd. Add the newly built sd to it
95  logVol->SetSensitiveDetector(sd);
96  } else {
97  GGSMultiSensitiveDetector *multiSD = dynamic_cast<GGSMultiSensitiveDetector *>(logVol->GetSensitiveDetector());
98  if (multiSD) {
99  // Case 2: the current sd is a multisd. Add the newly built sd to it
100  multiSD->AddSensitiveDetector(sd);
101  } else {
102  // Case 3: the current sd is a standard sd. Replace it with a multisd and add it and the newly built sd to the
103  // multisd
104  multiSD = new GGSMultiSensitiveDetector(G4String(logVol->GetName()).append(".GGSMultiSD"));
105  multiSD->AddSensitiveDetector(logVol->GetSensitiveDetector());
106  multiSD->AddSensitiveDetector(sd);
107  G4SDManager::GetSDMpointer()->AddNewDetector(multiSD);
108  logVol->SetSensitiveDetector(multiSD);
109  }
110  }
111 
112  // Add the sd to the sdmanager if it has not already been added
113  GGSSmartLog::MuteOutput();
114  if (G4SDManager::GetSDMpointer()->GetCollectionID(sd->GetName()) == -1) {
115  G4SDManager::GetSDMpointer()->AddNewDetector(sd);
116  }
117  GGSSmartLog::UnmuteOutput();
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
~GGSScoringManager()
Destructor.
static GGSScoringManager & GetInstance()
Singleton getter.
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
void RegisterSDClass(const G4String &className, G4VSensitiveDetector *(*sdBuilder)(G4String))
Class registration method (class factory approach).
A multiple sensitive detector.
Singleton for handling plugin scoring classes.
Messenger for the GGSScoringManager singleton.
void AddSensitiveDetector(G4VSensitiveDetector *sd)
Adds a new sensitive detector to the multidetector.
void AddSDToLogVol(const G4String &sdClassName, const G4String &logVolNameAndParams)
Adds a sensitive detector to a logical volume (class factory approach).