Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 // ******************************************* 27 // 28 // CaTS (Calorimetry and Tracking Simulation) 29 // 30 // Authors : Hans Wenzel 31 // Soon Yung Jun 32 // (Fermi National Accelerator Labo 33 // 34 // History 35 // October 18th, 2021 : first implementatio 36 // November 10th, 2021 : implement writing o 37 // per worker thread w 38 // 39 // ******************************************* 40 // 41 /// \file RootIO.cc 42 /// \brief Implementation of the CaTS::RootIO 43 44 #ifdef WITH_ROOT 45 // Project headers 46 #include "RootIO.hh" 47 #include "ConfigurationManager.hh" 48 #include "Event.hh" 49 // Geant4 headers 50 #include <G4String.hh> 51 #include <G4ios.hh> 52 #include <G4Threading.hh> 53 #include <G4RunManager.hh> 54 // Root headers 55 #include "TBranch.h" 56 #include "TFile.h" 57 #include "TObject.h" 58 #include "TSystem.h" 59 #include "TTree.h" 60 #include "TROOT.h" 61 // c++ headers 62 #include <string> 63 #include <stdio.h> 64 65 G4ThreadLocal RootIO* RootIO::fgInstance = nul 66 // mutex in a file scope 67 namespace { 68 // Mutex to lock RootIO constructor 69 G4Mutex RootIOMutex = G4MUTEX_INITIALIZER; 70 } // namespace 71 72 RootIO::RootIO() { 73 G4AutoLock lock(&RootIOMutex); 74 TSystem ts; 75 gSystem->Load("libCaTSClassesDict"); 76 if (G4Threading::IsMultithreadedApplicatio 77 if (G4Threading::IsWorkerThread()) { 78 G4String fFilename = Configuration 79 "_Thread_" + 80 std::to_string(G4Threading 81 G4cout << "RootIO:: Opening File: 82 fFile = new TFile(fFilename.c_str( 83 TTree::SetMaxTreeSize(1000 * Long6 84 // Create a ROOT Tree and one supe 85 ftree = new TTree("Events", "ROOT 86 ftree->SetAutoSave(1000000000); // 87 Int_t branchStyle = 1; 88 TTree::SetBranchStyle(branchStyle) 89 } 90 } else { 91 G4String fFilename = ConfigurationMana 92 G4cout << "RootIO:: Opening File: " << 93 fFile = new TFile(fFilename.c_str(), " 94 TTree::SetMaxTreeSize(1000 * Long64_t( 95 // Create a ROOT Tree and one superbra 96 ftree = new TTree("Events", "ROOT tree 97 ftree->SetAutoSave(1000000000); // aut 98 Int_t branchStyle = 1; 99 TTree::SetBranchStyle(branchStyle); 100 } 101 } 102 103 RootIO* RootIO::GetInstance() { 104 if (fgInstance == nullptr) { 105 static G4ThreadLocalSingleton<RootIO> 106 fgInstance = inst.Instance(); 107 } 108 return fgInstance; 109 } 110 111 void RootIO::Write(Event* fevent) { 112 if (ConfigurationManager::getInstance()->i 113 G4cout << "writing Event: " << fevent- 114 if ((fevent->GetEventNumber()) % 1000 == 0 115 G4cout << "writing Event: " << fevent- 116 if (!fevtinitialized) { 117 Int_t bufsize = 64000; 118 fevtbranch = ftree->Branch("event.", & 119 fevtbranch->SetAutoDelete(kFALSE); 120 fevtinitialized = true; 121 } 122 fFile = ftree->GetCurrentFile(); // just i 123 fnb += ftree->Fill(); 124 fFile->Write("", TObject::kOverwrite); 125 } 126 127 void RootIO::Close() { 128 G4cout << " Closing File: " << G4endl; 129 fFile = ftree->GetCurrentFile(); 130 fFile->Close(); 131 } 132 133 void RootIO::Merge() { 134 if (G4Threading::IsMasterThread()) { 135 unsigned int nthreads = G4RunManager:: 136 G4cout << "RootIO::Merging: " << nthre 137 G4String filename = ConfigurationManag 138 G4String tmpfn; 139 std::vector<TFile*> filevec; 140 std::vector<Event*> evtvec; 141 std::vector<TTree*> treevec; 142 std::vector<TBranch*> branchvec; 143 TList* list = new TList; 144 TTree* newtree; 145 for (unsigned int i = 0; i < nthreads; 146 tmpfn = filename + "_Thread_" + st 147 filevec.push_back(new TFile(tmpfn. 148 treevec.push_back((TTree*) (fileve 149 list->Add(treevec[i]); 150 if (i == nthreads - 1) { 151 G4String tmpfn2 = filename + " 152 TFile* fm = new TFile(tmpfn2.c 153 newtree = TTree::MergeTrees(li 154 newtree->SetName("Events"); 155 Event* eventm = new Event(); 156 newtree->SetBranchAddress("eve 157 TBranch* fevtbranchm = newtree 158 int neventm = fevtbranchm->Get 159 G4cout << "Nr. of Events: " < 160 newtree->Write(); 161 fm->Close(); 162 } 163 } 164 for (unsigned int i = 0; i < nthreads; 165 tmpfn = filename + "_Thread_" + st 166 if (remove(tmpfn.c_str()) != 0) { 167 G4cout << "Error deleting file 168 } else { 169 G4cout << "File: " << tmpfn << 170 } 171 } 172 } 173 } 174 #endif 175