Fixing bugs in Event Display
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageServerThread.cxx
CommitLineData
5eb34a26 1#include "AliStorageServerThread.h"
2#include "AliStorageTypes.h"
3#include "AliESDEvent.h"
4
5eb34a26 5#include <iostream>
6#include <fstream>
7
8#include <TFile.h>
9#include <TThread.h>
10
11using namespace std;
5eb34a26 12
13AliStorageServerThread::AliStorageServerThread() :
5eb34a26 14 fDatabase(0),
15 fStoragePath("")
16{
17 TThread::Lock();
18 fDatabase = new AliStorageDatabase();
19 //load parameters from config file
164d3d29 20 ifstream configFile (GetConfigFilePath());
5eb34a26 21
22
23 if (configFile.is_open())
24 {
25 string line;
26 int from,to;
27 while(configFile.good())
28 {
29 getline(configFile,line);
30 from = line.find("\"")+1;
31 to = line.find_last_of("\"");
32 if(line.find("STORAGE_PATH=")==0)
33 {
34 fStoragePath=line.substr(from,to-from);
35 }
36 }
37 if(configFile.eof())
38 {
39 configFile.clear();
40 }
41 configFile.close();
42 }
43 else
44 {
45 cout<<"SERVER -- Unable to open config file"<<endl;
46 }
47 TThread::UnLock();
48
49 //start communication on socket
5eb34a26 50 StartCommunication();
51}
52
53AliStorageServerThread::~AliStorageServerThread()
54{
164d3d29 55 cout<<"SERVER -- AliStorageServerThread destructor called";
a410aca4 56 if (fDatabase) {delete fDatabase;}
5eb34a26 57 cout<<" --- OK"<<endl;
58}
59
60void AliStorageServerThread::StartCommunication()
61{
164d3d29 62 AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
63 storageSockets socket = SERVER_COMMUNICATION_REP;
64 eventManager->CreateSocket(socket);
65
66 struct serverRequestStruct *request;
5eb34a26 67
68 while(1)
69 {
164d3d29 70 request = eventManager->GetServerStruct(socket);
5eb34a26 71
164d3d29 72 switch(request->messageType)
a410aca4 73 {
74 case REQUEST_LIST_EVENTS:
75 {
76 vector<serverListStruct> result = fDatabase->GetList(request->list);
77 eventManager->Send(result,socket);
78 break;
79 }
80 case REQUEST_GET_EVENT:
81 {
82 AliESDEvent *event = fDatabase->GetEvent(request->event);
83 eventManager->Send(event,socket);
84 delete event;
85 break;
86 }
87 case REQUEST_GET_NEXT_EVENT:
88 {
fae81379 89 cout<<"NEXT EVENT request received"<<endl;
a410aca4 90 AliESDEvent *event = fDatabase->GetNextEvent(request->event);
91 eventManager->Send(event,socket);
92 delete event;
93 break;
94 }
95 case REQUEST_GET_PREV_EVENT:
96 {
97 AliESDEvent *event = fDatabase->GetPrevEvent(request->event);
98 eventManager->Send(event,socket);
99 delete event;
100 break;
101 }
102 case REQUEST_GET_LAST_EVENT:
103 {
104 AliESDEvent *event = fDatabase->GetLastEvent();
105 eventManager->Send(event,socket);
106 delete event;
107 break;
108 }
109 case REQUEST_GET_FIRST_EVENT:
110 {
111 AliESDEvent *event = fDatabase->GetFirstEvent();
112 eventManager->Send(event,socket);
113 delete event;
114 break;
115 }
116 case REQUEST_MARK_EVENT:
117 {
118 struct eventStruct *markData = &(request->event);
119 eventManager->Send(MarkEvent(*markData),socket);
120 break;
121 }
122 default:break;
123 }
124
5eb34a26 125 }
126}
127
128bool AliStorageServerThread::MarkEvent(struct eventStruct event)
129{
a410aca4 130 string pathToFile = fDatabase->GetFilePath(event);
131 TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
132 if(!tmpFile)
133 {
134 cout<<"SERVER -- couldn't open temp file"<<endl;
135 return false;
136 }
6ce16778 137
138 tmpFile->cd(Form("run%d",event.runNumber));
139
140 AliESDEvent *eventToMark = (AliESDEvent*)gDirectory->Get(Form("event%d",event.eventNumber));
a410aca4 141 if(!eventToMark)
142 {
143 cout<<"SERVER -- couldn't find such event"<<endl;
144 if(tmpFile){delete tmpFile;}
145 return false;
146 }
147 cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
5eb34a26 148
6ce16778 149 TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
5eb34a26 150
6ce16778 151 if(!permFile)
152 {
153 cout<<"SERVER -- Couldn't open perm file"<<endl;
154 if(tmpFile){delete tmpFile;}
155 if(eventToMark){delete eventToMark;}
156 return false;
157 }
5eb34a26 158
6ce16778 159 //create new directory for this run
160 TDirectory *currentRun;
161 if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
162 {
163 cout<<"SERVER -- creating new directory for this run"<<endl;
164 currentRun->cd();
165 }
166 else
167 {
168 cout<<"SERVER -- opening existing directory for this run"<<endl;
169 permFile->cd(Form("run%d",event.runNumber));
170 }
5eb34a26 171
6ce16778 172 //try to add record to the database
173 if(!fDatabase->MarkEvent(event))
174 {
175 cout<<"SERVER -- could not mark event in the database"<<endl;
176 if(tmpFile){delete tmpFile;}
177 if(eventToMark){delete eventToMark;}
178 if(permFile){delete permFile;}
179 return false;
180 }
5eb34a26 181
6ce16778 182 eventToMark->Write(Form("event%d",event.eventNumber));
183 permFile->Close();
184 tmpFile->Close();
5eb34a26 185
6ce16778 186 if(tmpFile){delete tmpFile;}
187 if(eventToMark){delete eventToMark;}
188 if(permFile){delete permFile;}
189 // if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
190 return true;
5eb34a26 191}
192