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 "G4ToolsAnalysisManager.hh" 29 #include "G4ToolsAnalysisManager.hh" 30 #include "G4VNtupleFileManager.hh" 30 #include "G4VNtupleFileManager.hh" 31 #include "G4PlotManager.hh" 31 #include "G4PlotManager.hh" 32 #include "G4MPIToolsManager.hh" 32 #include "G4MPIToolsManager.hh" 33 #include "G4AnalysisUtilities.hh" 33 #include "G4AnalysisUtilities.hh" 34 #include "G4AutoLock.hh" 34 #include "G4AutoLock.hh" 35 #include "G4Threading.hh" 35 #include "G4Threading.hh" 36 36 37 using namespace G4Analysis; 37 using namespace G4Analysis; 38 38 39 namespace { 39 namespace { 40 //Mutex to lock master manager when merging 40 //Mutex to lock master manager when merging histograms 41 G4Mutex mergeHnMutex = G4MUTEX_INITIALIZER; 41 G4Mutex mergeHnMutex = G4MUTEX_INITIALIZER; 42 } 42 } 43 43 44 //____________________________________________ 44 //_____________________________________________________________________________ 45 G4ToolsAnalysisManager* G4ToolsAnalysisManager 45 G4ToolsAnalysisManager* G4ToolsAnalysisManager::Instance() 46 { 46 { 47 return fgToolsInstance; 47 return fgToolsInstance; 48 } 48 } 49 49 50 //____________________________________________ 50 //_____________________________________________________________________________ 51 G4bool G4ToolsAnalysisManager::IsInstance() 51 G4bool G4ToolsAnalysisManager::IsInstance() 52 { 52 { 53 return ( fgToolsInstance != nullptr ); 53 return ( fgToolsInstance != nullptr ); 54 } 54 } 55 55 56 //____________________________________________ 56 //_____________________________________________________________________________ 57 G4ToolsAnalysisManager::G4ToolsAnalysisManager 57 G4ToolsAnalysisManager::G4ToolsAnalysisManager(const G4String& type) 58 : G4VAnalysisManager(type) 58 : G4VAnalysisManager(type) 59 { 59 { 60 // Set instance pointer 60 // Set instance pointer 61 if ( ! G4Threading::IsWorkerThread() ) fgMas 61 if ( ! G4Threading::IsWorkerThread() ) fgMasterToolsInstance = this; 62 fgToolsInstance = this; 62 fgToolsInstance = this; 63 63 64 // Create managers 64 // Create managers 65 fH1Manager = new G4THnToolsManager<kDim1, to 65 fH1Manager = new G4THnToolsManager<kDim1, tools::histo::h1d>(fState); 66 fH2Manager = new G4THnToolsManager<kDim2, to 66 fH2Manager = new G4THnToolsManager<kDim2, tools::histo::h2d>(fState); 67 fH3Manager = new G4THnToolsManager<kDim3, to 67 fH3Manager = new G4THnToolsManager<kDim3, tools::histo::h3d>(fState); 68 fP1Manager = new G4THnToolsManager<kDim2, to 68 fP1Manager = new G4THnToolsManager<kDim2, tools::histo::p1d>(fState); 69 fP2Manager = new G4THnToolsManager<kDim3, to 69 fP2Manager = new G4THnToolsManager<kDim3, tools::histo::p2d>(fState); 70 // The managers will be deleted by the b 70 // The managers will be deleted by the base class 71 71 72 // Set managers to base class which takes th 72 // Set managers to base class which takes then their ownership 73 SetH1Manager(fH1Manager); 73 SetH1Manager(fH1Manager); 74 SetH2Manager(fH2Manager); 74 SetH2Manager(fH2Manager); 75 SetH3Manager(fH3Manager); 75 SetH3Manager(fH3Manager); 76 SetP1Manager(fP1Manager); 76 SetP1Manager(fP1Manager); 77 SetP2Manager(fP2Manager); 77 SetP2Manager(fP2Manager); 78 78 79 // Plot manager 79 // Plot manager 80 fPlotManager = std::make_unique<G4PlotManage 80 fPlotManager = std::make_unique<G4PlotManager>(fState); 81 } 81 } 82 82 83 //____________________________________________ 83 //_____________________________________________________________________________ 84 G4ToolsAnalysisManager::~G4ToolsAnalysisManage 84 G4ToolsAnalysisManager::~G4ToolsAnalysisManager() 85 { 85 { 86 if ( fState.GetIsMaster() ) fgMasterToolsIns 86 if ( fState.GetIsMaster() ) fgMasterToolsInstance = nullptr; 87 fgToolsInstance = nullptr; 87 fgToolsInstance = nullptr; 88 } 88 } 89 89 90 // 90 // 91 // private methods 91 // private methods 92 // 92 // 93 93 94 //____________________________________________ 94 //_____________________________________________________________________________ 95 G4bool G4ToolsAnalysisManager::WriteHns() 95 G4bool G4ToolsAnalysisManager::WriteHns() 96 { 96 { 97 // Nothing to be done on worker 97 // Nothing to be done on worker 98 if ( G4Threading::IsWorkerThread() ) return 98 if ( G4Threading::IsWorkerThread() ) return false; 99 99 100 auto result = true; 100 auto result = true; 101 101 102 // Write all histograms/profile on master 102 // Write all histograms/profile on master 103 result &= WriteT(fH1Manager->GetTHnVectorRef 103 result &= WriteT(fH1Manager->GetTHnVectorRef()); 104 result &= WriteT(fH2Manager->GetTHnVectorRef 104 result &= WriteT(fH2Manager->GetTHnVectorRef()); 105 result &= WriteT(fH3Manager->GetTHnVectorRef 105 result &= WriteT(fH3Manager->GetTHnVectorRef()); 106 result &= WriteT(fP1Manager->GetTHnVectorRef 106 result &= WriteT(fP1Manager->GetTHnVectorRef()); 107 result &= WriteT(fP2Manager->GetTHnVectorRef 107 result &= WriteT(fP2Manager->GetTHnVectorRef()); 108 108 109 return result; 109 return result; 110 } 110 } 111 111 112 //____________________________________________ 112 //_____________________________________________________________________________ 113 G4bool G4ToolsAnalysisManager::ResetHns() 113 G4bool G4ToolsAnalysisManager::ResetHns() 114 { 114 { 115 // Reset histograms and profiles 115 // Reset histograms and profiles 116 116 117 auto result = true; 117 auto result = true; 118 118 119 result &= fH1Manager->Reset(); 119 result &= fH1Manager->Reset(); 120 result &= fH2Manager->Reset(); 120 result &= fH2Manager->Reset(); 121 result &= fH3Manager->Reset(); 121 result &= fH3Manager->Reset(); 122 result &= fP1Manager->Reset(); 122 result &= fP1Manager->Reset(); 123 result &= fP2Manager->Reset(); 123 result &= fP2Manager->Reset(); 124 124 125 return result; 125 return result; 126 } 126 } 127 127 128 //____________________________________________ 128 //_____________________________________________________________________________ 129 G4bool G4ToolsAnalysisManager::MergeHns() 129 G4bool G4ToolsAnalysisManager::MergeHns() 130 { 130 { 131 // Nothing to be done on master 131 // Nothing to be done on master 132 if ( ! G4Threading::IsWorkerThread() ) retur 132 if ( ! G4Threading::IsWorkerThread() ) return false; 133 133 134 if (fgMasterToolsInstance == nullptr) { 134 if (fgMasterToolsInstance == nullptr) { 135 if (! IsEmpty() ) { 135 if (! IsEmpty() ) { 136 Warn("No master G4AnalysisManager instan 136 Warn("No master G4AnalysisManager instance exists.\n" 137 "Histogram/profile data will not be 137 "Histogram/profile data will not be merged.", 138 fkClass, "Merge"); 138 fkClass, "Merge"); 139 return false; 139 return false; 140 } 140 } 141 return true; 141 return true; 142 } 142 } 143 143 144 Message(kVL4, "merge on worker", "histograms 144 Message(kVL4, "merge on worker", "histograms"); 145 145 146 // The worker manager just adds its histogra 146 // The worker manager just adds its histograms to the master 147 fH1Manager->Merge(mergeHnMutex, fgMasterTool 147 fH1Manager->Merge(mergeHnMutex, fgMasterToolsInstance->fH1Manager); 148 fH2Manager->Merge(mergeHnMutex, fgMasterTool 148 fH2Manager->Merge(mergeHnMutex, fgMasterToolsInstance->fH2Manager); 149 fH3Manager->Merge(mergeHnMutex, fgMasterTool 149 fH3Manager->Merge(mergeHnMutex, fgMasterToolsInstance->fH3Manager); 150 fP1Manager->Merge(mergeHnMutex, fgMasterTool 150 fP1Manager->Merge(mergeHnMutex, fgMasterToolsInstance->fP1Manager); 151 fP2Manager->Merge(mergeHnMutex, fgMasterTool 151 fP2Manager->Merge(mergeHnMutex, fgMasterToolsInstance->fP2Manager); 152 152 153 Message(kVL3, "merge on worker", "histograms 153 Message(kVL3, "merge on worker", "histograms"); 154 154 155 return true; 155 return true; 156 } 156 } 157 157 158 // 158 // 159 // protected methods 159 // protected methods 160 // 160 // 161 161 162 //____________________________________________ 162 //_____________________________________________________________________________ 163 G4bool G4ToolsAnalysisManager::OpenFileImpl(co 163 G4bool G4ToolsAnalysisManager::OpenFileImpl(const G4String& fileName) 164 { 164 { 165 // Create ntuple manager(s) 165 // Create ntuple manager(s) 166 // and set it to base class which takes then 166 // and set it to base class which takes then their ownership 167 SetNtupleManager(fVNtupleFileManager->Create 167 SetNtupleManager(fVNtupleFileManager->CreateNtupleManager()); 168 168 169 auto result = true; 169 auto result = true; 170 170 171 // Open file 171 // Open file 172 if ( fVNtupleFileManager->GetMergeMode() != 172 if ( fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) { 173 result &= fVFileManager->OpenFile(fileName 173 result &= fVFileManager->OpenFile(fileName); 174 } 174 } 175 175 176 // Open ntuple files and create ntuples from 176 // Open ntuple files and create ntuples from bookings 177 result &= fVNtupleFileManager->ActionAtOpenF 177 result &= fVNtupleFileManager->ActionAtOpenFile(fVFileManager->GetFullFileName()); 178 178 179 return result; 179 return result; 180 } 180 } 181 181 182 //____________________________________________ 182 //_____________________________________________________________________________ 183 G4bool G4ToolsAnalysisManager::WriteImpl() 183 G4bool G4ToolsAnalysisManager::WriteImpl() 184 { 184 { 185 Message(kVL4, "write", "files"); 185 Message(kVL4, "write", "files"); 186 186 187 auto result = true; 187 auto result = true; 188 if ( G4Threading::IsWorkerThread() ) { 188 if ( G4Threading::IsWorkerThread() ) { 189 result &= MergeHns(); 189 result &= MergeHns(); 190 } 190 } 191 else { 191 else { 192 // Open all files registered with objects 192 // Open all files registered with objects 193 fVFileManager->OpenFiles(); 193 fVFileManager->OpenFiles(); 194 194 195 // Write all histograms/profile on master 195 // Write all histograms/profile on master 196 result &= WriteHns(); 196 result &= WriteHns(); 197 } 197 } 198 198 199 // Ntuples 199 // Ntuples 200 if (fVNtupleFileManager) { 200 if (fVNtupleFileManager) { 201 result &= fVNtupleFileManager->ActionAtWri 201 result &= fVNtupleFileManager->ActionAtWrite(); 202 } 202 } 203 203 204 // Files 204 // Files 205 if ( (fVNtupleFileManager == nullptr) || 205 if ( (fVNtupleFileManager == nullptr) || 206 (fVNtupleFileManager->GetMergeMode() != 206 (fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave) ) { 207 result &= fVFileManager->WriteFiles(); 207 result &= fVFileManager->WriteFiles(); 208 } 208 } 209 209 210 // Write ASCII if activated 210 // Write ASCII if activated 211 if ( IsAscii() ) { 211 if ( IsAscii() ) { 212 result &= WriteAscii(fVFileManager->GetFil 212 result &= WriteAscii(fVFileManager->GetFileName()); 213 } 213 } 214 214 215 Message(kVL3, "write", "files", "", result); 215 Message(kVL3, "write", "files", "", result); 216 216 217 return result; 217 return result; 218 } 218 } 219 219 220 //____________________________________________ 220 //_____________________________________________________________________________ 221 G4bool G4ToolsAnalysisManager::CloseFileImpl(G 221 G4bool G4ToolsAnalysisManager::CloseFileImpl(G4bool reset) 222 { 222 { 223 Message(kVL4, "close", "files"); 223 Message(kVL4, "close", "files"); 224 224 225 auto result = true; 225 auto result = true; 226 if (fVNtupleFileManager) { 226 if (fVNtupleFileManager) { 227 result &= fVNtupleFileManager->ActionAtClo 227 result &= fVNtupleFileManager->ActionAtCloseFile(); 228 } 228 } 229 229 230 // close file 230 // close file 231 if ( (fVNtupleFileManager == nullptr) || 231 if ( (fVNtupleFileManager == nullptr) || 232 (fVNtupleFileManager->GetMergeMode() != 232 (fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave) ) { 233 if ( ! fVFileManager->CloseFiles() ) { 233 if ( ! fVFileManager->CloseFiles() ) { 234 Warn("Closing files failed", fkClass, "C 234 Warn("Closing files failed", fkClass, "CloseFileImpl"); 235 result = false; 235 result = false; 236 } 236 } 237 } 237 } 238 238 239 // delete empty files 239 // delete empty files 240 if ( ! fVFileManager->DeleteEmptyFiles() ) { 240 if ( ! fVFileManager->DeleteEmptyFiles() ) { 241 Warn("Deleting empty files failed", fkClas 241 Warn("Deleting empty files failed", fkClass, "CloseFileImpl"); 242 result = false; 242 result = false; 243 } 243 } 244 244 245 // reset histograms 245 // reset histograms 246 if ( reset ) { 246 if ( reset ) { 247 if ( ! Reset() ) { 247 if ( ! Reset() ) { 248 Warn("Resetting data failed", fkClass, " 248 Warn("Resetting data failed", fkClass, "CloseFileImpl"); 249 result = false; 249 result = false; 250 } 250 } 251 } 251 } 252 252 253 Message(kVL3, "close", "files", "", result); 253 Message(kVL3, "close", "files", "", result); 254 254 255 return result; 255 return result; 256 } 256 } 257 257 258 //____________________________________________ 258 //_____________________________________________________________________________ 259 G4bool G4ToolsAnalysisManager::ResetImpl() 259 G4bool G4ToolsAnalysisManager::ResetImpl() 260 { 260 { 261 // Reset histograms and ntuple 261 // Reset histograms and ntuple 262 262 263 Message(kVL4, "reset", ""); 263 Message(kVL4, "reset", ""); 264 264 265 auto result = true; 265 auto result = true; 266 result &= ResetHns(); 266 result &= ResetHns(); 267 if ( fVNtupleFileManager != nullptr ) { 267 if ( fVNtupleFileManager != nullptr ) { 268 result &= fVNtupleFileManager->Reset(); 268 result &= fVNtupleFileManager->Reset(); 269 } 269 } 270 270 271 Message(kVL3, "reset", "", "", result); 271 Message(kVL3, "reset", "", "", result); 272 272 273 return result; 273 return result; 274 } 274 } 275 275 276 //____________________________________________ 276 //_____________________________________________________________________________ 277 void G4ToolsAnalysisManager::ClearImpl() 277 void G4ToolsAnalysisManager::ClearImpl() 278 { 278 { 279 // Reset histograms and profiles 279 // Reset histograms and profiles 280 280 281 fH1Manager->ClearData(); 281 fH1Manager->ClearData(); 282 fH2Manager->ClearData(); 282 fH2Manager->ClearData(); 283 fH3Manager->ClearData(); 283 fH3Manager->ClearData(); 284 fP1Manager->ClearData(); 284 fP1Manager->ClearData(); 285 fP2Manager->ClearData(); 285 fP2Manager->ClearData(); 286 } 286 } 287 287 288 //____________________________________________ 288 //_____________________________________________________________________________ 289 G4bool G4ToolsAnalysisManager::PlotImpl() 289 G4bool G4ToolsAnalysisManager::PlotImpl() 290 { 290 { 291 291 292 // Only master thread performs plotting 292 // Only master thread performs plotting 293 if ( G4Threading::IsWorkerThread() ) return 293 if ( G4Threading::IsWorkerThread() ) return true; 294 294 295 auto result = true; 295 auto result = true; 296 296 297 // Open output file 297 // Open output file 298 fPlotManager->OpenFile(fVFileManager->GetPlo 298 fPlotManager->OpenFile(fVFileManager->GetPlotFileName()); 299 299 300 // H1 300 // H1 301 result 301 result 302 &= fPlotManager->PlotAndWrite<tools::histo 302 &= fPlotManager->PlotAndWrite<tools::histo::h1d>(fH1Manager->GetTHnVectorRef()); 303 303 304 // H2 304 // H2 305 result 305 result 306 &= fPlotManager->PlotAndWrite<tools::histo 306 &= fPlotManager->PlotAndWrite<tools::histo::h2d>(fH2Manager->GetTHnVectorRef()); 307 307 308 // H3 308 // H3 309 // not yet available in tools 309 // not yet available in tools 310 310 311 // P1 311 // P1 312 result 312 result 313 &= fPlotManager->PlotAndWrite<tools::histo 313 &= fPlotManager->PlotAndWrite<tools::histo::p1d>(fP1Manager->GetTHnVectorRef()); 314 314 315 // P2 315 // P2 316 // not yet available in tools 316 // not yet available in tools 317 317 318 // Close output file 318 // Close output file 319 result &= fPlotManager->CloseFile(); 319 result &= fPlotManager->CloseFile(); 320 320 321 return result; 321 return result; 322 } 322 } 323 323 324 //____________________________________________ 324 //_____________________________________________________________________________ 325 G4bool G4ToolsAnalysisManager::MergeImpl(tools 325 G4bool G4ToolsAnalysisManager::MergeImpl(tools::histo::hmpi* hmpi) 326 { 326 { 327 327 328 // if ( G4Threading::IsWorkerThread() ) ret 328 // if ( G4Threading::IsWorkerThread() ) return true; 329 329 330 if (hmpi == nullptr) return false; 330 if (hmpi == nullptr) return false; 331 331 332 auto result = true; 332 auto result = true; 333 333 334 // Create MPI manager 334 // Create MPI manager 335 G4MPIToolsManager mpiToolsManager(fState, hm 335 G4MPIToolsManager mpiToolsManager(fState, hmpi); 336 336 337 // H1 337 // H1 338 result &= mpiToolsManager.Merge<tools::histo 338 result &= mpiToolsManager.Merge<tools::histo::h1d>(fH1Manager->GetTHnVectorRef()); 339 339 340 // H2 340 // H2 341 result &= mpiToolsManager.Merge<tools::histo 341 result &= mpiToolsManager.Merge<tools::histo::h2d>(fH2Manager->GetTHnVectorRef()); 342 342 343 // H3 343 // H3 344 result &= mpiToolsManager.Merge<tools::histo 344 result &= mpiToolsManager.Merge<tools::histo::h3d>(fH3Manager->GetTHnVectorRef()); 345 345 346 // P1 346 // P1 347 result &= mpiToolsManager.Merge<tools::histo 347 result &= mpiToolsManager.Merge<tools::histo::p1d>(fP1Manager->GetTHnVectorRef()); 348 348 349 // P2 349 // P2 350 result &= mpiToolsManager.Merge<tools::histo 350 result &= mpiToolsManager.Merge<tools::histo::p2d>(fP2Manager->GetTHnVectorRef()); 351 351 352 return result; 352 return result; 353 } 353 } 354 354 355 355 356 //____________________________________________ 356 //_____________________________________________________________________________ 357 G4bool G4ToolsAnalysisManager::IsEmpty() 357 G4bool G4ToolsAnalysisManager::IsEmpty() 358 { 358 { 359 return fH1Manager->IsEmpty() && fH2Manager-> 359 return fH1Manager->IsEmpty() && fH2Manager->IsEmpty() && fH3Manager->IsEmpty() && 360 fP1Manager->IsEmpty() && fP2Manager-> 360 fP1Manager->IsEmpty() && fP2Manager->IsEmpty(); 361 } 361 } 362 362