]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MONITOR/alieventserver/AliEventServerReconstruction.cxx
Merging STORAGE-dev to master
[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 {}
49
50 AliEventServerReconstruction::~AliEventServerReconstruction()
51 {
52         Close();
53         if(fSettings){delete fSettings;fSettings=0;}
54 }
55
56 void AliEventServerReconstruction::Close()
57 {
58         if(fIsListenning)
59         {
60                 Info("AliRecoServer::Close", "Closing Server");
61                 StopReconstruction();
62                 fIsListenning = kFALSE;
63         }
64 }
65
66 Bool_t AliEventServerReconstruction::StartReconstruction(Int_t run, const char* input)
67 {
68         fCurrentRunId = run;
69
70         StopReconstruction();
71
72         // re-read settings
73         if(fSettings){delete fSettings;}
74         fSettings = new TEnv(AliEventServerUtil::GetPathToServerConf());
75         
76         TString recoBaseDir = fSettings->GetValue("server.saveRecoDir",
77                                                   DEFAULT_SERVER_SAVE_RECO_DIR);
78         
79         // Create directories and logfile
80         TString logFile = Form("%s/log/run%d.log",
81                                recoBaseDir.Data(),
82                                run);
83         
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
91         gSystem->cd(recoBaseDir.Data());
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         SetupReco(input);
101         fAliReco->InitRun(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         fRecoThread = new TThread("AliEventServerReconstruction",
106                               Dispatch,
107                               (void*)this);
108         fRecoThread->Run();
109         fIsListenning = kTRUE;
110
111         return true;
112 }
113
114 void AliEventServerReconstruction::StopReconstruction()
115 {
116         if(!fRecoThread) return;
117         delete fRecoThread;
118         fRecoThread=0;
119         Emit("Stopped()");
120   
121         if(fAliReco){delete fAliReco;fAliReco=0;}
122         if(fCDBmanager){delete fCDBmanager;fCDBmanager=0;}
123 }
124
125 void AliEventServerReconstruction::ReconstructionHandle()
126 {
127         TThread::SetCancelAsynchronous();
128         TThread::SetCancelOn();
129         
130         if(!fAliReco) return;
131         
132         AliESDEvent* event;
133         
134         AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
135         eventManager->CreateSocket(EVENTS_SERVER_PUB);
136         eventManager->CreateSocket(XML_PUB);
137
138         fAliReco->Begin(NULL);
139         if (fAliReco->GetAbort() != TSelector::kContinue) return;
140         fAliReco->SlaveBegin(NULL);
141         if (fAliReco->GetAbort() != TSelector::kContinue) return;
142
143         //******* The loop over events
144         Int_t iEvent = 0;
145         while (fAliReco->HasNextEventAfter(iEvent))
146         {
147                 // check if process has enough resources 
148                 if (!fAliReco->HasEnoughResources(iEvent)) break;
149                 Bool_t status = fAliReco->ProcessEvent(iEvent);
150       
151                 if (status)
152                 {
153                         event = fAliReco->GetESDEvent();
154                         eventManager->Send(event,EVENTS_SERVER_PUB);
155                         eventManager->SendAsXml(event,XML_PUB);
156                 }
157                 else
158                 {
159                         fAliReco->Abort("ProcessEvent",TSelector::kAbortFile);
160                 }
161                 
162                 fAliReco->CleanProcessedEvent();
163                 iEvent++;
164         }
165         fAliReco->SlaveTerminate();
166         if (fAliReco->GetAbort() != TSelector::kContinue) return;
167         fAliReco->Terminate();
168         if (fAliReco->GetAbort() != TSelector::kContinue) return;  
169 }
170
171 Int_t AliEventServerReconstruction::RetrieveGRP(UInt_t run, TString &gdc)
172 {
173         if(!fSettings) return (-1);
174
175         // Retrieve GRP entry for given run from aldaqdb.
176         TString dbHost = fSettings->GetValue("logbook.host", DEFAULT_LOGBOOK_HOST);
177         Int_t dbPort =  fSettings->GetValue("logbook.port", DEFAULT_LOGBOOK_PORT);
178         TString dbName =  fSettings->GetValue("logbook.db", DEFAULT_LOGBOOK_DB);
179         TString user =  fSettings->GetValue("logbook.user", DEFAULT_LOGBOOK_USER);
180         TString password = fSettings->GetValue("logbook.pass", DEFAULT_LOGBOOK_PASS);
181         
182         Int_t ret=AliGRPPreprocessor::ReceivePromptRecoParameters(run, dbHost.Data(),
183                                                                   dbPort, dbName.Data(),
184                                                                   user.Data(), password.Data(),
185                                                                   Form("local://%s",gSystem->pwd()),
186                                                                   gdc);
187
188         if(ret>0) Info("RetrieveGRP","Last run of the same type is: %d",ret);
189         else if(ret==0) Warning("RetrieveGRP","No previous run of the same type found");
190         else if(ret<0) Error("Retrieve","Error code while retrieving GRP parameters returned: %d",ret);
191         return(ret);
192 }
193
194 void AliEventServerReconstruction::SetupReco(const char* input)
195 {
196         if(!fSettings) return;
197
198         //AliTPCRecoParam::SetUseTimeCalibration(kFALSE); //-- !probably should be set from conf file!
199
200         printf(Form("=========================[local://%s/..]===========\n",gSystem->pwd()));
201
202         /* Settings CDB */
203         fCDBmanager = AliCDBManager::Instance();
204   
205         fCDBmanager->SetDefaultStorage(fSettings->GetValue("cdb.defaultStorage", DEFAULT_CDB_STORAGE));
206         fCDBmanager->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath1", DEFAULT_CDB_SPEC_STORAGE_PATH1),  
207                                     fSettings->GetValue( "cdb.specificStorageValue1", DEFAULT_CDB_SPEC_STORAGE_VALUE1));
208
209         fCDBmanager->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath2", DEFAULT_CDB_SPEC_STORAGE_PATH2),  
210                                     fSettings->GetValue( "cdb.specificStorageValue2", DEFAULT_CDB_SPEC_STORAGE_VALUE2));
211   
212         fCDBmanager->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath3", DEFAULT_CDB_SPEC_STORAGE_PATH3),  
213                                     fSettings->GetValue( "cdb.specificStorageValue3", DEFAULT_CDB_SPEC_STORAGE_VALUE3));
214   
215         /* Reconstruction settings */
216         if(fAliReco) delete fAliReco;
217   
218         AliReconstruction* rec = new AliReconstruction;
219         
220         // QA options
221         rec->SetRunQA(fSettings->GetValue( "qa.runDetectors", DEFAULT_QA_RUN));
222         rec->SetRunGlobalQA(fSettings->GetValue( "qa.runGlobal", DEFAULT_QA_RUN_GLOBAL));
223         rec->SetQARefDefaultStorage(fSettings->GetValue( "qa.defaultStorage",DEFAULT_QAREF_STORAGE)) ;
224         rec->SetRunPlaneEff(fSettings->GetValue( "reco.runPlaneEff", DEFAULT_RECO_RUN_PLANE_EFF));
225
226         // AliReconstruction settings
227         rec->SetWriteESDfriend(fSettings->GetValue( "reco.writeESDfriend", DEFAULT_RECO_WRITE_ESDF));
228         rec->SetWriteAlignmentData(fSettings->GetValue( "reco.writeAlignment",DEFAULT_RECO_WRITE_ALIGN));
229         rec->SetInput(input); // reconstruct data from this input
230         rec->SetRunReconstruction(fSettings->GetValue( "reco.detectors", DEFAULT_RECO_DETECTORS));
231         rec->SetUseTrackingErrorsForAlignment("ITS"); //-- !should be set from conf file!
232
233         // switch off cleanESD
234         rec->SetCleanESD(fSettings->GetValue( "reco.cleanESD",DEFAULT_RECO_CLEAN_ESD));
235
236         fAliReco = rec;
237 }