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)
143 Message(
kVL4,
"create from main",
"pntuple", mainNtuple->name());
145 auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->
GetDescription());
147 Warn(
"Cannot create pntuple. Main ntuple file does not exist.",
148 fkClass,
"CreateNtupleFromMain");
157 auto rfile = std::get<0>(*file);
160 auto mainBranch = mainNtuple->get_row_wise_branch();
162 =
new tools::wroot::mt_ntuple_row_wise(
163 G4cout, rfile->byte_swap(), rfile->compression(),
164 mainNtuple->dir().seek_directory(),
165 *mainBranch, mainBranch->basket_size(),
169 static_cast<tools::wroot::imt_ntuple*
>(mtNtuple));
171 static_cast<tools::wroot::base_pntuple*
>(mtNtuple));
174 std::vector<tools::uint32> basketSizes;
175 tools_vforcit(tools::wroot::branch*, ntupleDescription->
GetMainBranches(), it) {
176 basketSizes.push_back((*it)->basket_size());
178 auto basketEntries = fMainNtupleManager->GetBasketEntries();
181 new tools::wroot::mt_ntuple_column_wise(
182 G4cout, rfile->byte_swap(), rfile->compression(),
183 mainNtuple->dir().seek_directory(),
186 fRowMode, basketEntries, verbose);
189 static_cast<tools::wroot::imt_ntuple*
>(mtNtuple));
191 static_cast<tools::wroot::base_pntuple*
>(mtNtuple));
196 fNtupleVector.push_back(ntupleDescription->
GetNtuple());
198 Message(
kVL3,
"create from main",
"pntuple", mainNtuple->name());
202void G4RootPNtupleManager::CreateNtupleDescriptionsFromBooking()
208 auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
210 for (
auto g4NtupleBooking : g4NtupleBookings ) {
213 fNtupleDescriptionVector.push_back(ntupleDescription);
218void G4RootPNtupleManager::CreateNtuplesFromMain()
223 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
226 for (
auto mainNtuple : mainNtupleVector ) {
227 auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
228 CreateNtupleFromMain(ntupleDescription, mainNtuple);
233void G4RootPNtupleManager::CreateNtuplesIfNeeded()
248 if (fCreateNtuples) {
250 CreateNtupleDescriptionsFromBooking();
254 if (fMainNtupleManager->GetNewCycle()) {
255 fMainNtupleManager->CreateNtuplesFromBooking();
260 CreateNtuplesFromMain();
261 fCreateNtuples =
false;
267 if (fMainNtupleManager->GetNewCycle()) {
268 fMainNtupleManager->CreateNtuplesFromBooking();
273 CreateNtuplesFromMain();
288G4bool G4RootPNtupleManager::FillNtupleIColumn(
291 return FillNtupleTColumn<int>(ntupleId, columnId, value);
295G4bool G4RootPNtupleManager::FillNtupleFColumn(
298 return FillNtupleTColumn<float>(ntupleId, columnId, value);
302G4bool G4RootPNtupleManager::FillNtupleDColumn(
305 return FillNtupleTColumn<double>(ntupleId, columnId, value);
309G4bool G4RootPNtupleManager::FillNtupleSColumn(
312 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
316G4bool G4RootPNtupleManager::AddNtupleRow(
G4int ntupleId)
324 Message(
kVL4,
"add",
"pntuple row",
" ntupleId " + to_string(ntupleId));
329 CreateNtuplesIfNeeded();
331 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"AddNtupleRow");
332 if (ntupleDescription ==
nullptr)
return false;
338 mutex toolsLock(lock);
340 = ntupleDescription->
GetNtuple()->add_row(toolsLock, *rfile);
343 Warn(
"NtupleId " + to_string(ntupleId) +
"adding row failed.",
344 fkClass,
"AddNtupleRow");
350 Message(
kVL3,
"add",
"pntuple row",
" ntupleId " + to_string(ntupleId));
358G4bool G4RootPNtupleManager::Merge()
360 for (
auto ntupleDescription : fNtupleDescriptionVector) {
364 (ntupleDescription->
GetNtuple() ==
nullptr)) {
378 mutex toolsLock(lock);
380 = ntupleDescription->
GetNtuple()->end_fill(toolsLock, *rfile);
384 "end fill has failed.", fkClass,
"Merge");
401G4bool G4RootPNtupleManager::Reset()
407 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
408 ntupleDescription->
Reset();
411 fNtupleVector.clear();
417void G4RootPNtupleManager::Clear()
419 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
423 fNtupleDescriptionVector.clear();
424 fNtupleVector.clear();
431void G4RootPNtupleManager::SetActivation(
434 for (
auto ntupleDescription : fNtupleDescriptionVector ) {
441void G4RootPNtupleManager::SetActivation(
444 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"SetActivation");
445 if (ntupleDescription ==
nullptr)
return;
451G4bool G4RootPNtupleManager::GetActivation(
452 G4int ntupleId)
const
454 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId,
"GetActivation");
455 if (ntupleDescription ==
nullptr)
return false;
461void G4RootPNtupleManager::SetNewCycle(
G4bool value)
467G4bool G4RootPNtupleManager::GetNewCycle()
const
473G4int G4RootPNtupleManager::GetNofNtuples()
const
475 return (
G4int)fNtupleVector.size();
479void G4RootPNtupleManager::SetNtupleRowWise(
G4bool rowWise,
G4bool rowMode)
486G4bool G4RootPNtupleManager::List(std::ostream& ,
G4bool )
488 Warn(
"Not implemented.", fkClass,
"List");
#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)