Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 26 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4GenericAnalysisManager.hh" 29 #include "G4GenericAnalysisManager.hh" >> 30 #include "G4GenericAnalysisMessenger.hh" 30 #include "G4GenericFileManager.hh" 31 #include "G4GenericFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 33 #include "G4AnalysisUtilities.hh" 33 #include "G4NtupleBookingManager.hh" 34 #include "G4NtupleBookingManager.hh" 34 #include "G4VNtupleFileManager.hh" 35 #include "G4VNtupleFileManager.hh" 35 #include "G4ThreadLocalSingleton.hh" 36 #include "G4ThreadLocalSingleton.hh" 36 #include "G4Exception.hh" 37 #include "G4Exception.hh" 37 38 38 using namespace G4Analysis; 39 using namespace G4Analysis; 39 using std::to_string; 40 using std::to_string; 40 41 41 // mutex in a file scope 42 // mutex in a file scope 42 43 43 namespace { 44 namespace { 44 45 45 //____________________________________________ 46 //_____________________________________________________________________________ 46 void WriteHnWarning(const G4String& hnType, G4 47 void WriteHnWarning(const G4String& hnType, G4int id, 47 std::string_view inClass, 48 std::string_view inClass, 48 std::string_view inFunctio 49 std::string_view inFunction) 49 { 50 { 50 Warn("Failed to get " + hnType + " id " + to 51 Warn("Failed to get " + hnType + " id " + to_string(id), inClass, inFunction); 51 } 52 } 52 53 53 } 54 } 54 55 55 //____________________________________________ 56 //_____________________________________________________________________________ 56 G4GenericAnalysisManager* G4GenericAnalysisMan 57 G4GenericAnalysisManager* G4GenericAnalysisManager::Instance() 57 { 58 { 58 static G4ThreadLocalSingleton<G4GenericAnaly 59 static G4ThreadLocalSingleton<G4GenericAnalysisManager> instance; 59 fgIsInstance = true; 60 fgIsInstance = true; 60 return instance.Instance(); 61 return instance.Instance(); 61 } 62 } 62 63 63 //____________________________________________ 64 //_____________________________________________________________________________ 64 G4bool G4GenericAnalysisManager::IsInstance() 65 G4bool G4GenericAnalysisManager::IsInstance() 65 { 66 { 66 return fgIsInstance; 67 return fgIsInstance; 67 } 68 } 68 69 69 //____________________________________________ 70 //_____________________________________________________________________________ 70 G4GenericAnalysisManager::G4GenericAnalysisMan 71 G4GenericAnalysisManager::G4GenericAnalysisManager() 71 : G4ToolsAnalysisManager("") 72 : G4ToolsAnalysisManager("") 72 { 73 { >> 74 fMessenger = std::make_unique<G4GenericAnalysisMessenger>(this); >> 75 73 if ( ! G4Threading::IsWorkerThread() ) fgMas 76 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this; 74 77 75 // File manager 78 // File manager 76 fFileManager = std::make_shared<G4GenericFil 79 fFileManager = std::make_shared<G4GenericFileManager>(fState); 77 SetFileManager(fFileManager); 80 SetFileManager(fFileManager); 78 } 81 } 79 82 80 //____________________________________________ 83 //_____________________________________________________________________________ 81 G4GenericAnalysisManager::~G4GenericAnalysisMa 84 G4GenericAnalysisManager::~G4GenericAnalysisManager() 82 { 85 { 83 if ( fState.GetIsMaster() ) fgMasterInstance 86 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; 84 fgIsInstance = false; 87 fgIsInstance = false; 85 } 88 } 86 89 87 // 90 // 88 // private methods 91 // private methods 89 // 92 // 90 93 91 //____________________________________________ 94 //_____________________________________________________________________________ 92 void G4GenericAnalysisManager::CreateNtupleFil 95 void G4GenericAnalysisManager::CreateNtupleFileManager(const G4String& fileName) 93 { 96 { 94 if ( fNtupleFileManager ) { 97 if ( fNtupleFileManager ) { 95 Warn("The ntuple file manager already exis 98 Warn("The ntuple file manager already exists.", 96 fkClass, "CreateNtupleFileManager"); 99 fkClass, "CreateNtupleFileManager"); 97 return; 100 return; 98 } 101 } 99 102 100 auto fileType = GetExtension(fileName); 103 auto fileType = GetExtension(fileName); 101 auto output = G4Analysis::GetOutput(fileType 104 auto output = G4Analysis::GetOutput(fileType); 102 if ( output == G4AnalysisOutput::kNone ) { 105 if ( output == G4AnalysisOutput::kNone ) { 103 Warn("The file type " + fileType + "is not 106 Warn("The file type " + fileType + "is not supported.", 104 fkClass, "CreateNtupleFileManager"); 107 fkClass, "CreateNtupleFileManager"); 105 return; 108 return; 106 } 109 } 107 110 108 // Set file type to booked ntuples 111 // Set file type to booked ntuples 109 fNtupleBookingManager->SetFileType(fileType) 112 fNtupleBookingManager->SetFileType(fileType); 110 113 111 Message(kVL4, "create", "ntuple file manager 114 Message(kVL4, "create", "ntuple file manager", fileType); 112 115 113 fNtupleFileManager = fFileManager->CreateNtu 116 fNtupleFileManager = fFileManager->CreateNtupleFileManager(output); 114 if (fNtupleFileManager) { 117 if (fNtupleFileManager) { 115 SetNtupleFileManager(fNtupleFileManager); 118 SetNtupleFileManager(fNtupleFileManager); 116 fNtupleFileManager->SetBookingManager(fNtu 119 fNtupleFileManager->SetBookingManager(fNtupleBookingManager); 117 120 118 if ( fNtupleFileManager->IsNtupleMergingSu 121 if ( fNtupleFileManager->IsNtupleMergingSupported() ) { 119 // set merginng 122 // set merginng 120 fNtupleFileManager->SetNtupleMerging(fMe 123 fNtupleFileManager->SetNtupleMerging(fMergeNtuples, fNofNtupleFiles); 121 fNtupleFileManager->SetNtupleRowWise(fNt 124 fNtupleFileManager->SetNtupleRowWise(fNtupleRowWise, fNtupleRowMode); 122 fNtupleFileManager->SetBasketSize(fBaske 125 fNtupleFileManager->SetBasketSize(fBasketSize); 123 fNtupleFileManager->SetBasketEntries(fBa 126 fNtupleFileManager->SetBasketEntries(fBasketEntries); 124 } 127 } 125 else if ( fIsNtupleMergingSet && fMergeNtu 128 else if ( fIsNtupleMergingSet && fMergeNtuples ) { 126 Warn("Ntuple merging is not available wi 129 Warn("Ntuple merging is not available with " + fileType + " output.\n" + 127 "Setting is ignored.", 130 "Setting is ignored.", 128 fkClass, "CreateNtupleFileManager") 131 fkClass, "CreateNtupleFileManager"); 129 } 132 } 130 } 133 } 131 134 132 Message(kVL3, "create", "ntuple file manager 135 Message(kVL3, "create", "ntuple file manager", fileType); 133 } 136 } 134 137 135 // 138 // 136 // protected methods 139 // protected methods 137 // 140 // 138 141 139 //____________________________________________ 142 //_____________________________________________________________________________ 140 G4bool G4GenericAnalysisManager::OpenFileImpl( 143 G4bool G4GenericAnalysisManager::OpenFileImpl(const G4String& fileName) 141 { 144 { 142 Message(kVL4, "open", "file", fileName); 145 Message(kVL4, "open", "file", fileName); 143 146 144 // Add file name extension, if missing 147 // Add file name extension, if missing 145 auto fullFileName = fileName; 148 auto fullFileName = fileName; 146 if (GetExtension(fileName).size() == 0u) { 149 if (GetExtension(fileName).size() == 0u) { 147 auto defaultFileType = fFileManager->GetDe 150 auto defaultFileType = fFileManager->GetDefaultFileType(); 148 // G4cout << "File type is not defined, us 151 // G4cout << "File type is not defined, using default: " << defaultFileType << G4endl; 149 if (defaultFileType.size() == 0u) { 152 if (defaultFileType.size() == 0u) { 150 G4Exception("G4GenericAnalysisManager::O 153 G4Exception("G4GenericAnalysisManager::OpenFileImpl", "Analysis_F001", 151 FatalException, 154 FatalException, 152 G4String("Cannot open file \"" + fileN 155 G4String("Cannot open file \"" + fileName + "\".\n" 153 "Please, use a file name with an ext 156 "Please, use a file name with an extension or define the default file type\n" 154 "via G4AnalysisManager::SetDefaultFi 157 "via G4AnalysisManager::SetDefaultFileType()")); 155 } 158 } 156 159 157 fullFileName = fileName + "." + fFileManag 160 fullFileName = fileName + "." + fFileManager->GetDefaultFileType(); 158 } 161 } 159 162 160 // Create ntuple file manager if there are b 163 // Create ntuple file manager if there are booked ntuples 161 if (! fNtupleFileManager) { 164 if (! fNtupleFileManager) { 162 CreateNtupleFileManager(fullFileName); 165 CreateNtupleFileManager(fullFileName); 163 } 166 } 164 167 165 auto result = true; 168 auto result = true; 166 if (fNtupleFileManager) { 169 if (fNtupleFileManager) { 167 result &= G4ToolsAnalysisManager::OpenFile 170 result &= G4ToolsAnalysisManager::OpenFileImpl(fullFileName); 168 } 171 } 169 else { 172 else { 170 // no ntuples (check if this mode is suppo 173 // no ntuples (check if this mode is supported) 171 result &= fFileManager->OpenFile(fullFileN 174 result &= fFileManager->OpenFile(fullFileName); 172 } 175 } 173 176 174 Message(kVL3, "open", "file", fileName, resu 177 Message(kVL3, "open", "file", fileName, result); 175 178 176 return result; 179 return result; 177 } 180 } 178 181 179 //____________________________________________ 182 //_____________________________________________________________________________ 180 void G4GenericAnalysisManager::SetDefaultFileT << 181 { << 182 G4VAnalysisManager::SetDefaultFileTypeImpl(v << 183 << 184 fFileManager->SetDefaultFileType(value); << 185 } << 186 << 187 //____________________________________________ << 188 G4bool G4GenericAnalysisManager::WriteH1(G4int 183 G4bool G4GenericAnalysisManager::WriteH1(G4int id, const G4String& fileName) 189 { 184 { 190 // Experimental extra write 185 // Experimental extra write 191 186 192 // Do not write histo on worker (redundant a 187 // Do not write histo on worker (redundant and fails in hdf5 ) 193 // If default file is not used, users have t 188 // If default file is not used, users have to call Merge from their code 194 if ( G4Threading::IsWorkerThread() ) return 189 if ( G4Threading::IsWorkerThread() ) return false; 195 190 196 auto h1d = GetH1(id, false); 191 auto h1d = GetH1(id, false); 197 if (h1d == nullptr) { 192 if (h1d == nullptr) { 198 WriteHnWarning("H1", id, fkClass, "WriteH1 193 WriteHnWarning("H1", id, fkClass, "WriteH1"); 199 return false; 194 return false; 200 } 195 } 201 196 202 auto h1Name = GetH1Name(id); 197 auto h1Name = GetH1Name(id); 203 return fFileManager->WriteTExtra<tools::hist 198 return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name); 204 } 199 } 205 200 206 //____________________________________________ 201 //_____________________________________________________________________________ 207 G4bool G4GenericAnalysisManager::WriteH2(G4int 202 G4bool G4GenericAnalysisManager::WriteH2(G4int id, const G4String& fileName) 208 { 203 { 209 // Experimental extra write 204 // Experimental extra write 210 205 211 // Do not write histo on worker (redundant a 206 // Do not write histo on worker (redundant and fails in hdf5 ) 212 // If default file is not used, users have t 207 // If default file is not used, users have to call Merge from their code 213 if ( G4Threading::IsWorkerThread() ) return 208 if ( G4Threading::IsWorkerThread() ) return false; 214 209 215 auto h2d = GetH2(id, false); 210 auto h2d = GetH2(id, false); 216 if (h2d == nullptr) { 211 if (h2d == nullptr) { 217 WriteHnWarning("H2", id, fkClass, "WriteH2 212 WriteHnWarning("H2", id, fkClass, "WriteH2"); 218 return false; 213 return false; 219 } 214 } 220 215 221 auto h2Name = GetH2Name(id); 216 auto h2Name = GetH2Name(id); 222 return fFileManager->WriteTExtra<tools::hist 217 return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name); 223 } 218 } 224 //____________________________________________ 219 //_____________________________________________________________________________ 225 G4bool G4GenericAnalysisManager::WriteH3(G4int 220 G4bool G4GenericAnalysisManager::WriteH3(G4int id, const G4String& fileName) 226 { 221 { 227 // Experimental extra write 222 // Experimental extra write 228 223 229 // Do not write histo on worker (redundant a 224 // Do not write histo on worker (redundant and fails in hdf5 ) 230 // If default file is not used, users have t 225 // If default file is not used, users have to call Merge from their code 231 if ( G4Threading::IsWorkerThread() ) return 226 if ( G4Threading::IsWorkerThread() ) return false; 232 227 233 auto h3d = GetH3(id, false); 228 auto h3d = GetH3(id, false); 234 if (h3d == nullptr) { 229 if (h3d == nullptr) { 235 WriteHnWarning("H3", id, fkClass, "WriteH3 230 WriteHnWarning("H3", id, fkClass, "WriteH3"); 236 return false; 231 return false; 237 } 232 } 238 233 239 auto h3Name = GetH3Name(id); 234 auto h3Name = GetH3Name(id); 240 return fFileManager->WriteTExtra<tools::hist 235 return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name); 241 } 236 } 242 237 243 //____________________________________________ 238 //_____________________________________________________________________________ 244 G4bool G4GenericAnalysisManager::WriteP1(G4int 239 G4bool G4GenericAnalysisManager::WriteP1(G4int id, const G4String& fileName) 245 { 240 { 246 // Experimental extra write 241 // Experimental extra write 247 242 248 // Do not write histo on worker (redundant a 243 // Do not write histo on worker (redundant and fails in hdf5 ) 249 // If default file is not used, users have t 244 // If default file is not used, users have to call Merge from their code 250 if ( G4Threading::IsWorkerThread() ) return 245 if ( G4Threading::IsWorkerThread() ) return false; 251 246 252 auto p1d = GetP1(id, false); 247 auto p1d = GetP1(id, false); 253 if (p1d == nullptr) { 248 if (p1d == nullptr) { 254 WriteHnWarning("P1", id, fkClass, "WriteP1 249 WriteHnWarning("P1", id, fkClass, "WriteP1"); 255 return false; 250 return false; 256 } 251 } 257 252 258 auto p1Name = GetP1Name(id); 253 auto p1Name = GetP1Name(id); 259 return fFileManager->WriteTExtra<tools::hist 254 return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name); 260 } 255 } 261 << 262 //____________________________________________ 256 //_____________________________________________________________________________ 263 G4bool G4GenericAnalysisManager::WriteP2(G4int 257 G4bool G4GenericAnalysisManager::WriteP2(G4int id, const G4String& fileName) 264 { 258 { 265 // Experimental extra write 259 // Experimental extra write 266 260 267 // Do not write histo on worker (redundant a 261 // Do not write histo on worker (redundant and fails in hdf5 ) 268 // If default file is not used, users have t 262 // If default file is not used, users have to call Merge from their code 269 if ( G4Threading::IsWorkerThread() ) return 263 if ( G4Threading::IsWorkerThread() ) return false; 270 264 271 auto p2d = GetP2(id, false); 265 auto p2d = GetP2(id, false); 272 if (p2d == nullptr) { 266 if (p2d == nullptr) { 273 WriteHnWarning("P2", id, fkClass, "WriteP2 267 WriteHnWarning("P2", id, fkClass, "WriteP2"); 274 return false; 268 return false; 275 } 269 } 276 270 277 auto p2Name = GetP2Name(id); 271 auto p2Name = GetP2Name(id); 278 return fFileManager->WriteTExtra<tools::hist 272 return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name); 279 } << 280 << 281 //____________________________________________ << 282 tools::ntuple_booking* G4GenericAnalysisManage << 283 G4bool warn, G4bool onlyIfActive) const << 284 { << 285 return fNtupleBookingManager->GetNtuple(warn << 286 } << 287 << 288 //____________________________________________ << 289 tools::ntuple_booking* G4GenericAnalysisManage << 290 G4int ntupleId, G4bool warn, G4bool onlyIfAc << 291 { << 292 return fNtupleBookingManager->GetNtuple(ntup << 293 } 273 } 294 274