File indexing completed on 2025-01-18 09:59:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include "G4TScoreNtupleWriterMessenger.hh"
0031 #include "G4THitsMap.hh"
0032 #include "G4Threading.hh"
0033
0034
0035
0036
0037
0038
0039
0040 template <typename T>
0041 G4TScoreNtupleWriter<T>::G4TScoreNtupleWriter()
0042 : G4VScoreNtupleWriter()
0043 , fMessenger(nullptr)
0044 , fHCIds()
0045 , fAnalysisManager(nullptr)
0046 , fDefaultFileType("root")
0047 , fFileName("scoring")
0048 , fVerboseLevel(1)
0049 , fMergeNtuples(false)
0050 , fHasAnalysisFile(false)
0051 , fIsBooked(false)
0052 , fIsInitialized(false)
0053 , fFirstNtupleId(0)
0054 {
0055 if(G4Threading::IsMasterThread())
0056 {
0057 fMessenger = new G4TScoreNtupleWriterMessenger<T>(this);
0058 }
0059 }
0060
0061
0062 template <typename T>
0063 G4TScoreNtupleWriter<T>::~G4TScoreNtupleWriter()
0064 {
0065 delete fMessenger;
0066 }
0067
0068
0069
0070
0071
0072
0073 template <typename T>
0074 void G4TScoreNtupleWriter<T>::CreateAnalysisManager()
0075 {
0076 if(fAnalysisManager) {
0077 return;
0078 }
0079
0080
0081 fAnalysisManager = T::Instance();
0082
0083 fAnalysisManager->SetDefaultFileType(fDefaultFileType);
0084 fAnalysisManager->SetVerboseLevel(fVerboseLevel);
0085 fAnalysisManager->SetNtupleMerging(fMergeNtuples);
0086 }
0087
0088
0089
0090
0091
0092
0093 template <typename T>
0094 G4VScoreNtupleWriter* G4TScoreNtupleWriter<T>::CreateInstance() const
0095 {
0096 auto instance = new G4TScoreNtupleWriter();
0097 instance->SetFileName(fFileName);
0098 instance->SetVerboseLevel(fVerboseLevel);
0099 instance->SetNtupleMerging(fMergeNtuples);
0100 instance->fHasAnalysisFile = fHasAnalysisFile;
0101
0102 return instance;
0103 }
0104
0105
0106
0107
0108
0109
0110 template <typename T>
0111 G4bool G4TScoreNtupleWriter<T>::Book(G4HCofThisEvent* hce)
0112 {
0113 #ifdef G4VERBOSE
0114 if(fVerboseLevel > 1)
0115 {
0116 G4cout << "--- G4TScoreNtupleWriter<T>::Book" << G4endl;
0117 }
0118 #endif
0119 if(!hce)
0120 return false;
0121
0122
0123 if(fHCIds.size() == 0)
0124 {
0125 #ifdef G4VERBOSE
0126 if(fVerboseLevel > 1)
0127 {
0128 G4cout << "--- going to fill fHCIds " << G4endl;
0129 }
0130 #endif
0131 for(G4int i = 0; i < hce->GetNumberOfCollections(); ++i)
0132 {
0133 auto hitsMap = dynamic_cast<G4THitsMap<G4double>*>(hce->GetHC(i));
0134
0135 if(hitsMap)
0136 {
0137 #ifdef G4VERBOSE
0138 if(fVerboseLevel > 1)
0139 {
0140 G4cout << "--- adding hcID = " << i << G4endl;
0141 }
0142 #endif
0143 fHCIds.push_back(i);
0144 }
0145 }
0146 }
0147
0148
0149 if(fHCIds.size())
0150 {
0151 CreateAnalysisManager();
0152 }
0153
0154
0155 if(!fIsInitialized)
0156 {
0157 #ifdef G4VERBOSE
0158 if(fVerboseLevel > 1)
0159 {
0160 G4cout << "-- going to create ntuples " << G4endl;
0161 }
0162 #endif
0163 auto first = true;
0164 for(auto id : fHCIds)
0165 {
0166 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
0167
0168
0169 G4String ntupleName(hitsMap->GetSDname());
0170 ntupleName.append("_");
0171 ntupleName.append(hitsMap->GetName());
0172
0173 G4int ntupleId = fAnalysisManager->CreateNtuple(ntupleName, ntupleName);
0174 if(first)
0175 {
0176 fFirstNtupleId = ntupleId;
0177 #ifdef G4VERBOSE
0178 if(fVerboseLevel > 1)
0179 {
0180 G4cout << "-- set first ntuple Id " << fFirstNtupleId << G4endl;
0181 }
0182 #endif
0183 first = false;
0184 }
0185
0186 G4String colName(ntupleName);
0187 colName.append("_eventId");
0188 fAnalysisManager->CreateNtupleIColumn(colName);
0189 colName = ntupleName;
0190 colName.append("_cell");
0191 fAnalysisManager->CreateNtupleIColumn(colName);
0192 colName = ntupleName;
0193 colName.append("_score");
0194 fAnalysisManager->CreateNtupleDColumn(colName);
0195 fAnalysisManager->FinishNtuple();
0196 fIsBooked = true;
0197 }
0198 fIsInitialized = true;
0199 }
0200
0201 return fIsBooked;
0202 }
0203
0204
0205 template <typename T>
0206 void G4TScoreNtupleWriter<T>::OpenFile()
0207 {
0208 #ifdef G4VERBOSE
0209 if(fVerboseLevel > 1)
0210 {
0211 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile" << G4endl;
0212 }
0213 #endif
0214
0215 if(fAnalysisManager->IsOpenFile())
0216 return;
0217
0218 #ifdef G4VERBOSE
0219 if(fVerboseLevel > 1)
0220 {
0221 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile executing" << G4endl;
0222 }
0223 #endif
0224
0225 if(fAnalysisManager->GetFileName() == "")
0226 {
0227 fAnalysisManager->SetFileName(fFileName);
0228 }
0229 fAnalysisManager->OpenFile();
0230
0231 #ifdef G4VERBOSE
0232 if(fVerboseLevel > 1)
0233 {
0234 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile isOpenFile: "
0235 << fAnalysisManager->IsOpenFile() << G4endl;
0236 }
0237 #endif
0238
0239 fHasAnalysisFile = fHasAnalysisFile || fAnalysisManager->IsOpenFile();
0240
0241
0242 }
0243
0244
0245 template <typename T>
0246 void G4TScoreNtupleWriter<T>::Fill(G4HCofThisEvent* hce, G4int eventNumber)
0247 {
0248 #ifdef G4VERBOSE
0249 if(fVerboseLevel > 1)
0250 {
0251 G4cout << "--- start G4TScoreNtupleWriter<T>::Fill" << G4endl;
0252 }
0253 #endif
0254
0255 auto counter = 0;
0256 for(auto id : fHCIds)
0257 {
0258 #ifdef G4VERBOSE
0259 if(fVerboseLevel > 1)
0260 {
0261 G4cout << "in loop over fHCIds, counter " << counter << G4endl;
0262 }
0263 #endif
0264 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
0265
0266
0267
0268
0269
0270 std::map<G4int, G4double*>::iterator it;
0271 for(it = hitsMap->GetMap()->begin(); it != hitsMap->GetMap()->end(); it++)
0272 {
0273 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 0,
0274 eventNumber);
0275 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 1,
0276 it->first);
0277 fAnalysisManager->FillNtupleDColumn(counter + fFirstNtupleId, 2,
0278 *(it->second));
0279 fAnalysisManager->AddNtupleRow(counter + fFirstNtupleId);
0280 }
0281 counter++;
0282 }
0283
0284 #ifdef G4VERBOSE
0285 if(fVerboseLevel > 1)
0286 {
0287 G4cout << "--- done G4TScoreNtupleWriter<T>::Fill" << G4endl;
0288 }
0289 #endif
0290 }
0291
0292
0293 template <typename T>
0294 void G4TScoreNtupleWriter<T>::Write()
0295 {
0296 #ifdef G4VERBOSE
0297 if(fVerboseLevel > 1)
0298 {
0299 G4cout << "--- start G4TScoreNtupleWriter<T>::Write" << G4endl;
0300 }
0301 #endif
0302
0303 if(fHasAnalysisFile)
0304 {
0305 #ifdef G4VERBOSE
0306 if(fVerboseLevel > 1)
0307 {
0308 G4cout << "--- G4TScoreNtupleWriter<T>::Write - has file" << G4endl;
0309 }
0310 #endif
0311 fAnalysisManager->Write();
0312 fAnalysisManager->CloseFile();
0313 fAnalysisManager->SetFileName("");
0314 }
0315
0316 #ifdef G4VERBOSE
0317 if(fVerboseLevel > 1)
0318 {
0319 G4cout << "--- done G4TScoreNtupleWriter<T>::Write" << G4endl;
0320 }
0321 #endif
0322 }
0323
0324
0325 template <typename T>
0326 void G4TScoreNtupleWriter<T>::SetDefaultFileType(const G4String& value)
0327 {
0328 fDefaultFileType = value;
0329 if(fAnalysisManager)
0330 {
0331 fAnalysisManager->SetDefaultFileType(value);
0332 }
0333 }
0334
0335
0336 template <typename T>
0337 void G4TScoreNtupleWriter<T>::SetFileName(const G4String& fileName)
0338 {
0339 fFileName = fileName;
0340 }
0341
0342
0343 template <typename T>
0344 void G4TScoreNtupleWriter<T>::SetVerboseLevel(G4int value)
0345 {
0346 fVerboseLevel = value;
0347 if(fAnalysisManager)
0348 {
0349 fAnalysisManager->SetVerboseLevel(value);
0350 }
0351 }
0352
0353
0354 template <typename T>
0355 void G4TScoreNtupleWriter<T>::SetNtupleMerging(G4bool value)
0356 {
0357 fMergeNtuples = value;
0358 if(fAnalysisManager)
0359 {
0360 fAnalysisManager->SetNtupleMerging(value);
0361 }
0362 }