GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4HisConnectorRunnable.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
15 
16 #include <iostream>
17 
18 #include "TH1.h"
19 #include "TGraph.h"
20 
21 #include "TGo4LockGuard.h"
22 
23 #include "TGo4Task.h"
24 #include "TGo4AnalysisClientImp.h"
25 #include "TGo4AnalysisImp.h"
26 #include "TGo4HistogramServer.h"
27 #include "TGo4MbsHist.h"
28 
29 extern "C" {
30  #include "f_his_hist.h"
31 }
32 
33 TGo4HisConnectorRunnable::TGo4HisConnectorRunnable() : TGo4Runnable(nullptr, nullptr), fxHistogramServer(nullptr) {}
34 
36  : TGo4Runnable(name, hserv->GetAnalysisClient()->GetTask()), fxHistogramServer(hserv)
37 {
38 }
39 
41 
43 {
44  // wait for client request on server
45  // note: we use the __gsi histogram api__ here
46  // instead of the first approach that intended to launch
47  // one thread per client which is up permanently until client disconnects.
48  Bool_t histofromgraph = kFALSE;
49  Int_t action = 0;
50  char histo[128];
51  TH1 *his = nullptr;
52  TGo4MbsHist *mbshisto = nullptr;
54  Int_t result = f_his_wait(&action, histo);
55  // if(! GetThread()->IsRunning()) return 1; // fast stop in case of shutdown
56  if (result == COMM__SUCCESS) {
57  switch (action) {
58  case COMM__GETHIST:
59  if (!strcmp(histo, TGo4HistogramServer::fgcSHUTDOWNNAME)) {
60  // this is the last connect for shutdown, we do not acquire lockguard!
61  mbshisto = nullptr;
62  } else {
63  // normal case: lockguard to protect creation of histogram structure
64  TGo4LockGuard mainlock;
65  his = ana->GetHistogram(histo);
66  if (!his) {
67  TObject *ob = ana->GetObject(histo);
68  if (ob && ob->InheritsFrom(TH1::Class())) {
69  his = dynamic_cast<TH1 *>(ob);
70  } else if (ob && ob->InheritsFrom(TGraph::Class())) {
71  TGraph *gr = dynamic_cast<TGraph *>(ob);
72  his = gr->GetHistogram();
73  if (his) {
74  // this histogram has correct scales,
75  // but does not have graph points. we fill it:
76  Int_t maxpoints = gr->GetN();
77  for (Int_t point = 0; point < maxpoints; ++point) {
78  Double_t xg = 0;
79  Double_t yg = 0;
80  gr->GetPoint(point, xg, yg);
81  Int_t xbin = his->FindBin(xg);
82  his->SetBinContent(xbin, yg);
83  histofromgraph = kTRUE;
84  // note that this will fail if we have graph that
85  // is not injective (i.e. 1d-function). a histogram is not a graph!
86  }
87  }
88  }
89  }
90 
91  mbshisto = new TGo4MbsHist(his); // this will create structures
92  if (his && histofromgraph)
93  his->Reset();
94  } // if(!strcmp(histo,TGo4HistogramServer::fgcSHUTDOWNNAME))
95  const char *hisname;
96  if (his) {
97  hisname = his->GetName();
98  ana->Message(0, "Histogram server is sending histogram %s", hisname);
99  } else {
100  hisname = "No such histogram";
101  }
102  if (mbshisto) {
103  // the normal case: send created histogram structure
104  result = f_his_sendhis(mbshisto->GetHead(), mbshisto->GetHisNum(), (CHARS *)hisname,
105  (INTS4 *)mbshisto->GetBuffer());
106  delete mbshisto;
107  } else {
108  // only in case of server shutdown: send dummy to release socket wait
109  s_his_head header;
110  INTS4 buffer[128];
111  result = f_his_sendhis(&header, 1, (CHARS *)hisname, (INTS4 *)&buffer);
112  }
113  break;
114 
115  case COMM__GETDIR: {
116  TGo4LockGuard mainlock; // this should lock also dirmutex
117  mbshisto = new TGo4MbsHist(ana->GetObjectFolder(), histo);
118  result = f_his_senddir((s_his_head *)(mbshisto->GetBuffer()), mbshisto->GetHisNum());
119  // std::cout <<"sending dir buffer :" << std::endl;
120  // std::cout <<"\thisnum="<<mbshisto->GetHisNum()<<", buflen="<<mbshisto->GetBufLen() <<
121  // std::endl;
122  delete mbshisto;
123  } break;
124  default:
125  ana->Message(0, "Histogram server: got unknown command");
126  break;
127  } // switch
128  } else {
129  std::cout << "Histogram Server: Error on connect request: " << result << std::endl;
130  } // if(result==COMM__SUCCESS)
131 
132  return result;
133 }
INTS4 f_his_senddir(s_his_head *ps_head, INTS4 l_histos)
Definition: f_his_hist.c:563
#define COMM__SUCCESS
Definition: s_his_comm.h:27
INTS4 f_his_sendhis(s_his_head *ps_head, INTS4 l_histos, CHARS *pc_histo, INTS4 *pl_data)
Definition: f_his_hist.c:626
#define COMM__GETDIR
Definition: s_his_comm.h:20
#define COMM__GETHIST
Definition: s_his_comm.h:21
INTS4 f_his_wait(INTS4 *pl_action, CHARS *pc_histo)
Definition: f_his_hist.c:478
int INTS4
Definition: typedefs.h:28
TH1 * GetHistogram(const char *name)
TGo4HistogramServer * fxHistogramServer
static const char * fgcSHUTDOWNNAME
void Message(Int_t prio, const char *text,...)
s_his_head * GetHead()
Definition: TGo4MbsHist.h:47
TNamed * GetObject(const char *name, const char *folder=nullptr)
Int_t GetHisNum() const
Definition: TGo4MbsHist.h:50
char CHARS
Definition: typedefs.h:21
TGo4Analysis * GetAnalysis() const
Int_t * GetBuffer()
Definition: TGo4MbsHist.h:48
TFolder * GetObjectFolder()