FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairRunOnline.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 // ----- FairRunOnline source file -----
10 // ----- Created 06/01/04 by M. Al-Turany -----
11 // -------------------------------------------------------------------------
12 
13 #include "FairRunOnline.h"
14 
15 #include "FairBaseParSet.h"
16 #include "FairEventHeader.h"
17 #include "FairField.h"
18 #include "FairFileHeader.h"
19 #include "FairGeoParSet.h"
20 #include "FairLogger.h"
21 #include "FairParSet.h" // for FairParSet
22 #include "FairRootManager.h"
23 #include "FairRunIdGenerator.h"
24 #include "FairRuntimeDb.h"
25 #include "FairSource.h" // for FairSource, kONLINE
26 #include "FairTask.h"
27 
28 #include <TCollection.h> // for TIter
29 #include <TGeoManager.h>
30 #include <THttpServer.h>
31 #include <TList.h> // for TList
32 #include <TObjArray.h> // for TObjArray
33 #include <TObjString.h> // for TObjString
34 #include <TObject.h> // for TObject
35 #include <TROOT.h>
36 #include <TSystem.h>
37 #include <iostream>
38 #include <signal.h>
39 #include <stdlib.h>
40 
41 using std::cout;
42 using std::endl;
43 
45 
47 
49  : FairRun()
50  , fAutomaticFinish(kTRUE)
51  , fIsInitialized(kFALSE)
52  , fStatic(kFALSE)
53  , fField(0)
54  , fNevents(0)
55  , fServer(nullptr)
56  , fServerRefreshRate(0)
57 {
58  fgRinstance = this;
59  fAna = kTRUE;
60  LOG(info) << "FairRunOnline constructed at " << this;
61 }
62 
64  : FairRun()
65  , fAutomaticFinish(kTRUE)
66  , fIsInitialized(kFALSE)
67  , fStatic(kFALSE)
68  , fField(0)
69  , fNevents(0)
70  , fServer(nullptr)
71  , fServerRefreshRate(0)
72 {
73  fRootManager->SetSource(source);
74  fgRinstance = this;
75  fAna = kTRUE;
76  LOG(info) << "FairRunOnline constructed at " << this;
77 }
78 
80 {
81  // delete fFriendFileList;
82  delete fField;
83  if (gGeoManager) {
84  if (gROOT->GetVersionInt() >= 60602) {
85  gGeoManager->GetListOfVolumes()->Delete();
86  gGeoManager->GetListOfShapes()->Delete();
87  }
88  delete gGeoManager;
89  }
90  delete fServer;
91 }
92 
94 
95 void handler_ctrlc(int) { gIsInterrupted = kTRUE; }
96 
98 {
99  LOG(info) << "FairRunOnline::Init";
100  if (fIsInitialized) {
101  LOG(fatal) << "Error Init is already called before!";
102  exit(-1);
103  } else {
104  fIsInitialized = kTRUE;
105  }
106 
109 
110  // FairGeoLoader* loader = new FairGeoLoader("TGeo", "Geo Loader");
111  // FairGeoInterface* GeoInterFace = loader->getGeoInterface();
112  // GeoInterFace->SetNoOfSets(ListOfModules->GetEntries());
113  // GeoInterFace->setMediaFile(MatFname.Data());
114  // GeoInterFace->readMedia();
115 
116  // Add a Generated run ID to the FairRunTimeDb for input data which contain a FairMCEventHeader
117  // The call doesn't make sense for online sources which doesn't contain a FairMCEventHeader
118 
121  }
122 
123  GetEventHeader();
124 
126 
127  if (0 == fRunId) // Run ID was not set in run manager
128  {
129  if (0 == fEvtHeader->GetRunId()) // Run ID was not set in source
130  {
131  // Generate unique Run ID
132  FairRunIdGenerator genid;
133  fRunId = genid.generateId();
135  } else {
136  // Use Run ID from source
138  }
139  } else {
140  // Run ID was set in the run manager - propagate to source
142  }
143 
144  fRtdb->addRun(fRunId);
146  FairBaseParSet* par = dynamic_cast<FairBaseParSet*>(fRtdb->getContainer("FairBaseParSet"));
147  FairGeoParSet* geopar = dynamic_cast<FairGeoParSet*>(fRtdb->getContainer("FairGeoParSet"));
148  if (geopar) {
149  geopar->SetGeometry(gGeoManager);
150  }
151  if (fField) {
152  fField->Init();
154  }
155  TList* containerList = fRtdb->getListOfContainers();
156  TIter next(containerList);
157  FairParSet* cont;
158  TObjArray* ContList = new TObjArray();
159  while ((cont = dynamic_cast<FairParSet*>(next()))) {
160  ContList->Add(new TObjString(cont->GetName()));
161  }
162  if (par) {
163  par->SetContListStr(ContList);
164  par->setChanged();
165  par->setInputVersion(fRunId, 1);
166  }
167  if (geopar) {
168  geopar->setChanged();
169  geopar->setInputVersion(fRunId, 1);
170  }
171 
173 
175  fTask->SetParTask();
177 
178  // InitContainers();
179  // --- Get event header from Run
180  if (!fEvtHeader) {
181  LOG(fatal) << "FairRunOnline::InitContainers:No event header in run!";
182  return;
183  }
184  LOG(info) << "FairRunOnline::InitContainers: event header at " << fEvtHeader;
185  fRootManager->Register("EventHeader.", "Event", fEvtHeader, kTRUE);
187 
189 
190  // Now call the User initialize for Tasks
191  fTask->InitTask();
192 
193  // create the output tree after tasks initialisation
197 }
198 
200 {
201 
202  fRtdb = GetRuntimeDb();
203  FairBaseParSet* par = static_cast<FairBaseParSet*>(fRtdb->getContainer("FairBaseParSet"));
204  LOG(info) << "FairRunOnline::InitContainers: par = " << par;
205  if (nullptr == par)
206  LOG(warn) << "FairRunOnline::InitContainers: no 'FairBaseParSet' container !";
207 
208  if (par) {
209  fEvtHeader = static_cast<FairEventHeader*>(fRootManager->GetObject("EventHeader."));
210 
212 
213  // Copy the Event Header Info to Output
214  fEvtHeader->Register();
215 
216  // Init the containers in Tasks
218  fTask->ReInitTask();
219  // fTask->SetParTask();
221  // if (gGeoManager==0) {
222  // par->GetGeometry();
223  // }
224  } else {
225  // --- Get event header from Run
226  // fEvtHeader = dynamic_cast<FairEventHeader*> (FairRunOnline::Instance()->GetEventHeade
227  GetEventHeader();
228  if (!fEvtHeader) {
229  LOG(fatal) << "FairRunOnline::InitContainers:No event header in run!";
230  return;
231  }
232  LOG(info) << "FairRunOnline::InitContainers: event header at " << fEvtHeader;
233  fRootManager->Register("EventHeader.", "Event", fEvtHeader, kTRUE);
234  }
235 }
236 
237 Int_t FairRunOnline::EventLoop()
238 {
239  gSystem->IgnoreInterrupt();
240  signal(SIGINT, handler_ctrlc);
241 
243  Int_t tmpId = fEvtHeader->GetRunId();
244 
245  if (tmpId != -1 && tmpId != fRunId) {
246  LOG(info) << "FairRunOnline::EventLoop() Call Reinit due to changed RunID (from " << fRunId << " to " << tmpId;
247  fRunId = tmpId;
248  Reinit(fRunId);
250  fTask->ReInitTask();
251  }
252 
254  fTask->ExecuteTask("");
256  Fill();
258  fTask->FinishEvent();
259  fNevents += 1;
260  if (fServer && 0 == (fNevents % fServerRefreshRate)) {
261  fServer->ProcessRequests();
262  }
263 
264  if (gIsInterrupted) {
265  return 1;
266  }
267 
268  return 0;
269 }
270 
271 void FairRunOnline::Run(Int_t Ev_start, Int_t Ev_end)
272 {
273  fNevents = 0;
274 
275  gIsInterrupted = kFALSE;
276 
277  Int_t MaxAllowed = fRootManager->CheckMaxEventNo(Ev_end);
278  if (MaxAllowed != -1) {
279  if (Ev_end == 0) {
280  if (Ev_start == 0) {
281  Ev_end = MaxAllowed;
282  } else {
283  Ev_end = Ev_start;
284  if (Ev_end > MaxAllowed) {
285  Ev_end = MaxAllowed;
286  }
287  Ev_start = 0;
288  }
289  } else {
290  if (Ev_end > MaxAllowed) {
291  cout << "-------------------Warning---------------------------" << endl;
292  cout << " -W FairRunAna : File has less events than requested!!" << endl;
293  cout << " File contains : " << MaxAllowed << " Events" << endl;
294  cout << " Requested number of events = " << Ev_end << " Events" << endl;
295  cout << " The number of events is set to " << MaxAllowed << " Events" << endl;
296  cout << "-----------------------------------------------------" << endl;
297  Ev_end = MaxAllowed;
298  }
299  }
300  }
301  Int_t status;
302  if (Ev_start < 0) {
303  while (kTRUE) {
304  status = fRootManager->ReadEvent();
305  if (0 == status) {
306  status = EventLoop();
307  }
308  if (1 == status) {
309  break;
310  } else if (2 == status) {
311  continue;
312  }
313  if (gIsInterrupted) {
314  break;
315  }
316  }
317  } else {
318  for (Int_t i = Ev_start; i < Ev_end; i++) {
319  status = fRootManager->ReadEvent(i);
320  if (0 == status) {
321  status = EventLoop();
322  }
323  if (1 == status) {
324  break;
325  } else if (2 == status) {
326  i -= 1;
327  continue;
328  }
329 
330  if (gIsInterrupted) {
331  break;
332  }
333  }
334  }
335 
337  if (fAutomaticFinish) {
338  Finish();
339  }
340 }
341 
343 {
344  fTask->FinishTask();
346  fRootManager->Write();
348 
350 }
351 
352 void FairRunOnline::ActivateHttpServer(Int_t refreshRate, Int_t httpServer)
353 {
354  TString serverAddress = "http:";
355  serverAddress += httpServer;
356  fServer = new THttpServer(serverAddress);
357  fServerRefreshRate = refreshRate;
358 }
359 
360 void FairRunOnline::RegisterHttpCommand(TString name, TString command)
361 {
362  if (fServer) {
363  TString path = "/Objects/HISTO";
364  fServer->RegisterCommand(name, path + command);
365  }
366 }
367 
368 void FairRunOnline::Reinit(UInt_t runId)
369 {
370  // reinit procedure
371  fRtdb->initContainers(runId);
372 }
373 
375 {
376  fStatic = tempBool;
377  if (fStatic) {
378  LOG(info) << "Parameter Cont. initialisation is static";
379  } else {
380  LOG(info) << "Parameter Cont. initialisation is NOT static";
381  }
382 }
383 
384 void FairRunOnline::AddObject(TObject* object)
385 {
386  if (nullptr == object) {
387  return;
388  }
389  if (fServer) {
390  TString classname = TString(object->ClassName());
391  if (classname.EqualTo("TCanvas")) {
392  fServer->Register("CANVAS", object);
393  } else if (classname.EqualTo("TFolder")) {
394  fServer->Register("/", object);
395  } else if (classname.Contains("TH1") || classname.Contains("TH2")) {
396  fServer->Register("HISTO", object);
397  } else {
398  LOG(warn) << "FairRunOnline::AddObject : unrecognized object type : " << classname;
399  }
400  }
401 }
402 
404 {
405  if (fMarkFill) {
406  fRootManager->Fill();
407  } else {
408  fMarkFill = kTRUE;
409  }
410 }
411 
virtual void SetParUnpackers()=0
void ActivateHttpServer(Int_t refreshRate=10000, Int_t httpPort=8080)
void handler_ctrlc(int)
Int_t fServerRefreshRate
virtual const char * GetName() const
Definition: FairParSet.h:38
Int_t CheckMaxEventNo(Int_t EvtEnd=0)
Bool_t fAna
Definition: FairRun.h:203
virtual void ExecuteTask(Option_t *option="0")
Definition: FairTask.cxx:118
virtual Source_Type GetSourceType()=0
Int_t ReadEvent(Int_t i=0)
unsigned int generateId(void)
void ReInitTask()
Definition: FairTask.cxx:58
FairRootManager * fRootManager
Definition: FairRun.h:193
void SetRunId(Int_t runId)
Definition: FairSource.h:55
void setChanged(Bool_t flag=kTRUE)
Definition: FairParSet.h:72
void SetRunId(UInt_t runid)
FairTask * fTask
Definition: FairRun.h:191
void WriteFileHeader(FairFileHeader *f)
virtual void FillParContainer()
Definition: FairField.h:107
FairEventHeader * GetEventHeader()
Definition: FairRun.cxx:112
void SetContainerStatic(Bool_t tempBool=kTRUE)
FairEventHeader * fEvtHeader
Definition: FairRun.h:205
ClassImp(FairEventBuilder)
TObject * GetObject(const char *BrName)
virtual Bool_t InitUnpackers()=0
void SetContListStr(TObjArray *list)
TList * getListOfContainers()
FairParSet * getContainer(const Text_t *)
Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Bool_t initContainers(Int_t runId, Int_t refId=-1, const Text_t *fileName="")
void AddObject(TObject *object)
void Reinit(UInt_t runId)
FairSource * GetSource()
virtual void Close()=0
void StoreAllWriteoutBufferData()
virtual void Register(Bool_t Persistance=kTRUE)
FairMQExParamsParOne * par
FairField * fField
Bool_t gIsInterrupted
FairFileHeader * fFileHeader
Definition: FairRun.h:207
virtual void FinishTask()
Definition: FairTask.cxx:90
Double_t GetEventTime()
virtual void Fill()
Bool_t fIsInitialized
Definition: FairRunOnline.h:99
void setInputVersion(Int_t v=-1, Int_t i=0)
Definition: FairParSet.h:51
THttpServer * fServer
void InitTask()
Definition: FairTask.cxx:41
virtual Bool_t ReInitUnpackers()=0
void Run(Int_t Ev_start, Int_t Ev_end)
FairRuntimeDb * GetRuntimeDb(void)
Definition: FairRun.h:80
Bool_t fMarkFill
Definition: FairRun.h:213
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
static FairRunOnline * fgRinstance
FairRtdbRun * addRun(Int_t runId, Int_t refId=-1)
void RegisterHttpCommand(TString name, TString command)
void SetSource(FairSource *tempSource)
static FairRunOnline * Instance()
UInt_t fRunId
Definition: FairRun.h:201
virtual void FinishEvent()
Definition: FairTask.cxx:82
FairRuntimeDb * fRtdb
Definition: FairRun.h:189
virtual ~FairRunOnline()
void SetRunId(UInt_t runid)
void DeleteOldWriteoutBufferData()
void StoreWriteoutBufferData(Double_t eventTime)
void SetGeometry(TGeoManager *Geom)
Definition: FairGeoParSet.h:63
virtual void Init()
Definition: FairField.h:53
void SetParTask()
Definition: FairTask.cxx:73
void FillEventHeader(FairEventHeader *feh)