GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TCompiledHistograms.cxx
Go to the documentation of this file.
2
3#include <algorithm>
4#include <fstream>
5#include <iostream>
6#include <utility>
7
8#include <sys/stat.h>
9
10#include "TH1.h"
11#include "TFile.h"
12#include "TDirectory.h"
13#include "TObject.h"
14#include "TROOT.h"
15#include "TKey.h"
16
17#include "GValue.h"
18#include "GRootCommands.h"
19#include "TPreserveGDirectory.h"
20
21using void_alias = void*;
22
24 : fFunc(nullptr), fObj(&fObjects, &fGates, fCutFiles)
25{
26}
27
28TCompiledHistograms::TCompiledHistograms(std::string inputLib, std::string funcName)
29 : fLibName(std::move(inputLib)), fFuncName(std::move(funcName)), fFunc(nullptr), fObj(&fObjects, &fGates, fCutFiles)
30{
31 fLibrary = std::make_shared<DynamicLibrary>(fLibName.c_str(), true);
32 // Casting required to keep gcc from complaining.
33 *reinterpret_cast<void_alias*>(&fFunc) = fLibrary->GetSymbol(fFuncName.c_str());
34
35 if(fFunc == nullptr) {
36 std::cout << "Could not find " << fFuncName << "() inside "
37 << R"(")" << inputLib << R"(")" << std::endl;
38 }
40 fLastChecked = time(nullptr);
41}
42
44{
45 std::lock_guard<std::mutex> lock(fMutex);
46
47 TIter next(&fObjects);
48 TObject* obj = nullptr;
49 while((obj = next()) != nullptr) {
50 if(obj->InheritsFrom(TH1::Class())) {
51 auto* hist = static_cast<TH1*>(obj);
52 hist->Reset();
53 } else if(obj->InheritsFrom(TDirectory::Class())) {
54 auto* dir = static_cast<TDirectory*>(obj);
55 TIter dirnext(dir->GetList());
56 TObject* dirobj = nullptr;
57 while((dirobj = dirnext()) != nullptr) {
58 if(dirobj->InheritsFrom(TH1::Class())) {
59 auto* hist = static_cast<TH1*>(dirobj);
60 hist->Reset();
61 }
62 }
63 }
64 }
65 std::cout << "ended " << std::endl;
66}
67
69{
70 struct stat buf{};
71 stat(fLibName.c_str(), &buf);
72 return buf.st_mtime;
73}
74
76{
77 std::ifstream infile(fLibName);
78 return infile.is_open();
79}
80
81Int_t TCompiledHistograms::Write(const char*, Int_t, Int_t)
82{
83 fObjects.Sort();
84
85 TIter next(&fObjects);
86 TObject* obj = nullptr;
87 while((obj = next()) != nullptr) {
88 if(obj->InheritsFrom(TDirectory::Class())) {
89 // WATCH OUT: THIS DOESN'T SEEM THREAD-SAFE DUE TO gDIRECTORY BEING USED.
90 TPreserveGDirectory preserve;
91 auto* dir = static_cast<TDirectory*>(obj);
92 gDirectory->mkdir(dir->GetName())->cd();
93 TIter dir_next(dir->GetList());
94 TObject* dir_obj = nullptr;
95 while((dir_obj = dir_next()) != nullptr) {
96 dir_obj->Write();
97 }
98 } else {
99 obj->Write();
100 }
101 }
102
103 // objects.Write();
104 TPreserveGDirectory preserve;
105 gDirectory->mkdir("variables")->cd();
106 return 1;
107 // variables.Write();
108}
109
110void TCompiledHistograms::Load(const std::string& libName, const std::string& funcName)
111{
112 TCompiledHistograms other(libName, funcName);
113 swap_lib(other);
114}
115
117{
120 swap_lib(other);
121 }
122 fLastChecked = time(nullptr);
123}
124
126{
127 std::swap(fLibName, other.fLibName);
128 std::swap(fFuncName, other.fFuncName);
129 std::swap(fLibrary, other.fLibrary);
130 std::swap(fFunc, other.fFunc);
131 std::swap(fLastModified, other.fLastModified);
132 std::swap(fLastChecked, other.fLastChecked);
133 std::swap(fCheckEvery, other.fCheckEvery);
134}
135
136void TCompiledHistograms::Fill(std::shared_ptr<const TFragment> frag)
137{
138 std::lock_guard<std::mutex> lock(fMutex);
139 if(time(nullptr) > fLastChecked + fCheckEvery) {
140 Reload();
141 }
142
143 if(!fLibrary || (fFunc == nullptr) || (fDefaultDirectory == nullptr)) {
144 return;
145 }
146
147 TPreserveGDirectory preserve;
148 // fDefaultDirectory->cd();
150
151 fObj.SetFragment(std::move(frag));
152 fFunc(fObj);
153 fObj.SetFragment(nullptr);
154}
155
156void TCompiledHistograms::Fill(std::shared_ptr<TUnpackedEvent> detectors)
157{
158 std::lock_guard<std::mutex> lock(fMutex);
159 if(time(nullptr) > fLastChecked + fCheckEvery) {
160 Reload();
161 }
162
163 if(!fLibrary || (fFunc == nullptr) || (fDefaultDirectory == nullptr)) {
164 return;
165 }
166
167 TPreserveGDirectory preserve;
168 // fDefaultDirectory->cd();
170
171 fObj.SetDetectors(std::move(detectors));
172 fFunc(fObj);
173 fObj.SetDetectors(nullptr);
174}
175
177{
178 if(cut_file != nullptr) {
179 fCutFiles.push_back(cut_file);
180 }
181}
182
184{
185 fDefaultDirectory = dir;
186
187 TObject* obj = nullptr;
188 TIter next(&fObjects);
189 while((obj = next()) != nullptr) {
190 TH1* hist = static_cast<TH1*>(obj);
191 if(hist != nullptr) {
192 hist->SetDirectory(dir);
193 }
194 }
195}
void * void_alias
TH1D * hist
Definition UserFillObj.h:3
void AddCutFile(TFile *cut_file)
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
std::vector< TFile * > fCutFiles
void swap_lib(TCompiledHistograms &other)
std::shared_ptr< DynamicLibrary > fLibrary
void Fill(std::shared_ptr< const TFragment > frag)
void(* fFunc)(TRuntimeObjects &)
void SetDefaultDirectory(TDirectory *dir)
void Load(const std::string &libName, const std::string &funcName)
void SetFragment(std::shared_ptr< const TFragment > frag)
void SetDetectors(std::shared_ptr< TUnpackedEvent > det)
void SetDirectory(TDirectory *dir)