34#include "tools/wroot/file"
35#include "tools/wroot/ntuple"
46 G4String inFunction =
"G4RootPNtupleManager::";
47 inFunction += functionName;
49 description << what <<
" id= " <<
id <<
" does not exist.";
57 std::shared_ptr<G4NtupleBookingManager> bookingManger,
58 std::shared_ptr<G4RootMainNtupleManager> main,
61 fBookingManager(bookingManger),
62 fMainNtupleManager(main),
63 fNtupleDescriptionVector(),
72 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
73 delete ntupleDescription;
83G4RootPNtupleManager::GetNtupleDescriptionInFunction(
87 if ( index < 0 || index >=
G4int(fNtupleDescriptionVector.size()) ) {
89 NotExistException(
"ntuple description",
id, functionName);
94 return fNtupleDescriptionVector[index];
98tools::wroot::base_pntuple*
99 G4RootPNtupleManager::GetNtupleInFunction(
102 auto ntupleDescription = GetNtupleDescriptionInFunction(
id, functionName);
103 if ( ! ntupleDescription )
return nullptr;
105 if ( ! ntupleDescription->fBasePNtuple ) {
107 NotExistException(
"ntuple",
id, functionName);
111 return ntupleDescription->fBasePNtuple;
116G4RootPNtupleManager::GetMainNtupleInFunction(
119 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
122 if ( index < 0 || index >=
G4int(mainNtupleVector.size()) ) {
124 NotExistException(
"main ntuple",
id, functionName);
129 return mainNtupleVector[index];
137void G4RootPNtupleManager::CreateNtupleFromMain(
139 tools::wroot::ntuple* mainNtuple)
144 ->
Message(
"create from main",
"pntuple", mainNtuple->name());
148 auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->
fDescription);
150 G4String inFunction =
"G4RootPNtupleManager::::CreateNtupleFromMain";
153 <<
"Cannot create pntuple. Main ntuple file does not exist." <<
G4endl;
163 auto rfile = std::get<0>(*file);
166 auto mainBranch = mainNtuple->get_row_wise_branch();
167 tools::wroot::mt_ntuple_row_wise* mtNtuple
168 =
new tools::wroot::mt_ntuple_row_wise(
169 G4cout, rfile->byte_swap(), rfile->compression(),
170 mainNtuple->dir().seek_directory(),
171 *mainBranch, mainBranch->basket_size(),
175 =
static_cast<tools::wroot::imt_ntuple*
>(mtNtuple);
177 =
static_cast<tools::wroot::base_pntuple*
>(mtNtuple);
180 std::vector<tools::uint32> basketSizes;
181 tools_vforcit(tools::wroot::branch*, ntupleDescription->
fMainBranches, it) {
182 basketSizes.push_back((*it)->basket_size());
184 auto basketEntries = fMainNtupleManager->GetBasketEntries();
186 tools::wroot::mt_ntuple_column_wise* mtNtuple =
187 new tools::wroot::mt_ntuple_column_wise(
188 G4cout, rfile->byte_swap(), rfile->compression(),
189 mainNtuple->dir().seek_directory(),
192 fRowMode, basketEntries, verbose);
195 =
static_cast<tools::wroot::imt_ntuple*
>(mtNtuple);
197 =
static_cast<tools::wroot::base_pntuple*
>(mtNtuple);
202 fNtupleVector.push_back(ntupleDescription->
fNtuple);
207 ->
Message(
"create from main",
"pntuple", mainNtuple->name());
213void G4RootPNtupleManager::CreateNtuplesFromMain()
219 auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
220 for (
auto g4NtupleBooking : g4NtupleBookings ) {
223 fNtupleDescriptionVector.push_back(ntupleDescription);
226 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
229 for (
auto mainNtuple : mainNtupleVector ) {
230 auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
231 CreateNtupleFromMain(ntupleDescription, mainNtuple);
245G4bool G4RootPNtupleManager::FillNtupleIColumn(
248 return FillNtupleTColumn<int>(ntupleId, columnId, value);
252G4bool G4RootPNtupleManager::FillNtupleFColumn(
255 return FillNtupleTColumn<float>(ntupleId, columnId, value);
259G4bool G4RootPNtupleManager::FillNtupleDColumn(
262 return FillNtupleTColumn<double>(ntupleId, columnId, value);
266G4bool G4RootPNtupleManager::FillNtupleSColumn(
269 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
273G4bool G4RootPNtupleManager::AddNtupleRow(
G4int ntupleId)
283 description <<
" ntupleId " << ntupleId;
288 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"AddNtupleRow");
289 if ( ! ntupleDescription )
return false;
295 mutex toolsLock(lock);
297 = ntupleDescription->
fNtuple->add_row(toolsLock, *rfile);
301 description <<
" " <<
" ntupleId " << ntupleId
302 <<
"adding row has failed.";
303 G4Exception(
"G4RootPNtupleManager::AddNtupleRow()",
312 description <<
" ntupleId " << ntupleId;
321G4bool G4RootPNtupleManager::Merge()
323 for (
auto ntupleDescription : fNtupleDescriptionVector) {
342 mutex toolsLock(lock);
344 = ntupleDescription->
fNtuple->end_fill(toolsLock, *rfile);
349 <<
"end fill has failed.";
354 delete ntupleDescription->
fNtuple;
355 ntupleDescription->
fNtuple =
nullptr;
371 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
372 if ( deleteNtuple ) {
373 delete ntupleDescription->
fNtuple;
375 ntupleDescription->
fNtuple =
nullptr;
378 fNtupleVector.clear();
385void G4RootPNtupleManager::SetActivation(
388 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
395void G4RootPNtupleManager::SetActivation(
398 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"SetActivation");
399 if ( ! ntupleDescription )
return;
405G4bool G4RootPNtupleManager::GetActivation(
406 G4int ntupleId)
const
408 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"GetActivation");
409 if ( ! ntupleDescription )
return false;
415G4int G4RootPNtupleManager::GetNofNtuples()
const
417 return fNtupleVector.size();
421void G4RootPNtupleManager::SetNtupleRowWise(
G4bool rowWise,
G4bool rowMode)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
#define G4MUTEX_INITIALIZER
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL3() const
G4bool GetIsActivation() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
G4RootPNtupleManager(const G4AnalysisManagerState &state, std::shared_ptr< G4NtupleBookingManager > bookingManger, std::shared_ptr< G4RootMainNtupleManager > main, G4bool rowWise, G4bool rowMode)
tools::wroot::base_pntuple * fBasePNtuple
tools::wroot::imt_ntuple * fNtuple
std::vector< tools::wroot::branch * > fMainBranches
RootNtupleDescription fDescription
std::shared_ptr< TF > fFile
tools::ntuple_booking fNtupleBooking