CbmRoot
Loading...
Searching...
No Matches
CbmHistoServer.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer] */
4
5#include "CbmHistoServer.h"
6
7#include <mutex>
8//#include "CbmHistoCanvasDrawer.h"
9#include <Logger.h>
10
11#include "TH1.h"
12#include "THttpServer.h"
13#include "TMessage.h"
14#include "TObjArray.h"
15
16#include "RootSerializer.h"
17
18std::mutex mtx;
19
21 : FairMQDevice()
22 , fInputChannelName("histogram-in")
23 , fArrayHisto()
24 , fNMessages(0)
25 , fServer("http:8088")
26 // , fCanvasDrawer(nullptr)
27 , fStopThread(false)
28{
29}
30
32
34{
36
37 /*
38 if (fCanvasDrawer)
39 {
40 fCanvasDrawer->CreateCanvases(fServer);
41 }
42*/
43}
44
45bool CbmHistoServer::ReceiveData(FairMQMessagePtr& msg, int /*index*/)
46{
47 TObject* tempObject = nullptr;
48 RootSerializer().Deserialize(*msg, tempObject);
49
50 if (TString(tempObject->ClassName()).EqualTo("TObjArray")) {
51 std::lock_guard<std::mutex> lk(mtx);
52 TObjArray* arrayHisto = static_cast<TObjArray*>(tempObject);
53 TH1* histogram_new;
54 TH1* histogram_existing;
55 for (Int_t i = 0; i < arrayHisto->GetEntriesFast(); i++) {
56 TObject* obj = arrayHisto->At(i);
57 TH1* histogram = static_cast<TH1*>(obj);
58 int index1 = FindHistogram(histogram->GetName());
59 if (-1 == index1) {
60 histogram_new = static_cast<TH1*>(histogram->Clone());
61 fArrayHisto.Add(histogram_new);
62 fServer.Register("Histograms", histogram_new);
63 }
64 else {
65 histogram_existing = static_cast<TH1*>(fArrayHisto.At(index1));
66 histogram_existing->Add(histogram);
67 }
68 }
69
70 arrayHisto->Clear();
71 }
72
73 fNMessages += 1;
74
75 delete tempObject;
76
77 return true;
78}
79
81{
82 fStopThread = false;
83 fThread = std::thread(&CbmHistoServer::UpdateHttpServer, this);
84}
85
87{
88 while (!fStopThread) {
89 std::this_thread::sleep_for(std::chrono::milliseconds(10));
90 std::lock_guard<std::mutex> lk(mtx);
91
92 /*
93 if (fCanvasDrawer)
94 {
95 fCanvasDrawer->DrawHistograms(fArrayHisto);
96 }
97*/
98
99 fServer.ProcessRequests();
100 }
101}
102
104{
105 fStopThread = true;
106 fThread.join();
107}
108
109int CbmHistoServer::FindHistogram(const std::string& name)
110{
111 for (int i = 0; i < fArrayHisto.GetEntriesFast(); i++) {
112 TObject* obj = fArrayHisto.At(i);
113 if (TString(obj->GetName()).EqualTo(name)) { return i; }
114 }
115 return -1;
116}
std::mutex mtx
virtual ~CbmHistoServer()
virtual void PostRun()
THttpServer fServer
virtual void InitTask()
TObjArray fArrayHisto
std::string fInputChannelName
std::thread fThread
virtual void PreRun()
int FindHistogram(const std::string &name)
bool ReceiveData(FairMQMessagePtr &msg, int index)