CbmRoot
CbmHistoServer.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #include <mutex>
9 
10 #include "CbmHistoServer.h"
11 //#include "CbmHistoCanvasDrawer.h"
12 #include "FairLogger.h"
13 #include "RootSerializer.h"
14 
15 #include "TH1.h"
16 #include "THttpServer.h"
17 #include "TMessage.h"
18 #include "TObjArray.h"
19 
21 
23  : FairMQDevice()
24  , fInputChannelName("histogram-in")
25  , fArrayHisto()
26  , fNMessages(0)
27  , fServer("http:8088")
28  // , fCanvasDrawer(nullptr)
29  , fStopThread(false) {}
30 
32 
35 
36  /*
37  if (fCanvasDrawer)
38  {
39  fCanvasDrawer->CreateCanvases(fServer);
40  }
41 */
42 }
43 
44 bool CbmHistoServer::ReceiveData(FairMQMessagePtr& msg, int /*index*/) {
45  TObject* tempObject = nullptr;
46 #ifdef HAVE_RootDeserializer
47  Deserialize<RootDeserializer>(*msg, tempObject);
48 #else
49  Deserialize<RootSerializer>(*msg, tempObject);
50 #endif
51 
52  if (TString(tempObject->ClassName()).EqualTo("TObjArray")) {
53  std::lock_guard<std::mutex> lk(mtx);
54  TObjArray* arrayHisto = static_cast<TObjArray*>(tempObject);
55  TH1* histogram_new;
56  TH1* histogram_existing;
57  for (Int_t i = 0; i < arrayHisto->GetEntriesFast(); i++) {
58  TObject* obj = arrayHisto->At(i);
59  TH1* histogram = static_cast<TH1*>(obj);
60  int index1 = FindHistogram(histogram->GetName());
61  if (-1 == index1) {
62  histogram_new = static_cast<TH1*>(histogram->Clone());
63  fArrayHisto.Add(histogram_new);
64  fServer.Register("Histograms", histogram_new);
65  } else {
66  histogram_existing = static_cast<TH1*>(fArrayHisto.At(index1));
67  histogram_existing->Add(histogram);
68  }
69  }
70 
71  arrayHisto->Clear();
72  }
73 
74  fNMessages += 1;
75 
76  delete tempObject;
77 
78  return true;
79 }
80 
82  fStopThread = false;
83  fThread = std::thread(&CbmHistoServer::UpdateHttpServer, this);
84 }
85 
87  while (!fStopThread) {
88  std::this_thread::sleep_for(std::chrono::milliseconds(10));
89  std::lock_guard<std::mutex> lk(mtx);
90 
91  /*
92  if (fCanvasDrawer)
93  {
94  fCanvasDrawer->DrawHistograms(fArrayHisto);
95  }
96 */
97 
98  fServer.ProcessRequests();
99  }
100 }
101 
103  fStopThread = true;
104  fThread.join();
105 }
106 
107 int CbmHistoServer::FindHistogram(const std::string& name) {
108  for (int i = 0; i < fArrayHisto.GetEntriesFast(); i++) {
109  TObject* obj = fArrayHisto.At(i);
110  if (TString(obj->GetName()).EqualTo(name)) { return i; }
111  }
112  return -1;
113 }
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmHistoServer::FindHistogram
int FindHistogram(const std::string &name)
Definition: CbmHistoServer.cxx:107
CbmHistoServer::PostRun
virtual void PostRun()
Definition: CbmHistoServer.cxx:102
CbmHistoServer::fArrayHisto
TObjArray fArrayHisto
Definition: CbmHistoServer.h:56
CbmHistoServer::fInputChannelName
std::string fInputChannelName
Definition: CbmHistoServer.h:54
CbmHistoServer::UpdateHttpServer
void UpdateHttpServer()
Definition: CbmHistoServer.cxx:86
mtx
std::mutex mtx
Definition: CbmHistoServer.cxx:20
CbmHistoServer::ReceiveData
bool ReceiveData(FairMQMessagePtr &msg, int index)
Definition: CbmHistoServer.cxx:44
CbmHistoServer::PreRun
virtual void PreRun()
Definition: CbmHistoServer.cxx:81
mutex
spin_mutex mutex
Definition: CbmRichParallelQa.cxx:30
CbmHistoServer::fThread
std::thread fThread
Definition: CbmHistoServer.h:64
CbmHistoServer::fServer
THttpServer fServer
Definition: CbmHistoServer.h:60
CbmHistoServer.h
CbmHistoServer::~CbmHistoServer
virtual ~CbmHistoServer()
Definition: CbmHistoServer.cxx:31
CbmHistoServer::fNMessages
int fNMessages
Definition: CbmHistoServer.h:58
CbmHistoServer::CbmHistoServer
CbmHistoServer()
Definition: CbmHistoServer.cxx:22
CbmHistoServer::fStopThread
bool fStopThread
Definition: CbmHistoServer.h:65
CbmHistoServer::InitTask
virtual void InitTask()
Definition: CbmHistoServer.cxx:33