33#include "tools/wroot/file"
34#include "tools/wroot/ntuple"
49 std::string_view className,
50 std::string_view functionName)
52 Warn(what +
" id= " + to_string(
id) +
" does not exist.",
53 className, functionName);
61 std::shared_ptr<G4NtupleBookingManager> bookingManger,
62 std::shared_ptr<G4RootMainNtupleManager> main,
65 fBookingManager(
std::move(bookingManger)),
66 fMainNtupleManager(
std::move(main)),
74 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
75 delete ntupleDescription;
85G4RootPNtupleManager::GetNtupleDescriptionInFunction(
86 G4int id, std::string_view functionName,
G4bool warn)
const
89 if ( index < 0 || index >=
G4int(fNtupleDescriptionVector.size()) ) {
91 NotExistWarning(
"ntuple description",
id, fkClass, functionName);
96 return fNtupleDescriptionVector[index];
100tools::wroot::base_pntuple*
101 G4RootPNtupleManager::GetNtupleInFunction(
102 G4int id, std::string_view functionName,
G4bool warn)
const
104 auto ntupleDescription = GetNtupleDescriptionInFunction(
id, functionName);
105 if (ntupleDescription ==
nullptr)
return nullptr;
107 if (ntupleDescription->GetBasePNtuple() ==
nullptr) {
109 NotExistWarning(
"ntuple",
id, fkClass, functionName);
113 return ntupleDescription->GetBasePNtuple();
118G4RootPNtupleManager::GetMainNtupleInFunction(
119 G4int id, std::string_view functionName,
G4bool warn)
const
121 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
124 if ( index < 0 || index >=
G4int(mainNtupleVector.size()) ) {
126 NotExistWarning(
"main ntuple",
id, fkClass, functionName);
131 return mainNtupleVector[index];
139void G4RootPNtupleManager::CreateNtupleFromMain(
141 tools::wroot::ntuple* mainNtuple)
144 if (mainNtuple ==
nullptr) {
150 Message(
kVL4,
"create from main",
"pntuple", mainNtuple->name());
152 auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->
GetDescription());
154 Warn(
"Cannot create pntuple. Main ntuple file does not exist.",
155 fkClass,
"CreateNtupleFromMain");
164 auto rfile = std::get<0>(*file);
167 auto mainBranch = mainNtuple->get_row_wise_branch();
169 =
new tools::wroot::mt_ntuple_row_wise(
170 G4cout, rfile->byte_swap(), rfile->compression(),
171 mainNtuple->dir().seek_directory(),
172 *mainBranch, mainBranch->basket_size(),
176 static_cast<tools::wroot::imt_ntuple*
>(mtNtuple));
178 static_cast<tools::wroot::base_pntuple*
>(mtNtuple));
181 std::vector<tools::uint32> basketSizes;
182 tools_vforcit(tools::wroot::branch*, ntupleDescription->
GetMainBranches(), it) {
183 basketSizes.push_back((*it)->basket_size());
185 auto basketEntries = fMainNtupleManager->GetBasketEntries();
188 new tools::wroot::mt_ntuple_column_wise(
189 G4cout, rfile->byte_swap(), rfile->compression(),
190 mainNtuple->dir().seek_directory(),
193 fRowMode, basketEntries, verbose);
196 static_cast<tools::wroot::imt_ntuple*
>(mtNtuple));
198 static_cast<tools::wroot::base_pntuple*
>(mtNtuple));
203 fNtupleVector.push_back(ntupleDescription->
GetNtuple());
205 Message(
kVL3,
"create from main",
"pntuple", mainNtuple->name());
209void G4RootPNtupleManager::CreateNtupleDescriptionsFromBooking()
215 auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
217 for (
auto g4NtupleBooking : g4NtupleBookings ) {
220 fNtupleDescriptionVector.push_back(ntupleDescription);
225void G4RootPNtupleManager::CreateNtuplesFromMain()
230 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
233 for (
auto mainNtuple : mainNtupleVector ) {
234 auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
235 CreateNtupleFromMain(ntupleDescription, mainNtuple);
240void G4RootPNtupleManager::CreateNtuplesIfNeeded()
255 if (fCreateNtuples) {
257 CreateNtupleDescriptionsFromBooking();
261 if (fMainNtupleManager->GetNewCycle()) {
262 fMainNtupleManager->CreateNtuplesFromBooking();
267 CreateNtuplesFromMain();
268 fCreateNtuples =
false;
274 if (fMainNtupleManager->GetNewCycle()) {
275 fMainNtupleManager->CreateNtuplesFromBooking();
280 CreateNtuplesFromMain();
295G4bool G4RootPNtupleManager::FillNtupleIColumn(
298 return FillNtupleTColumn<int>(ntupleId, columnId, value);
302G4bool G4RootPNtupleManager::FillNtupleFColumn(
305 return FillNtupleTColumn<float>(ntupleId, columnId, value);
309G4bool G4RootPNtupleManager::FillNtupleDColumn(
312 return FillNtupleTColumn<double>(ntupleId, columnId, value);
316G4bool G4RootPNtupleManager::FillNtupleSColumn(
319 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
323G4bool G4RootPNtupleManager::AddNtupleRow(
G4int ntupleId)
331 Message(
kVL4,
"add",
"pntuple row",
" ntupleId " + to_string(ntupleId));
336 CreateNtuplesIfNeeded();
338 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"AddNtupleRow");
339 if (ntupleDescription ==
nullptr)
return false;
345 mutex toolsLock(lock);
347 = ntupleDescription->
GetNtuple()->add_row(toolsLock, *rfile);
350 Warn(
"NtupleId " + to_string(ntupleId) +
"adding row failed.",
351 fkClass,
"AddNtupleRow");
357 Message(
kVL3,
"add",
"pntuple row",
" ntupleId " + to_string(ntupleId));
365G4bool G4RootPNtupleManager::Merge()
367 for (
auto ntupleDescription : fNtupleDescriptionVector) {
371 (ntupleDescription->
GetNtuple() ==
nullptr)) {
385 mutex toolsLock(lock);
387 = ntupleDescription->
GetNtuple()->end_fill(toolsLock, *rfile);
391 "end fill has failed.", fkClass,
"Merge");
408G4bool G4RootPNtupleManager::Reset()
414 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
415 ntupleDescription->
Reset();
418 fNtupleVector.clear();
424void G4RootPNtupleManager::Clear()
426 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
430 fNtupleDescriptionVector.clear();
431 fNtupleVector.clear();
443 auto ntupleDescription = GetNtupleDescriptionInFunction(
id,
"Delete",
true);
445 if (ntupleDescription ==
nullptr)
return false;
455 fNtupleVector[index] =
nullptr;
464void G4RootPNtupleManager::SetActivation(
467 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
474void G4RootPNtupleManager::SetActivation(
477 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"SetActivation");
478 if (ntupleDescription ==
nullptr)
return;
484G4bool G4RootPNtupleManager::GetActivation(
485 G4int ntupleId)
const
487 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"GetActivation");
488 if (ntupleDescription ==
nullptr)
return false;
494void G4RootPNtupleManager::SetNewCycle(
G4bool value)
500G4bool G4RootPNtupleManager::GetNewCycle()
const
506void G4RootPNtupleManager::SetNtupleRowWise(
G4bool rowWise,
G4bool rowMode)
#define G4MUTEX_INITIALIZER
G4GLOB_DLL std::ostream G4cout
G4bool GetIsActivation() const
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool IsVerbose(G4int verboseLevel) const
tools::wroot::imt_ntuple * GetNtuple() const
void SetNtuple(tools::wroot::imt_ntuple *intuple)
std::vector< tools::wroot::branch * > & GetMainBranches()
void SetBasePNtuple(tools::wroot::base_pntuple *basePNtuple)
RootNtupleDescription & GetDescription()
G4RootPNtupleManager()=delete
~G4RootPNtupleManager() override
void SetFile(std::shared_ptr< FT > file)
const tools::ntuple_booking & GetNtupleBooking() const
void SetIsNtupleOwner(G4bool isNtupleOwner)
G4bool GetActivation() const
void SetActivation(G4bool activation)
std::shared_ptr< FT > GetFile() const
void SetHasFill(G4bool hasFill)
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)