Check if busy before creating thread
[u/mrichter/AliRoot.git] / EVE / EveHLT / AliEveEventBufferHomer.cxx
1 #if __GNUC__>= 3
2    using namespace std;
3 #endif
4
5 #include "AliEveEventBufferHomer.h"
6 #include "AliEveHOMERManager.h"
7
8 #include "TList.h"
9 #include "TFile.h"
10
11 #include <iostream>
12
13 ClassImp(AliEveEventBufferHomer);
14
15 ///_______________________________________________________________________
16 AliEveEventBufferHomer::AliEveEventBufferHomer() :
17   fHomer(NULL),
18   fEventNo(0),
19   fAsyncList(NULL)
20 {
21   // see header file for class documentation
22   fHomer = new AliEveHOMERManager();
23   Initialize();
24
25 }
26
27
28 ///____________________________________________________________________
29 AliEveEventBufferHomer::~AliEveEventBufferHomer() {
30   
31   if(fHomer)
32     delete fHomer;
33   fHomer = NULL;
34
35 }
36
37
38
39 ULong64_t AliEveEventBufferHomer::GetEventIdFromSource() {
40   ///
41   return fHomer->GetEventID();
42 }
43
44 ///______________________________________________________________________
45 TObject * AliEveEventBufferHomer::GetEventFromSource() {
46   //see header file for documentation
47   if(fHomer) {
48     cout << "calling nexthomerevent"<<endl;
49     TList * blockList = fHomer->NextHOMEREvent();
50     cout  << "getting async block list"<<endl;
51     TList * aList = fHomer->GetAsyncBlockList();
52     fAsyncList = aList;
53     if(blockList)  return dynamic_cast<TObject*>(blockList);
54     else return NULL;
55   } 
56
57   cout << "AliEveEventBufferHomer::GetEventFromSource() : fHomer is null pointer!!"<<endl;
58   return NULL;
59 }
60
61 ///___________________________________________________________________________
62 void AliEveEventBufferHomer::AddToBuffer(TObject * event) {
63   cout << "Add to buffer"<<endl;
64   if(!event) return;
65   fBIndex[kTop] = CalculateNext(fBIndex[kTop]);
66   TList * list = dynamic_cast<TList*>(fEventBuffer->At(fBIndex[kTop]));
67   if(list) {
68     list->Clear();
69     list->AddAll(dynamic_cast<TList*>(event));
70   }
71 }
72
73
74
75 ///______________________________________________________________________
76 void AliEveEventBufferHomer::Initialize() {
77   //Add TList pointers to the buffer array
78
79   //Create TLists in all of buffer
80   for(Int_t i = 0; i < fBufferSize; i++) {
81     TList * list = new TList();
82     list->SetOwner(kTRUE);
83     fEventBuffer->AddAt(list, i);
84   }
85
86   //Create the async list
87   fAsyncList = new TList();
88   fAsyncList->SetOwner(kTRUE);
89
90   Int_t iResult = fHomer->Initialize();
91   if(iResult) {
92     cout << "Error Initializing HOMER!!!"<<endl;
93     return;
94   }
95
96
97   fHomer->SetRetryCount(1,15);
98   fHomer->SetBlockOwner(kFALSE);
99   fHomer->StartEveSourceListLoop();
100 }
101
102
103 ///______________________________________________________________________
104 void AliEveEventBufferHomer::ConnectToSource () {
105
106   fHomer->SetRetryCount(200, 15);
107   fHomer->ReConnectHOMER();
108 }
109
110 ///_____________________________________________________________________
111 void AliEveEventBufferHomer::WriteToFile(Int_t runnumber){
112
113   TFile * file = TFile::Open(Form("%d_0x%016X_ESD.root", runnumber, GetEventId()), "RECREATE"); 
114   fEventBuffer->At(fBIndex[kCurrent])->Write("blockList", TObject::kSingleKey);
115   file->Close();
116   
117   if(fAsyncList) {
118     TFile * afile = TFile::Open(Form("%d_0x%016X_Async.root",  runnumber, GetEventId()), "RECREATE"); 
119     fAsyncList->Write("blockList", TObject::kSingleKey);
120     afile->Close();
121   }
122 }