BmnRoot
Loading...
Searching...
No Matches
BmnCustomQa.cxx
Go to the documentation of this file.
1#include "BmnCustomQa.h"
2
3#include <filesystem>
4#include <fstream>
5#include <iostream>
6#include <mutex>
7// Boost
8#include <boost/beast/core/detail/base64.hpp>
9// ROOT
10#include "TClonesArray.h"
11#include "TH1.h"
12#include "TH2F.h"
13// FairRoot
14#include <FairRootManager.h>
15#include <fairlogger/Logger.h>
16// BmnRoot
17#include "BmnEnums.h"
18#include "BmnHistManager.h"
19#include "BmnMath.h"
20
21#include <THttpServer.h>
22#include <THttpWSHandler.h>
23
24namespace fs = std::filesystem;
25namespace b64 = boost::beast::detail::base64;
26
27BmnCustomQa::BmnCustomQa(string name, string padConfFile)
28 : BmnQaBase(name.c_str(), 1)
29 , fPadConfDirWeb("config/")
30 , fPadConfDir("config/")
31 , fPadConfFileNameDefault(fPadConfDir + "pad-sample-default.json")
32 , fPadConfFileName(padConfFile)
33 , fPadGenerator(nullptr)
34 , fPadTree(nullptr)
35 , fTreeTemp(nullptr)
36 , can(nullptr)
37{
38 SetTitle((name + "_title").c_str());
39 LOGF(info, "fName %s", fName.Data());
40 LOGF(info, "fTitle %s", fTitle.Data());
41 /* ifstream in(fPadConfFileName);
42 if (!in)
43 throw string("Cannot open file " + fPadConfFileName);
44 stringstream ss;
45 ss << in.rdbuf();
46 fPadConfText = ss.str();
47 cout << fPadConfText << endl; */
48}
49/* /root.dst/custom_qa_title/SaveConfig/cmd.json
50/root.dst/custom_qa_title/SaveConfig/cmd.json */
51
53{
54 LOGF(info, "~BmnCustomQa()");
55}
56
58{
59 fHM = new BmnHistManager();
60 ReadDataBranches();
61 CreateHistograms();
62 if (fMonitorMode) {
63 // fServer->SetTimer(50, kFALSE);
64 fServer->Register("/", this);
65 fServer->Register("/", can);
66 // fServer->Register("/", &fPadConfText);
67 fServer->AddLocation(fPadConfDirWeb.c_str(), fPadConfDir.c_str());
68
69 TString path = "/" + fTitle + "/";
70 TString cmd = "/" + fName + "/->ResetPadTree()";
71 TString cmdTitle = path + "Reset";
72 LOGF(info, "cmdTitle %s", cmdTitle.Data());
73 fServer->RegisterCommand(cmdTitle.Data(), cmd.Data(), "button;");
74
75 cmd = "/" + fName + "/->SaveConfig(%arg1%)";
76 cmdTitle = path + "SaveConfig";
77 LOGF(info, "cmdTitle %s", cmdTitle.Data());
78 fServer->RegisterCommand(cmdTitle.Data(), cmd.Data(), "button;");
79
80 fServer->Restrict((TString("/") + fName).Data(),
81 (TString("allow_method=") + "FetchConfig,LoadDefaultConfig").Data());
82 } else {
83 }
84 return kSUCCESS;
85}
86
87void BmnCustomQa::Exec(Option_t* opt)
88{
89 if (fEventNo % 100 == 0)
90 printf("Events: %d\n", fEventNo);
91 fEventNo++;
92 ProcessGlobal();
93 if (fMonitorMode) {
95 fServer->ProcessRequests();
96 chrono::seconds time = chrono::duration_cast<chrono::seconds>(SysClock::now() - fTicksLastUpdate);
98 Redraw();
99 }
100 }
101}
102
104{
105 DBG("Finish()");
106 // fServer->Restrict((TString("/") + fName).Data(), TString("allow_method=") + "FetchConfig").Data());
107 fHM->WriteToFile();
108 /* if (!fMonitorMode) {
109 Redraw();
110 // fReport = new BmnCustomQaReport(fOutName);
111 // fReport->SetOnlyPrimes(fPrimes);
112 // fReport->Create(fHM, fOutputDir);
113 } */
114 Redraw();
115 can->SaveAs("custom.pdf");
116}
117
118void BmnCustomQa::Redraw()
119{
120 LOGF(info, "Redraw pid %d", getpid());
121 // std::lock_guard<std::mutex> lock(mtx_can);
122 /* if (mtx_can.try_lock()) {
123 LOGF(info, "Redraw try_lock za");
124 } else {
125 LOGF(info, "Redraw try_lock ne");
126 return;
127 } */
128 BmnHist::FillPadFromTree(fPadTree, fTreeTemp);
129 fTreeTemp->Reset();
130 BmnHist::DrawPadFromTree(fPadTree);
131 if (fVerbose)
132 printf("Draw! iters %d\n", fNItersSinceUpdate);
133 fTicksLastUpdate = SysClock::now();
135 /* mtx_can.unlock(); */
136 return;
137}
138
139void BmnCustomQa::ReadDataBranches()
140{
141 FairRootManager* ioman = FairRootManager::Instance();
142 if (nullptr == ioman)
143 Fatal("Init", "BmnRootManager is not instantiated");
144 fTreeTemp = new TTree(fName + "_tree", "qq");
145 fTreeTemp->SetDirectory(nullptr);
146 TList* brList = ioman->GetBranchNameList();
147 for (Int_t i = 0; i < brList->GetEntries(); i++) {
148 TString str = static_cast<TObjString*>(brList->At(i))->GetString();
149 LOGF(debug, " str: %s", str.Data());
150 TObject* obj = ioman->GetObject(str);
151 if (obj == nullptr)
152 continue;
153 LOGF(debug, " obj: %p", (void*)obj);
154 TClass* cl = TClass::GetClass(obj->ClassName());
155 LOGF(debug, "ClassName: %s", obj->ClassName());
156 if (cl == TClonesArray::Class()) {
157 TClonesArray* newTCA = static_cast<TClonesArray*>(obj);
158 fTreeTemp->Branch(str.Data(), &newTCA);
159 // fArrVec.push_back(newTCA);
160
161 } else {
162 if (cl == TFolder::Class())
163 continue;
164 TNamed* workObj = dynamic_cast<TNamed*>(obj);
165 if (workObj == nullptr) {
166 LOGF(error, "%s is not a TNamed!", str.Data());
167 continue;
168 }
169 LOGF(debug, " TNamed obj n: %s t: %s", workObj->GetName(), workObj->GetTitle());
170 TBranch* b = fTreeTemp->Branch(str.Data(), "TObject", workObj);
171 LOGF(debug, " branch 0x%016lX", (uint64_t)b);
172 // fNamVec.push_back(workObj);
173 }
174 }
175 printf("\nBRANCHES READ!\n\n");
176}
177
178void BmnCustomQa::CreateHistograms()
179{
180 can = new TCanvas("CustomCan", "Custom Canvas", 1920, 1080);
181 RecreateHistTree();
182 printf("\nHISTOGRAMS CREATED!\n\n");
183}
184
185void BmnCustomQa::RecreateHistTree()
186{
187 LOGF(info, "RecreateHistTree pid %d", getpid());
188 // std::lock_guard<std::mutex> lock(mtx_can);
189 /* if (mtx_can.try_lock()) {
190 LOGF(info, "RecreateHistTree try_lock za");
191 } else {
192 LOGF(info, "RecreateHistTree try_lock ne");
193 return;
194 } */
195 // mtx_can.lock();
196 try {
197 can->Clear();
198 ReloadConf();
200 } catch (std::exception& e) {
201 LOGF(error, "RecreateHistTree exception: %s", e.what());
202 }
203 /* mtx_can.unlock(); */
204 return;
205}
206
207void BmnCustomQa::ProcessGlobal()
208{
209 fTreeTemp->Fill();
210 // printf("tree len %lld\n", fTreeTemp->GetEntriesFast());
211}
212
214{
215 LOGF(info, "ResetPadTree");
216 BmnHist::ResetPadTree(fPadTree);
217}
218
219void BmnCustomQa::ReloadConf()
220{
221 if (fPadGenerator)
222 delete fPadGenerator;
223 fPadGenerator = new BmnPadGenerator();
224 std::ifstream ifs(fPadConfFileName, std::ios::binary);
225 if (!ifs) {
226 LOGF(error, "Failed to open config file: %s", fPadConfFileName.c_str());
227 return;
228 }
229 string str_out{std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>()};
230 fCurConfig.String() = TString(str_out.c_str());
231 fPadGenerator->LoadPTFrom(fPadConfFileName);
232 fPadTree = fPadGenerator->GetPadBranch();
233}
234
236{
237 LOGF(info, "SaveConfig");
238 string conf_str;
239 conf_str.resize(b64::decoded_size(str.size()));
240 auto res = b64::decode(&conf_str[0], str.data(), str.size());
241 conf_str.resize(res.first);
242 LOG(debug) << conf_str;
243 ofstream out(fPadConfFileName, std::ios::out);
244 if (!out)
245 LOGF(error, "SaveConfig failed to open %s for writing", fPadConfFileName.c_str());
246 out << conf_str;
247 out.flush();
248 out.close();
249 RecreateHistTree();
250}
251
253{
254 LOGF(info, "LoadDefaultConfig pid %d", getpid());
255 try {
256 fs::copy(fPadConfFileNameDefault, fPadConfFileName, fs::copy_options::overwrite_existing);
257 } catch (fs::filesystem_error& e) {
258 LOGF(error, "LoadDefaultConfig failed to copy %s to %s: %s", fPadConfFileNameDefault.c_str(),
259 fPadConfFileName.c_str(), e.what());
260 return nullptr; //"LoadDefaultConfig failed to copy";
261 }
262 RecreateHistTree();
263 return &fCurConfig;
264}
265
267{
268 LOGF(info, "FetchConfig");
269 return &fCurConfig;
270}
TCanvas * can(nullptr)
int i
Definition P4_F32vec4.h:22
#define DBG(a)
Definition BmnMath.h:24
TObjString * LoadDefaultConfig()
virtual void Exec(Option_t *opt)
Derived from FairTask.
virtual void Finish()
Derived from FairTask.
virtual ~BmnCustomQa()
Destructor.
TObjString * FetchConfig()
void SaveConfig(string str)
virtual InitStatus Init()
Derived from FairTask.
void ResetPadTree()
BmnCustomQa()
Constructor.
Definition BmnCustomQa.h:27
Histogram manager.
void WriteToFile()
Write all histograms to current opened file.
static BmnStatus DrawPadFromTree(BmnPadBranch *br)
Definition BmnHist.cxx:165
static void ResetPadTree(BmnPadBranch *br)
Definition BmnHist.cxx:146
static BmnStatus FillPadFromTree(BmnPadBranch *br, TTree *tree)
Definition BmnHist.cxx:180
Generates pad tree from the scheme.
static void PadTree2Canvas(BmnPadBranch *br, TVirtualPad *pad)
Create pad structure in the canvas from the pad tree and associate pad branch instances with the pad ...
BmnPadBranch * GetPadBranch()
void LoadPTFrom(string FileName)
Load pad tree from json config.
Bool_t fMonitorMode
Definition BmnQaBase.h:157
chrono::time_point< chrono::system_clock > fTicksLastUpdate
Definition BmnQaBase.h:167
const Int_t fNItersToUpdate
Definition BmnQaBase.h:164
const chrono::seconds fTimeToUpdate
Definition BmnQaBase.h:165
BmnHistManager * fHM
Definition BmnQaBase.h:152
THttpServer * fServer
Definition BmnQaBase.h:149
Int_t fEventNo
Definition BmnQaBase.h:161
Int_t fNItersSinceUpdate
redraw each timeout seconds
Definition BmnQaBase.h:166