]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MONITOR/alieventserver/AliEventServerReconstruction.cxx
e2d09ec94b36a0311d3566f9bb6db19a0d56327a
[u/mrichter/AliRoot.git] / MONITOR / alieventserver / AliEventServerReconstruction.cxx
1 // Author: Mihai Niculescu 2013
2
3 /**************************************************************************
4  * Copyright(c) 1998-2013, ALICE Experiment at CERN, all rights reserved. *
5  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
6  * full copyright notice.                                                 *
7  **************************************************************************/
8  
9 #include <TEnv.h>
10 #include <TSystem.h>
11 #include <TThread.h>
12 #include <TXMLEngine.h>
13
14 #include <AliLog.h>
15 #include <AliESDEvent.h>
16 #include <AliCDBManager.h>
17 #include <AliGRPPreprocessor.h>
18 #include <AliReconstruction.h>
19 #include <AliTPCRecoParam.h>
20
21 #include <iostream>
22 #include <sstream>
23
24 #include "AliEventServerUtil.h"
25 #include "AliEventServerReconstruction.h"
26 #include "AliStorageEventManager.h"
27 #include "AliStorageTypes.h"
28
29 #include "AliESDEvent.h"
30 #include "AliESDtrack.h"
31 #include "AliTrackPointArray.h"
32 #include "AliESDfriendTrack.h"
33 #include "AliExternalTrackParam.h"
34 #include "AliTrackerBase.h"
35 #include "AliTracker.h"
36
37 using namespace std;
38
39 AliEventServerReconstruction::AliEventServerReconstruction()
40         : TQObject(),
41           fAliReco(0),
42           fCDBmanager(0),
43           fCurrentRunId(0),
44           fIsListenning(kFALSE),
45           fSettings(0),
46           fHost(0),
47           fRecoThread(0),
48           fRecoIsRunning(false),
49           fRecoWasInitialized(false)
50 {}
51
52 AliEventServerReconstruction::~AliEventServerReconstruction()
53 {
54         Close();
55         //if(fSettings!=0){/*delete fSettings;*/fSettings=0;}
56         if(fAliReco){delete fAliReco;fAliReco=0;}
57 }
58
59 void AliEventServerReconstruction::Close()
60 {
61         if(fIsListenning)
62         {
63                 Info("AliRecoServer::Close", "Closing Server");
64                 StopReconstruction();
65                 fIsListenning = kFALSE;
66         }
67 }
68
69 Bool_t AliEventServerReconstruction::StartReconstruction(Int_t run, const char* input)
70 {
71   cout<<"Start of run:"<<run<<endl;
72         fCurrentRunId = run;
73
74         // re-read settings
75         //      if(fSettings){delete fSettings;fSettings=0;}
76         //fSettings = new TEnv(AliEventServerUtil::GetPathToServerConf());
77         fSettings.ReadFile(AliEventServerUtil::GetPathToServerConf(),kEnvUser);
78
79
80         TString recoBaseDir = fSettings.GetValue("server.saveRecoDir",DEFAULT_SERVER_SAVE_RECO_DIR);
81         
82         // Create directories and logfile
83         TString logFile = Form("%s/log/run%d.log",recoBaseDir.Data(),run);
84         Info("DoStart","Reconstruction log will be written to %s",logFile.Data());
85         if( gSystem->RedirectOutput(logFile.Data())!=0)
86         {
87                 printf(Form("AliRecoServer::StartReconstruction [] Error while trying to redirect output to [%s]. Exiting...", logFile.Data()) );
88                 return kFALSE;
89         }
90         gSystem->cd(recoBaseDir.Data());
91
92
93         TString gdcs;
94         if (RetrieveGRP(run,gdcs) <= 0 || gdcs.IsNull()){return kFALSE;}
95           
96         gSystem->mkdir(Form("run%d", run));
97         gSystem->cd(Form("run%d", run));
98
99         // Create Reco and Reco Thread
100         cout<<"Setup reco will be called"<<endl;
101         SetupReco(input);
102         
103         fHost = (const char*)Form("%s:%d", fSettings.GetValue("server.host", DEFAULT_SERVER_HOST), fSettings.GetValue("server.port", DEFAULT_SERVER_PORT));
104         
105         cout<<"Creating new thread"<<endl;
106         fRecoThread = new TThread("AliEventServerReconstruction",Dispatch, (void*)this);
107         fRecoThread->Run();
108         fIsListenning = kTRUE;
109         fRecoIsRunning=true;
110         cout<<"Reco started"<<endl;
111         return true;
112 }
113
114 bool AliEventServerReconstruction::StopReconstruction()
115 {
116   cout<<"Reco server -- StopPeconstruction() called"<<endl;
117   if(!fRecoIsRunning || !fRecoThread)
118     {
119       cout<<"Reco is not running. No need to stop it."<<endl;
120       return true;
121     }
122   if(!fRecoWasInitialized)
123     {
124       cout<<"Reco is under initialization. Wait until it's finished"<<endl;
125       
126       return false;
127     }
128   cout<<"killing thread"<<endl;
129   fRecoThread->Kill();
130   cout<<"thread killed"<<endl;
131   delete fRecoThread;
132   fRecoThread=0;
133   cout<<"Reco server -- thread removed"<<endl;
134   // Emit("Stopped()");
135
136   cout<<"Reco server -- terminating reconstruction"<<endl;      
137   fAliReco->SlaveTerminate();
138   if (fAliReco->GetAbort() != TSelector::kContinue) return false;
139   fAliReco->Terminate();
140   if (fAliReco->GetAbort() != TSelector::kContinue) return false; 
141         
142   if(fAliReco){delete fAliReco;fAliReco=0;}
143   cout<<"Reco server -- deleting CDBManager"<<endl;
144   if(fCDBmanager){fCDBmanager->Destroy();fCDBmanager=0;}
145   cout<<"Reco server -- recontruction stopped"<<endl;
146   fRecoIsRunning=false;
147   fRecoWasInitialized=false;
148   return true;
149 }
150
151 void AliEventServerReconstruction::ReconstructionHandle()
152 {
153         TThread::SetCancelAsynchronous();
154         TThread::SetCancelOn();
155         
156         if(!fAliReco) return;
157
158         AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
159         eventManager->CreateSocket(EVENTS_SERVER_PUB);
160         eventManager->CreateSocket(XML_PUB);
161         cout<<"Sockets created"<<endl;
162         fAliReco->Begin(NULL);
163         cout<<"Reco began"<<endl;
164         if (fAliReco->GetAbort() != TSelector::kContinue) return;
165         fAliReco->SlaveBegin(NULL);
166         cout<<"Slave began"<<endl;
167         if (fAliReco->GetAbort() != TSelector::kContinue) return;
168
169         fRecoWasInitialized=true;
170         //******* The loop over events
171         Int_t iEvent = 0;
172         AliESDEvent* event;
173         while (fAliReco->HasNextEventAfter(iEvent))
174         {
175                 // check if process has enough resources 
176           cout<<"Event server -- checking resources"<<endl;
177                 if (!fAliReco->HasEnoughResources(iEvent)) break;
178                 cout<<"Event server -- resources checked"<<endl;
179                 Bool_t status = fAliReco->ProcessEvent(iEvent);
180                 cout<<"Event server -- event processed"<<endl;
181       
182                 if (status)
183                 {
184                         event = fAliReco->GetESDEvent();
185                         cout<<"Event server -- sending event"<<endl;
186                         eventManager->Send(event,EVENTS_SERVER_PUB);
187                         cout<<"Event server -- sending event as xml"<<endl;
188                         eventManager->SendAsXml(event,XML_PUB);
189                         cout<<"Event server -- xml sent"<<endl;
190                 }
191                 else
192                 {
193                   cout<<"Event server -- aborting"<<endl;
194                         fAliReco->Abort("ProcessEvent",TSelector::kAbortFile);
195                 }
196                 cout<<"Event server -- cleaning event"<<endl;
197                 fAliReco->CleanProcessedEvent();
198                 cout<<"Event server -- event cleaned"<<endl;
199                 iEvent++;
200         }
201         StopReconstruction();
202 }
203
204 Int_t AliEventServerReconstruction::RetrieveGRP(UInt_t run, TString &gdc)
205 {
206   //if(fSettings==0) return (-1);
207
208         // Retrieve GRP entry for given run from aldaqdb.
209         TString dbHost = fSettings.GetValue("logbook.host", DEFAULT_LOGBOOK_HOST);
210         Int_t dbPort =  fSettings.GetValue("logbook.port", DEFAULT_LOGBOOK_PORT);
211         TString dbName =  fSettings.GetValue("logbook.db", DEFAULT_LOGBOOK_DB);
212         TString user =  fSettings.GetValue("logbook.user", DEFAULT_LOGBOOK_USER);
213         TString password = fSettings.GetValue("logbook.pass", DEFAULT_LOGBOOK_PASS);
214         
215         Int_t ret=AliGRPPreprocessor::ReceivePromptRecoParameters(run, dbHost.Data(),
216                                                                   dbPort, dbName.Data(),
217                                                                   user.Data(), password.Data(),
218                                                                   Form("local://%s",gSystem->pwd()),
219                                                                   gdc);
220
221         if(ret>0) Info("RetrieveGRP","Last run of the same type is: %d",ret);
222         else if(ret==0) Warning("RetrieveGRP","No previous run of the same type found");
223         else if(ret<0) Error("Retrieve","Error code while retrieving GRP parameters returned: %d",ret);
224         return(ret);
225 }
226
227 void AliEventServerReconstruction::SetupReco(const char* input)
228 {
229   //if(fSettings==0) return;
230
231         //AliTPCRecoParam::SetUseTimeCalibration(kFALSE); //-- !probably should be set from conf file!
232
233         printf(Form("=========================[local://%s/..]===========\n",gSystem->pwd()));
234
235         /* Settings CDB */
236         fCDBmanager = AliCDBManager::Instance();
237   
238         fCDBmanager->SetDefaultStorage(fSettings.GetValue("cdb.defaultStorage", DEFAULT_CDB_STORAGE));
239         fCDBmanager->SetSpecificStorage(fSettings.GetValue( "cdb.specificStoragePath1", DEFAULT_CDB_SPEC_STORAGE_PATH1),  
240                                     fSettings.GetValue( "cdb.specificStorageValue1", DEFAULT_CDB_SPEC_STORAGE_VALUE1));
241
242         fCDBmanager->SetSpecificStorage(fSettings.GetValue( "cdb.specificStoragePath2", DEFAULT_CDB_SPEC_STORAGE_PATH2),  
243                                     fSettings.GetValue( "cdb.specificStorageValue2", DEFAULT_CDB_SPEC_STORAGE_VALUE2));
244   
245         fCDBmanager->SetSpecificStorage(fSettings.GetValue( "cdb.specificStoragePath3", DEFAULT_CDB_SPEC_STORAGE_PATH3),  
246                                     fSettings.GetValue( "cdb.specificStorageValue3", DEFAULT_CDB_SPEC_STORAGE_VALUE3));
247   
248         /* Reconstruction settings */  
249         if(!fAliReco)fAliReco = new AliReconstruction();
250
251         // QA options
252         //rec->SetRunQA(fSettings->GetValue( "qa.runDetectors", DEFAULT_QA_RUN));
253         //rec->SetRunGlobalQA(fSettings->GetValue( "qa.runGlobal", DEFAULT_QA_RUN_GLOBAL));
254         fAliReco->SetQARefDefaultStorage(fSettings.GetValue( "qa.defaultStorage",DEFAULT_QAREF_STORAGE)) ;
255         //rec->SetRunPlaneEff(fSettings->GetValue( "reco.runPlaneEff", DEFAULT_RECO_RUN_PLANE_EFF));
256
257         fAliReco->SetRunQA(":");
258         fAliReco->SetRunGlobalQA(false);
259         fAliReco->SetRunPlaneEff(false);
260
261         // AliReconstruction settings
262         fAliReco->SetWriteESDfriend(fSettings.GetValue( "reco.writeESDfriend", DEFAULT_RECO_WRITE_ESDF));
263         fAliReco->SetWriteAlignmentData(fSettings.GetValue( "reco.writeAlignment",DEFAULT_RECO_WRITE_ALIGN));
264         fAliReco->SetInput(input); // reconstruct data from this input
265         fAliReco->SetRunReconstruction(fSettings.GetValue( "reco.detectors", DEFAULT_RECO_DETECTORS));
266         fAliReco->SetUseTrackingErrorsForAlignment("ITS"); //-- !should be set from conf file!
267
268         // switch off cleanESD
269         fAliReco->SetCleanESD(fSettings.GetValue( "reco.cleanESD",DEFAULT_RECO_CLEAN_ESD));
270
271         // init reco for given run
272         fAliReco->InitRun(input);
273 }