FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMQExHistoServer.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 
9 #include "FairMQExHistoServer.h"
10 
11 #include "FairLogger.h"
13 #include "RootSerializer.h"
14 
15 #include <TH1.h>
16 #include <TObject.h>
17 #include <TString.h>
18 #include <chrono>
19 #include <mutex>
20 
21 std::mutex mtx;
22 
24  : fInputChannelName("histogram-in")
25  , fArrayHisto()
26  , fNMessages(0)
27  , fServer("http:8080")
28  , fCanvasDrawer(nullptr)
29  , fStopThread(false)
30 {}
31 
33 
35 {
36  OnData(fInputChannelName, &FairMQExHistoServer::ReceiveData);
37 
38  if (fCanvasDrawer) {
39  fCanvasDrawer->CreateCanvases(fServer);
40  }
41 }
42 
43 bool FairMQExHistoServer::ReceiveData(FairMQMessagePtr& msg, int index)
44 {
45  TObject* tempObject = nullptr;
46  Deserialize<RootSerializer>(*msg, tempObject);
47 
48  if (TString(tempObject->ClassName()).EqualTo("TObjArray")) {
49  std::lock_guard<std::mutex> lk(mtx);
50  TObjArray* arrayHisto = static_cast<TObjArray*>(tempObject);
51  TH1* histogram_new;
52  TH1* histogram_existing;
53  for (int i = 0; i < arrayHisto->GetEntriesFast(); i++) {
54  TObject* obj = arrayHisto->At(i);
55  TH1* histogram = static_cast<TH1*>(obj);
56  int index1 = FindHistogram(histogram->GetName());
57  if (-1 == index1) {
58  histogram_new = static_cast<TH1*>(histogram->Clone());
59  fArrayHisto.Add(histogram_new);
60  fServer.Register("Histograms", histogram_new);
61  } else {
62  histogram_existing = static_cast<TH1*>(fArrayHisto.At(index1));
63  histogram_existing->Add(histogram);
64  }
65  }
66 
67  arrayHisto->Clear();
68  }
69 
70  fNMessages += 1;
71 
72  delete tempObject;
73 
74  return true;
75 }
76 
78 {
79  fStopThread = false;
80  fThread = std::thread(&FairMQExHistoServer::UpdateHttpServer, this);
81 }
82 
84 {
85  while (!fStopThread) {
86  std::this_thread::sleep_for(std::chrono::milliseconds(10));
87  std::lock_guard<std::mutex> lk(mtx);
88 
89  if (fCanvasDrawer) {
90  fCanvasDrawer->DrawHistograms(fArrayHisto);
91  }
92 
93  fServer.ProcessRequests();
94  }
95 }
96 
98 {
99  fStopThread = true;
100  fThread.join();
101 }
102 
103 int FairMQExHistoServer::FindHistogram(const std::string& name)
104 {
105  for (int i = 0; i < fArrayHisto.GetEntriesFast(); i++) {
106  TObject* obj = fArrayHisto.At(i);
107  if (TString(obj->GetName()).EqualTo(name)) {
108  return i;
109  }
110  }
111  return -1;
112 }
bool ReceiveData(FairMQMessagePtr &msg, int index)
std::mutex mtx