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 "GRootCommands.h"
18#include "TPreserveGDirectory.h"
19
20using void_alias = void*;
21
23 : fFunc(nullptr), fObj(&fObjects, &fGates, fCutFiles)
24{
25}
26
27TCompiledHistograms::TCompiledHistograms(std::string inputLib, std::string funcName)
28 : fLibName(std::move(inputLib)), fFuncName(std::move(funcName)), fFunc(nullptr), fObj(&fObjects, &fGates, fCutFiles)
29{
30 fLibrary = std::make_shared<DynamicLibrary>(fLibName.c_str(), true);
31 // Casting required to keep gcc from complaining.
32 *reinterpret_cast<void_alias*>(&fFunc) = fLibrary->GetSymbol(fFuncName.c_str());
33
34 if(fFunc == nullptr) {
35 std::cout << "Could not find " << fFuncName << "() inside "
36 << R"(")" << inputLib << R"(")" << std::endl;
37 }
39 fLastChecked = time(nullptr);
40}
41
43{
44 std::lock_guard<std::mutex> lock(fMutex);
45
46 TIter next(&fObjects);
47 TObject* obj = nullptr;
48 while((obj = next()) != nullptr) {
49 if(obj->InheritsFrom(TH1::Class())) {
50 auto* hist = static_cast<TH1*>(obj);
51 hist->Reset();
52 } else if(obj->InheritsFrom(TDirectory::Class())) {
53 auto* dir = static_cast<TDirectory*>(obj);
54 TIter dirnext(dir->GetList());
55 TObject* dirobj = nullptr;
56 while((dirobj = dirnext()) != nullptr) {
57 if(dirobj->InheritsFrom(TH1::Class())) {
58 auto* hist = static_cast<TH1*>(dirobj);
59 hist->Reset();
60 }
61 }
62 }
63 }
64 std::cout << "ended " << std::endl;
65}
66
68{
69 struct stat buf{};
70 stat(fLibName.c_str(), &buf);
71 return buf.st_mtime;
72}
73
75{
76 std::ifstream infile(fLibName);
77 return infile.is_open();
78}
79
80Int_t TCompiledHistograms::Write(const char*, Int_t, Int_t)
81{
82 fObjects.Sort();
83
84 TIter next(&fObjects);
85 TObject* obj = nullptr;
86 while((obj = next()) != nullptr) {
87 if(obj->InheritsFrom(TDirectory::Class())) {
88 // WATCH OUT: THIS DOESN'T SEEM THREAD-SAFE DUE TO gDIRECTORY BEING USED.
89 TPreserveGDirectory preserve;
90 auto* dir = static_cast<TDirectory*>(obj);
91 gDirectory->mkdir(dir->GetName())->cd();
92 TIter dir_next(dir->GetList());
93 TObject* dir_obj = nullptr;
94 while((dir_obj = dir_next()) != nullptr) {
95 dir_obj->Write();
96 }
97 } else {
98 obj->Write();
99 }
100 }
101
102 // objects.Write();
103 TPreserveGDirectory preserve;
104 gDirectory->mkdir("variables")->cd();
105 return 1;
106 // variables.Write();
107}
108
109void TCompiledHistograms::Load(const std::string& libName, const std::string& funcName)
110{
111 TCompiledHistograms other(libName, funcName);
112 swap_lib(other);
113}
114
116{
119 swap_lib(other);
120 }
121 fLastChecked = time(nullptr);
122}
123
125{
126 std::swap(fLibName, other.fLibName);
127 std::swap(fFuncName, other.fFuncName);
128 std::swap(fLibrary, other.fLibrary);
129 std::swap(fFunc, other.fFunc);
130 std::swap(fLastModified, other.fLastModified);
131 std::swap(fLastChecked, other.fLastChecked);
132 std::swap(fCheckEvery, other.fCheckEvery);
133}
134
135void TCompiledHistograms::Fill(std::shared_ptr<const TFragment> frag)
136{
137 std::lock_guard<std::mutex> lock(fMutex);
138 if(time(nullptr) > fLastChecked + fCheckEvery) {
139 Reload();
140 }
141
142 if(!fLibrary || (fFunc == nullptr) || (fDefaultDirectory == nullptr)) {
143 return;
144 }
145
146 TPreserveGDirectory preserve;
147 // fDefaultDirectory->cd();
149
150 fObj.SetFragment(std::move(frag));
151 fFunc(fObj);
152 fObj.SetFragment(nullptr);
153}
154
155void TCompiledHistograms::Fill(std::shared_ptr<TUnpackedEvent> detectors)
156{
157 std::lock_guard<std::mutex> lock(fMutex);
158 if(time(nullptr) > fLastChecked + fCheckEvery) {
159 Reload();
160 }
161
162 if(!fLibrary || (fFunc == nullptr) || (fDefaultDirectory == nullptr)) {
163 return;
164 }
165
166 TPreserveGDirectory preserve;
167 // fDefaultDirectory->cd();
169
170 fObj.SetDetectors(std::move(detectors));
171 fFunc(fObj);
172 fObj.SetDetectors(nullptr);
173}
174
176{
177 if(cut_file != nullptr) {
178 fCutFiles.push_back(cut_file);
179 }
180}
181
183{
184 fDefaultDirectory = dir;
185
186 TObject* obj = nullptr;
187 TIter next(&fObjects);
188 while((obj = next()) != nullptr) {
189 TH1* hist = static_cast<TH1*>(obj);
190 if(hist != nullptr) {
191 hist->SetDirectory(dir);
192 }
193 }
194}
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)