ndmspc  v1.2.0-0.1.rc3
NGnThreadData.cxx
1 #include <NGnTree.h>
2 #include <NStorageTree.h>
3 #include <TROOT.h>
4 #include <TCanvas.h>
5 #include <mutex>
6 #include "THnSparse.h"
7 #include "NBinningPoint.h"
8 #include "NLogger.h"
9 #include "NUtils.h"
10 #include "NGnThreadData.h"
11 
13 ClassImp(Ndmspc::NGnThreadData);
15 
16 namespace Ndmspc {
19 bool NGnThreadData::Init(size_t id, NGnProcessFuncPtr func, NGnBeginFuncPtr funcBegin, NGnEndFuncPtr endFunc,
20  NGnTree * ngnt, NBinning * binningIn, NGnTree * input, const std::string & filename,
21  const std::string & treename)
22 {
26  SetAssignedIndex(id);
27  SetThreadId(std::this_thread::get_id());
28 
29  TH1::AddDirectory(kFALSE); // Disable ROOT auto directory management
30 
31  // if (!func) {
32  // NLogError("NGnThreadData::Init: Process function is not set !!!");
33  // return false;
34  // }
35  fBeginFunc = funcBegin;
36  fProcessFunc = func;
37  fEndFunc = endFunc;
38 
39  if (ngnt == nullptr) {
40  NLogError("NGnThreadData::Init: NGnTree is nullptr !!!");
41  return false;
42  }
43 
44  fIsPureCopy = ngnt->IsPureCopy();
45 
46  fBiningSource = binningIn;
47 
48  if (fBiningSource == nullptr) {
49  NLogError("NGnThreadData::Init: Binning Source is nullptr !!!");
50  return false;
51  }
52 
53  fHnSparseBase = (NGnTree *)ngnt->Clone();
54  // fHnSparseBase = new NGnTree(hnsb->GetBinning(), (NStorageTree *)hnsb->GetStorageTree()->Clone());
55  // fHnSparseBase = new NGnTree(hnsb->GetBinning(), new NStorageTree(hnsb->GetBinning()));
56  // fHnSparseBase = new NGnTree(hnsb->GetBinning(), nullptr);
57 
58  if (fHnSparseBase->GetBinning() == nullptr) {
59  NLogError("NGnThreadData::InitStorage: Binning is not set !!!");
60  return false;
61  }
62 
63  if (fHnSparseBase->GetStorageTree() == nullptr) {
64  NLogError("NGnThreadData::InitStorage: Storage tree is not set !!!");
65  return false;
66  }
67 
68  // NLogDebug("NGnThreadData::Init: Initializing storage for thread %zu", id);
69  // NGnTree * ngntIn = new NGnTree(ngnt->GetInput(), "");
70  //
71  // fHnSparseBase->SetInput(ngntIn); // Clear input for the thread local NGnTree
72 
74  std::string fn = ts->GetFileName();
75  ts->Clear("F");
76  ts->InitTree(filename.empty() ? fn : filename, treename);
77 
78  NTreeBranch * b = nullptr;
79  // loop over all branches and add them to the new storage tree
80  for (auto & kv : ngnt->GetStorageTree()->GetBranchesMap()) {
81  NLogTrace("NGnThreadData::Init: Adding branch '%s' to thread %zu", kv.first.c_str(), id);
82  b = ts->GetBranch(kv.first);
83  if (b) continue;
84 
85  b = ts->GetBranch(kv.first);
86  if (b) continue;
87 
88  ts->AddBranch(kv.first, nullptr, kv.second.GetObjectClassName());
89  }
90  b = ts->GetBranch("_outputPoint");
91  if (!b) ts->AddBranch("_outputPoint", nullptr, "TList");
92 
93  if (ngnt->GetParameters()) {
94  NLogTrace("NGnThreadData::Init: Setting parameters branch for thread %zu", id);
95  b = ts->GetBranch("_params");
96  if (!b) ts->AddBranch("_params", nullptr, "Ndmspc::NParameters");
97 
98  NParameters * params = (NParameters *)ngnt->GetParameters()->Clone();
99  ts->GetBranch("_params")->SetAddress(params);
101  }
102 
103  // Recreate the point and set the storage tree
105 
106  // for (auto & kv : ts->GetBranchesMap()) {
107  // NLogTrace("NGnThreadData::Init: Adding branch '%s' to thread %zu", kv.first.c_str(), id);
108  // fHnSparseBase->GetStorageTree()->AddBranch(kv.first, nullptr, kv.second.GetObjectClassName());
109  // }
110 
111  // TODO: check if needed or move it somewhere else like Reset();
112  //
113  // Loop over aoll definitions and reset their content and ids
114  for (const auto & name : fHnSparseBase->GetBinning()->GetDefinitionNames()) {
116  if (def) {
117  def->GetContent()->Reset();
118  def->GetIds().clear();
119  }
120  }
121 
122  if (input) {
123  NLogTrace("NGnThreadData::Init: Setting input NGnTree for thread %zu '%s'", id,
124  input->GetStorageTree()->GetFileName().c_str());
125  std::string branches = NUtils::Join(input->GetStorageTree()->GetBrancheNames(true), ',');
126  fHnSparseBase->SetInput(NGnTree::Open(input->GetStorageTree()->GetFileName(), branches)); // Set the input NGnTree
127  }
128 
129  // fHnSparseBase->GetBinning()->GetDefinition()->GetContent()->Reset();
130  // fHnSparseBase->GetBinning()->GetDefinition()->GetIds().clear();
131 
132  ExecuteBeginFunction();
133 
134  return true;
135 }
136 
137 void NGnThreadData::Process(const std::vector<int> & coords)
138 {
142  TH1::AddDirectory(kFALSE); // Disable ROOT auto directory management
143 
144  // Ensure this thread has a current pad so that user code calling h->Fit()
145  // does not trigger the non-thread-safe TCanvas::MakeDefCanvas().
146  // gPad is thread_local in ROOT 6: each worker thread starts with nullptr.
147  // We create a minimal batch canvas (batch mode is already set by
148  // NGnTree::Process before ExecuteParallel) and serialise the one-time
149  // TCanvas::Constructor call with a static mutex. After this block gPad is
150  // set for this thread and subsequent Fit() calls find it non-null.
151  if (!gPad) {
152  static std::mutex sPadMutex;
153  std::lock_guard<std::mutex> lk(sPadMutex);
154  // gPad is still nullptr for THIS thread even inside the lock (thread-local);
155  // the mutex only serialises concurrent TCanvas::Constructor calls.
156  TString cname = TString::Format("_ndmspc_wk%zu", GetAssignedIndex());
157  auto * c = new TCanvas(cname, cname, 1, 1);
158  fDeferredDeletes.push_back(c); // cleaned up by FlushDeferredDeletes
159  }
160 
161  fNProcessed++;
162  // NThreadData::Process(coords);
163 
164  // NLogDebug("NGnThreadData::Process: Thread %d processing coordinates %s", GetAssignedIndex(),
165  // NUtils::GetCoordsString(coords).c_str());
166 
167  if (!fHnSparseBase) {
168  NLogError("NGnThreadData::Process: NGnTree is not set in NGnThreadData !!!");
169  return;
170  }
171 
172  if (!fProcessFunc) {
173  NLogError("NGnThreadData::Process: Process function is not set in NGnThreadData !!!");
174  return;
175  }
176 
177  // NBinning * binning = fBiningSource;
178  NBinningDef * binningDef = fBiningSource->GetDefinition();
179  if (binningDef == nullptr) {
180  NLogError("NGnThreadData::Process: Binning definition is not set in NGnThreadData !!!");
181  return;
182  }
183 
185  NGnTree * in = fHnSparseBase->GetInput();
186 
188 
189  Long64_t entry = fBiningSource->GetDefinition()->GetId(coords[0]);
190 
191  if (entry < fHnSparseBase->GetBinning()->GetContent()->GetNbins()) {
192  fHnSparseBase->GetBinning()->GetDefinition()->GetIds().push_back(entry);
193  // entry = binningDef->GetContent()->GetBinContent(entry);
194  // point->SetEntryNumber(entry);
195  NLogDebug("NGnThreadData::Process: [%zu] Skipping entry=%lld, because it was already process !!!",
196  GetAssignedIndex(), entry);
197  return;
198  }
199 
200  if (fResourceMonitor == nullptr) {
202  fResourceMonitor->Initialize(binningDef->GetContent());
204  }
205 
206  // Long64_t entry = binningDef->GetId(coords[0]);
207  // NLogDebug("NGnThreadData::Process: [%zu] Entry in global content mapping: %lld",
208  // GetAssignedIndex(), entry);
209  fBiningSource->GetContent()->GetBinContent(entry, point->GetCoords());
210  point->RecalculateStorageCoords(entry, false);
211  point->SetCfg(fCfg); // Set configuration to the point
212  // point->Print("C");
213 
214  // TODO: check if entry was already processed
215  // So we dont execute the function again
216 
217  // NLogDebug(
218  // "AAA NGnThreadData::Process: Thread %zu processing entry %lld for coordinates %s", GetAssignedIndex(),
219  // entry,
220  // NUtils::GetCoordsString(NUtils::ArrayToVector(point->GetCoords(), point->GetNDimensionsContent())).c_str());
221  point->SetTreeStorage(ts); // Set the storage tree to the binning point
222  point->SetInput(in); // Set the input NGnTree to the binning point
223  TList * outputPoint = new TList();
224 
226 
227  fProcessFunc(point, fHnSparseBase->GetOutput(), outputPoint, GetAssignedIndex());
228 
231 
232  if (!point->GetCfg()["_ndmspc"].is_null()) {
233  fCfg["_ndmspc"] = point->GetCfg()["_ndmspc"]; // Get configuration from the point
234  }
235 
236  // NLogTrace(
237  // "NGnThreadData::Process: [%zu] entry=%lld coords=%s outputPoint=%d", GetAssignedIndex(), entry,
238  // NUtils::GetCoordsString(NUtils::ArrayToVector(point->GetCoords(), point->GetNDimensionsContent())).c_str(),
239  // outputPoint->GetEntries());
240  if (outputPoint->GetEntries() > 0) {
241  NLogTrace(
242  "NGnThreadData::Process: [%zu] Entry '%lld' was accepted. %s", GetAssignedIndex(), entry,
244 
245  if (!fIsPureCopy) {
246  ts->GetBranch("_outputPoint")->SetAddress(outputPoint); // Set the output list as branch address
247  }
248  //
249  // ts->Fill(point, nullptr, false, {}, false);
250  Int_t bytes = ts->Fill(point, nullptr, false, {}, false);
251  if (bytes > 0) {
252  // Long64_t entryInBinDef = binningDefgcc->GetId(coords[0]);
253  // NLogDebug("NGnThreadData::Process: Thread %zu: Filled %d bytes for coordinates %s entry=%lld",
254  // GetAssignedIndex(), bytes, NUtils::GetCoordsString(coords).c_str(), entry);
255 
256  fHnSparseBase->GetBinning()->GetDefinition()->GetIds().push_back(entry);
257  // NLogInfo("Entry number in storage tree: %lld", point->GetEntryNumber());
258  // fHnSparseBase->GetBinning()->GetDefinition()->GetIds().push_back(point->GetEntryNumber());
259  }
260  else {
261  NLogTrace("NGnThreadData::Process: [%zu] Entry '%lld' Fill was done with 0 bytes. Skipping ...",
262  GetAssignedIndex(), entry);
263  // NLogError("NGnThreadData::Process: Thread %zu: zero bytes were writtent for coordinates %s
264  // entry=%lld",
265  // GetAssignedIndex(), NUtils::GetCoordsString(coords).c_str(), entry);
266  }
267  // outputPoint->Print();
268  // outputPoint->Clear(); // Clear the list to avoid memory leaks
269  }
270  else {
271  NLogTrace(
272  "NGnThreadData::Process: [%zu] Entry '%lld' No output %s. Skipping ...", GetAssignedIndex(), entry,
274  // NLogTrace(
275  // "No output for coordinates %s",
276  // NUtils::GetCoordsString(NUtils::ArrayToVector(point->GetCoords(),
277  // point->GetNDimensionsContent())).c_str());
278  }
279 
280  // Defer deletion to FlushDeferredDeletes() on the main thread.
281  // ROOT's cleanup machinery (GarbageCollect, RecursiveRemove) is not thread-safe.
282  {
283  NLogTrace("NGnThreadData::Process: [%zu] Cleaning output list with %d entries for entry '%lld' ...",
284  GetAssignedIndex(), outputPoint->GetEntries(), entry);
285 
286  TObject * obj = nullptr;
287  while ((obj = outputPoint->First())) {
288  outputPoint->Remove(obj);
289  fDeferredDeletes.push_back(obj);
290  }
291  delete outputPoint;
292  }
293 }
294 
295 void NGnThreadData::SetCurrentDefinitionName(const std::string & name)
296 {
299  }
300  if (fBiningSource) {
302  }
303 }
304 
305 void NGnThreadData::SyncCurrentDefinitionIds(const std::vector<Long64_t> & ids)
306 {
307  // fHnSparseBase tracks only the entries actually written by this worker.
308  // Clear it so Process() builds the list from scratch (same as thread mode via Init).
309  // Do NOT assign the full 'ids' list here — that would include unprocessed entries
310  // (e.g., even-indexed entries when onlyOddPoints=true) and corrupt the merge step.
312  if (auto * def = fHnSparseBase->GetBinning()->GetDefinition()) {
313  def->GetIds().clear();
314  }
315  }
316 
317  // fBiningSource is used for GetId() coordinate lookups — it needs the full list.
318  if (fBiningSource) {
319  if (auto * def = fBiningSource->GetDefinition()) {
320  def->GetIds() = ids;
321  }
322  }
323 }
324 
325 Long64_t NGnThreadData::Merge(TCollection * list)
326 {
330  Long64_t nmerged = 0;
331 
332  NLogTrace("NGnThreadData::Merge: BEGIN ------------------------------------------------");
333  NLogTrace("NGnThreadData::Merge: Merging thread data from %zu threads ...", list->GetEntries());
334 
335  NStorageTree * ts = nullptr;
336  std::map<std::string, TList *> listOutputs;
337 
338  // TList * listOut = new TList();
339  TList * listTreeStorage = new TList();
340 
341  for (auto obj : *list) {
342  if (obj->IsA() == NGnThreadData::Class()) {
343  NGnThreadData * hnsttd = (NGnThreadData *)obj;
344  NLogDebug("NGnThreadData::Merge: Merging thread %zu processed %lld ...", hnsttd->GetAssignedIndex(),
345  hnsttd->GetNProcessed());
346  ts = hnsttd->GetHnSparseBase()->GetStorageTree();
347  if (!ts) {
348  NLogError("NGnThreadData::Merge: Storage tree is not set in NGnTree !!!");
349  continue;
350  }
351  // hnsttd->Print();
352 
353  for (auto & kv : hnsttd->GetHnSparseBase()->GetOutputs()) {
354  NLogTrace("NGnThreadData::Merge: Found output list '%s' with %d objects", kv.first.c_str(),
355  kv.second ? kv.second->GetEntries() : 0);
356  if (kv.second && !kv.second->IsEmpty()) {
357  NLogTrace("NGnThreadData::Merge: Merging output list '%s' with %d objects", kv.first.c_str(),
358  kv.second->GetEntries());
359  if (listOutputs.find(kv.first) == listOutputs.end()) {
360  if (fHnSparseBase->GetOutput(kv.first)->IsEmpty()) {
361  listOutputs[kv.first] = new TList();
362  fHnSparseBase->GetOutput(kv.first)->AddAll(kv.second);
363  }
364  }
365  else {
366  listOutputs[kv.first]->Add(kv.second);
367  }
368  }
369  }
370 
371  // if (fOutput == nullptr) {
372  // fOutput = hnsttd->GetOutput();
373  // }
374  // else {
375  // listOut->Add(hnsttd->GetOutput());
376  // }
377 
378  const std::string mergeFilename =
379  hnsttd->GetResultsFilename().empty() ? ts->GetFileName() : hnsttd->GetResultsFilename();
380  NGnTree * hnsb = NGnTree::Open(mergeFilename);
381  if (!hnsb) {
382  NLogError("NGnThreadData::Merge: Failed to open NGnTree from file '%s' !!!", mergeFilename.c_str());
383  continue;
384  }
385 
386  // hnsb->Print();
387  listTreeStorage->Add(hnsb->GetStorageTree());
388 
389  // TODO: check if needed
390  // fHnSparseBase->GetBinning()->GetDefinition()->GetContent()->Add(
391  // hnsb->GetBinning()->GetDefinition()->GetContent());
392  nmerged++;
393  }
394  }
395 
396 
397  // for (const auto & name : fBiningSource->GetDefinitionNames()) {
398  // auto binningDef = fBiningSource->GetDefinition(name);
399  // if (!binningDef) {
400  // NLogError("NGnThreadData::Merge: Binning definition '%s' not found in NGnTree !!!", name.c_str());
401  // continue;
402  // }
403  // // add ids from fBiningSource to binningDef
404  // // binningDef->GetIds().insert(binningDef->GetIds().end(), fBiningSource->GetDefinition(name)->GetIds().begin(),
405  // // fBiningSource->GetDefinition(name)->GetIds().end());
406  // NLogDebug("NGnThreadData::Merge: BEFORE Final IDs in definition '%s': %s", name.c_str(),
407  // NUtils::GetCoordsString(binningDef->GetIds(), -1).c_str());
408  // }
409 
410 
411  // FIXME: Fix this properly [it should be ok now]
412  fHnSparseBase->GetBinning()->GetContent()->Reset();
413  // Print hnsb binning definition ids
414  for (const auto & name : fBiningSource->GetDefinitionNames()) {
415  NBinningDef * targetBinningDef = fBiningSource->GetDefinition(name);
416  if (auto * mergedDef = fHnSparseBase->GetBinning()->GetDefinition(name)) {
417  mergedDef->GetIds().clear();
418  }
419  for (auto id : targetBinningDef->GetIds()) {
421  fBiningSource->GetContent()->GetBinContent(id, point.GetCoords());
422  Long64_t bin = fHnSparseBase->GetBinning()->GetContent()->GetBin(point.GetCoords());
423  NLogTrace("NGnThreadData::Merge: [%s] Adding def_id=%lld to content_bin=%lld",name.c_str(), id, bin);
424  fHnSparseBase->GetBinning()->GetContent()->SetBinContent(bin, id);
425  }
426  // fHnSparseBase->GetBinning()->GetDefinition(name)->Print();
427  }
428  // FIXME: End
429  NLogDebug("NGnThreadData::Merge: Total entries to merge: %lld", nmerged);
430 
431  for (const auto & name : fHnSparseBase->GetBinning()->GetDefinitionNames()) {
432  auto binningDef = fHnSparseBase->GetBinning()->GetDefinition(name);
433  if (!binningDef) {
434  NLogError("NGnThreadData::Merge: Binning definition '%s' not found in NGnTree !!!", name.c_str());
435  continue;
436  }
437  // add ids from fBiningSource to binningDef
438  // binningDef->GetIds().insert(binningDef->GetIds().end(), fBiningSource->GetDefinition(name)->GetIds().begin(),
439  // fBiningSource->GetDefinition(name)->GetIds().end());
440  NLogTrace("NGnThreadData::Merge: Final IDs in definition '%s': %s", name.c_str(),
441  NUtils::GetCoordsString(binningDef->GetIds(), -1).c_str());
442  }
443 
444  // fHnSparseBase->GetBinning()->GetContent()->Projection(5)->Print("all");
445  // NLogDebug("Not ready to merge, exiting ...");
446  // return 0;
447 
448  NLogTrace("NGnThreadData::Merge: Merging %d storage trees ...", listTreeStorage->GetEntries());
449  // fHnSparseBase->GetBinning()->GetPoint()->Reset();
450  // fHnSparseBase->GetBinning()->GetDefinition("default")->Print();
451  // fHnSparseBase->GetBinning()->GetDefinition("b2")->Print();
452  fHnSparseBase->GetStorageTree()->SetBinning(fHnSparseBase->GetBinning()); // Update binning to the merged one
453  fHnSparseBase->GetStorageTree()->Merge(listTreeStorage);
454  // NLogDebug("Not ready to merge after Storage merge, exiting ...");
455  // return 0;
456 
457  // loop over all output lists and merge them
458  for (auto & kv : listOutputs) {
459  if (kv.second && !kv.second->IsEmpty()) {
460  NLogTrace("NGnThreadData::Merge: Merging output list '%s' with %d objects", kv.first.c_str(),
461  kv.second->GetEntries() + 1);
462  fHnSparseBase->GetOutput(kv.first)->Merge(kv.second);
463  // fHnSparseBase->GetOutput(kv.first)->Print();
464  }
465  }
466 
467  // Loop over binning definitions and merge their contents
469  for (const auto & name : fHnSparseBase->GetBinning()->GetDefinitionNames()) {
470  NBinningDef * binningDef = fHnSparseBase->GetBinning()->GetDefinition(name);
471  if (!binningDef) {
472  NLogError("NGnThreadData::Merge: Binning definition '%s' not found in NGnTree !!!", name.c_str());
473  continue;
474  }
475  // binningDef->Print();
476  // Recalculate binningDef content based on ids
477  binningDef->GetContent()->Reset();
478  for (auto id : binningDef->GetIds()) {
479  fHnSparseBase->GetEntry(id, false);
480  Long64_t bin = binningDef->GetContent()->GetBin(fHnSparseBase->GetBinning()->GetPoint()->GetStorageCoords());
481  binningDef->GetContent()->SetBinContent(bin, id);
482  // binningDef->GetIds().push_back(id);
483  NLogTrace("NGnThreadData::Merge: -> Setting content bin %lld to id %lld", bin, id);
484  }
485  }
486 
487  // // print all definitions
488  // for (const auto & name : fHnSparseBase->GetBinning()->GetDefinitionNames()) {
489  // fHnSparseBase->GetBinning()->GetDefinition(name)->Print();
490  // }
491 
492  if (fHnSparseBase->GetInput()) {
493  fHnSparseBase->GetInput()->Close(false);
494  }
495 
496  // Set default setting
500 
501  NLogTrace("NGnThreadData::Merge: END ------------------------------------------------");
502 
504  // NLogError("NGnThreadData::Merge: Not implemented !!!");
506  return nmerged;
507 }
508 
509 void NGnThreadData::ExecuteBeginFunction()
510 {
511  if (fBeginFunc) {
513  }
514 }
515 
516 void NGnThreadData::ExecuteEndFunction()
517 {
518  if (fEndFunc) {
520  }
521 }
522 
524 {
525  if (fDeferredDeletes.empty()) return;
526 
527  NLogTrace("NGnThreadData::FlushDeferredDeletes: [%zu] Deleting %zu deferred objects ...",
529 
531 }
532 
533 } // namespace Ndmspc
Defines binning mapping and content for NDMSPC histograms.
Definition: NBinningDef.h:26
THnSparse * GetContent() const
Get the template content histogram.
Definition: NBinningDef.h:118
Long64_t GetId(size_t index) const
Get bin ID at specified index.
std::vector< Long64_t > GetIds() const
Get list of bin IDs.
Definition: NBinningDef.h:93
Represents a single point in multi-dimensional binning.
Definition: NBinningPoint.h:21
void SetTreeStorage(NStorageTree *s)
Set storage tree object pointer.
bool RecalculateStorageCoords(Long64_t entry=-1, bool useBinningDefCheck=false)
Recalculate storage coordinates for the point.
Int_t * GetStorageCoords() const
Get pointer to storage coordinates array.
Definition: NBinningPoint.h:67
void SetCfg(json cfg)
Set configuration JSON object.
Int_t * GetCoords() const
Get pointer to content coordinates array.
Definition: NBinningPoint.h:55
void SetParameters(NParameters *params)
Set the parameters for this binning point.
void SetInput(NGnTree *input)
Set input NGnTree object pointer.
virtual void Reset()
Reset the binning point to initial state.
json & GetCfg()
Get reference to configuration JSON object.
Int_t GetNDimensionsContent() const
Get number of dimensions in content histogram.
Definition: NBinningPoint.h:49
NBinning object for managing multi-dimensional binning and axis definitions.
Definition: NBinning.h:45
NBinningDef * GetDefinition(const std::string &name="")
Get binning definition by name.
Definition: NBinning.cxx:1024
std::vector< std::string > GetDefinitionNames() const
Get all definition names.
Definition: NBinning.h:270
NBinningPoint * GetPoint()
Get the current binning point.
Definition: NBinning.cxx:1128
THnSparse * GetContent() const
Get the content histogram.
Definition: NBinning.h:217
void SetCurrentDefinitionName(const std::string &name)
Set current definition name.
Definition: NBinning.cxx:1184
Thread-local data object for NDMSPC processing.
Definition: NGnThreadData.h:19
NGnEndFuncPtr fEndFunc
Function pointer to the end function.
NGnBeginFuncPtr fBeginFunc
Function pointer to the begin function.
bool Init(size_t id, NGnProcessFuncPtr func, NGnBeginFuncPtr beginFunc, NGnEndFuncPtr endFunc, NGnTree *ngnt, NBinning *binningIn, NGnTree *input=nullptr, const std::string &filename="", const std::string &treename="ngnt")
Initialize thread data for processing.
NGnThreadData()
Constructor.
NGnTree * GetHnSparseBase() const
Get pointer to base NGnTree object.
Definition: NGnThreadData.h:65
NBinning * fBiningSource
Pointer to the source binning (from the original NGnTree)
virtual void Process(const std::vector< int > &coords)
Process coordinates (virtual).
Long64_t GetNProcessed() const
Get number of processed entries.
Definition: NGnThreadData.h:71
virtual Long64_t Merge(TCollection *list)
Merge thread data from a collection (virtual).
virtual ~NGnThreadData()
Destructor.
json fCfg
Configuration object.
void FlushDeferredDeletes()
Delete deferred ROOT objects, skipping TCanvas/TPad (leaked safely).
std::vector< TObject * > fDeferredDeletes
Objects deferred for single-threaded deletion.
NGnTree * fHnSparseBase
Pointer to the base class.
Long64_t fNProcessed
Number of processed entries.
const std::string & GetResultsFilename() const
Get the results filename for TCP mode.
bool fIsPureCopy
Flag indicating pure copy mode.
NGnProcessFuncPtr fProcessFunc
Function pointer to the processing function.
NDMSPC tree object for managing multi-dimensional data storage and processing.
Definition: NGnTree.h:75
NBinning * GetBinning() const
Get pointer to binning object.
Definition: NGnTree.h:161
std::map< std::string, TList * > GetOutputs() const
Get outputs map.
Definition: NGnTree.h:179
bool Close(bool write=false)
Close the tree, optionally writing data.
Definition: NGnTree.cxx:1326
bool IsPureCopy() const
Checks if the tree is a pure copy.
Definition: NGnTree.h:223
Int_t GetEntry(Long64_t entry, bool checkBinningDef=true)
Get entry by index.
Definition: NGnTree.cxx:1340
void SetInput(NGnTree *input)
Set input NGnTree pointer.
Definition: NGnTree.h:204
TList * GetOutput(std::string name="")
Get output list by name.
Definition: NGnTree.cxx:1206
NParameters * GetParameters() const
Returns the parameters associated with this tree.
Definition: NGnTree.h:329
NStorageTree * GetStorageTree() const
Get pointer to storage tree object.
Definition: NGnTree.h:173
NGnTree * GetInput() const
Get pointer to input NGnTree.
Definition: NGnTree.h:198
static NGnTree * Open(const std::string &filename, const std::string &branches="", const std::string &treename="ngnt")
Open NGnTree from file.
Definition: NGnTree.cxx:1222
NParameters object.
Definition: NParameters.h:13
Monitors and records resource usage (CPU, memory, wall time) for processes or threads.
void Start()
Records the starting resource usage and wall time.
void End()
Records the ending resource usage and wall time.
void Fill(Int_t *coords, int threadId)
Fills resource usage data into the histogram.
THnSparse * GetHnSparse() const
Returns the pointer to the THnSparse histogram.
THnSparse * Initialize(THnSparse *hns)
Initializes the THnSparse histogram for resource data.
NDMSPC storage tree object for managing ROOT TTree-based data storage.
Definition: NStorageTree.h:22
Long64_t Merge(TCollection *list)
Merge storage trees from a collection.
void SetEnabledBranches(std::vector< std::string > branches, int status=1)
Set enabled/disabled status for branches.
virtual void Clear(Option_t *option="")
Clear storage tree data.
std::string GetFileName() const
Get file name.
Definition: NStorageTree.h:194
bool AddBranch(const std::string &name, void *address, const std::string &className)
Add a branch to the tree.
std::vector< std::string > GetBrancheNames(bool onlyEnabled=false) const
Branches handling.
NTreeBranch * GetBranch(const std::string &name)
Get pointer to NTreeBranch by name.
bool InitTree(const std::string &filename="", const std::string &treename="ngnt")
Initialize tree from file and tree name.
Int_t Fill(NBinningPoint *point, NStorageTree *hnstIn=nullptr, bool ignoreFilledCheck=false, std::vector< std::vector< int >> ranges={}, bool useProjection=false)
Fill tree with NBinningPoint and optional input tree.
void SetBinning(NBinning *binning)
Set binning object pointer.
Definition: NStorageTree.h:182
std::map< std::string, NTreeBranch > GetBranchesMap() const
Get map of branch names to NTreeBranch objects.
Definition: NStorageTree.h:129
Thread-local data object for NDMSPC processing.
Definition: NThreadData.h:21
size_t GetAssignedIndex() const
Get the assigned index for the thread.
Definition: NThreadData.h:96
void SetAssignedIndex(size_t assignedIndex)
Set the assigned index for the thread.
Definition: NThreadData.h:53
NResourceMonitor * fResourceMonitor
Pointer to resource monitor.
Definition: NThreadData.h:133
void SetThreadId(std::thread::id threadId)
Set the thread's unique identifier.
Definition: NThreadData.h:41
NDMSPC tree branch object for managing ROOT TBranch and associated data.
Definition: NTreeBranch.h:18
void SetAddress(void *address, bool deleteExisting=false)
Set address for branch data.
Definition: NTreeBranch.cxx:59
static std::string Join(const std::vector< std::string > &values, const char delim=',')
Join vector of strings into a single string with delimiter.
Definition: NUtils.cxx:1111
static std::string GetCoordsString(const std::vector< int > &coords, int index=-1, int width=0)
Get string representation of coordinates.
Definition: NUtils.cxx:1588
static void SafeDeleteObjects(std::vector< TObject * > &objects)
Safely delete a vector of ROOT objects, bypassing GarbageCollect.
Definition: NUtils.cxx:1953
static std::vector< int > ArrayToVector(Int_t *v1, int size)
Convert array to vector.
Definition: NUtils.cxx:1551