GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
MainGo4EventServerExample.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 
29 #include <iostream>
30 
31 #include "TApplication.h"
32 #include "TH1.h"
33 #include "TCanvas.h"
34 #include "TBenchmark.h"
35 
36 #include "TGo4Log.h"
37 #include "Go4EventServer.h"
38 #include "TGo4SimpleEvent.h"
39 #include "TGo4SimpleSubEvent.h"
41 
42 int main(int argc, char **argv)
43 {
44  if(argc<2) {
45  std::cout << "usage: MainGo4EventServerExample eventnumber"<<std::endl;
46  return 0;
47  }
48 
49  TApplication theApp("App", &argc, argv);
50 
51  // the following statements control go4 debug output:
52  TGo4Log::Instance(); // init logger object
53  TGo4Log::SetIgnoreLevel(0); // set this to 1 to suppress detailed debug output
54  // set this to 2 to get warnings and errors only
55  // set this to 3 to get errors only
56  TGo4Log::LogfileEnable(kFALSE); // will enable or disable logging all messages to file
57 
58  TBenchmark mybench;
59  const char *num = argv[1];
60  Int_t maxevents = atoi(num);
61  Int_t value = 0;
62 
63  TCanvas *can = new TCanvas("MyCanvas","MainGo4EventServer");
64  can->Divide(1,2);
65  TH1D* histo1 = new TH1D ("histogram 1", "mbsevent data",400,0,4000);
66  TH1D* histo2 = new TH1D ("histogram 2", "simpleevent data",400,0,4000);
67 
68  TGo4MbsSubEvent *sub = nullptr;
69  TGo4SimpleSubEvent* simpsub = nullptr;
70 
71  // create the event structure objects:
72  // raw mbs event
73  Short_t idfield[2] = {0,3}; // this array defines the procids for the subevents
74  // note: if other procids are found during read,
75  // subevents of theses procids will be added dynamically
76  TGo4MbsEvent *event = new TGo4MbsEvent(2, idfield, 1);
77  // create initial mbs event with 2 subevents
78  // having the procids giving in the idfield (0 and 3)
79  // initial size for each subevent data field is 1 longword
80  // initial size is reallocated dynamically
81  TGo4SimpleEvent* simpevent = new TGo4SimpleEvent(2);
82  // user event with 2 subevents
84  // the "unpack processor" converting mbs events to user events
85  TGo4EventSource *input = nullptr;
86  // TGo4EventSource *simpinput = nullptr;
87  TGo4EventStore *output = nullptr;
88  TGo4EventStore *simpoutput = nullptr;
89 
90  // Create instance of TGo4EventSource which specifies
91  // the input. Please change subclass of TGo4EventSource
92  // to change input type.
94  TString testfilename = TString::Format("%s/data/test.lmd",getenv("GO4SYS")); // this file is part of go4 distribution
95  input = new TGo4MbsFile(testfilename.Data());
96 // input= new TGo4MbsFile("dat0.lmd");
97  // for listmode file with given path and name
98  // file will be opened in constructor and closed in destructor
100  // input= new TGo4MbsTransport("r2f-2");
101  // connect to mbs transport channel, with given hostname
102  // constructor connects to transport, dtor disconnects
104 // input= new TGo4MbsStream("r2f-2");
105  // connect to mbs stream channel, with given hostname
106  // constructor connects to stream, dtor disconnects
108 // input= new TGo4MbsEventServer("r2f-2");
109  // connect to mbs event server channel, with given hostname
110  // constructor connects to event server, dtor disconnects
112 // input= new TGo4RevServ("r2f-2");
113  // connect to mbs remote event server, with given hostname
114  // constructor connects to revserv, dtor disconnects
115 
116  // optional: output of the mbs raw event classes into ROOT file
117  output = new TGo4FileStore("MbsEvents",99,5);
118 
119  // optional: input of the mbs raw events from ROOT file
120  //simpinput= new TGo4FileSource("MbsEvents");
121 
122  // output of user events into ROOT file
123  simpoutput = new TGo4FileStore("SimpleEvents",99,5);
124 
126  event->PrintEvent();
127  simpevent->PrintEvent();
128 
130  event->SetEventSource(input); // tell raw event to be filled from mbs source
131 // event->SetEventSource(simpinput); // tell raw event to be read from root file
132 
133  proc->SetInputEvent(event); // tell "unpack processor" to take mbs event as input
134  simpevent->SetEventSource(proc); // tell user event to be filled by unpack processor
135 
136 
137  std::cout << "starting event loop:"<< std::endl;
138  mybench.Start("Go4EventServer");
139  for(Int_t t = 0; t < maxevents; ++t)
140  {
142  event->Clear();
143  Int_t errmess = event->Fill(); // fills raw event from mbs source
144  if(errmess==GETEVT__NOMORE)
145  {
146  std::cout << "no more events from MbsSource."<< std::endl;
147  break;
148  //gApplication->Terminate();
149  }
150  else if(errmess != 0)
151  {
152  std::cout << "ERROR on event fill."<< std::endl;
153  break;
154  }
156  sub=event->GetSubEvent(3); // subevent by procid
157  if (sub) {
158  value = sub->Data(1); // access data array at 1
159  if (value) {
160  histo1->Fill(value);
161  }
162  }
163 
164  if(output)
165  output->Store(event); // write mbs raw event to ROOT file
166 
168  simpevent->Clear();
169  simpevent->Fill(); // this will invoke unpack processor methods
171  simpsub=simpevent->GetSubEvent(3);
172  if (simpsub)
173  {
174  value= simpsub->fiD1; // userevents have data as members,
175  // may also be seen in ROOT TreeBrowser
176  if(value) {
177  histo2->Fill(value);
178  }
179  }
180 
181  if(simpoutput)
182  simpoutput->Store(simpevent); // write user event to ROOT file
183 
184 
185  if(t%2000 == 0 && t != 0) {
186  event->PrintEvent();
187  can->cd(1);
188  histo1->Draw();
189  can->cd(2);
190  histo2->Draw();
191  can->Modified();
192  can->Update();
193  std::cout << " 2000 events processed "<< std::endl;
194  }
195 
196  } // for(.....)
197  std::cout << "\t finished filling and saving events."<<std::endl;
198  mybench.Show("Go4EventServer");
199  delete output;
200  delete event;
201  delete input;
202  delete simpoutput;
203  delete simpevent;
204  delete proc;
205  //gApplication->Terminate();
206  theApp.Run();
207  return 0;
208 }
static TGo4Log * Instance()
Definition: TGo4Log.cxx:85
virtual Int_t Store(TGo4EventElement *event)=0
Int_t Fill() override
Int_t Data(Int_t i) const
static void SetIgnoreLevel(Int_t level)
Definition: TGo4Log.cxx:332
#define GETEVT__NOMORE
Definition: f_evt.h:134
TGo4SimpleSubEvent * GetSubEvent(Short_t procid)
void SetInputEvent(TGo4EventElement *raw)
void PrintEvent() override
int main(int argc, char **argv)
void SetEventSource(TGo4EventSource *src)
static void LogfileEnable(Bool_t on=kTRUE)
Definition: TGo4Log.cxx:363
void Clear(Option_t *opt="") override