Adding base classes for zmq networking and modificiations to event server
[u/mrichter/AliRoot.git] / MONITOR / AliRecoServer.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
13 #include <AliLog.h>
14 #include <AliESDEvent.h>
15 #include <AliCDBManager.h>
16 #include <AliGRPPreprocessor.h>
17 #include <AliReconstruction.h>
18 #include <AliTPCRecoParam.h>
19
20 #include <zmq.hpp>
21
22 #include "AliEventServerUtil.h"
23 #include "AliRecoServer.h"
24 #include "AliRecoServerThread.h"
25
26 ClassImp(AliRecoServer)
27 AliRecoServer::AliRecoServer()
28   : TQObject(),
29   fContext(0),
30   fReco(0),
31   fCDBman(0),
32   fCurrentRunId(0),
33   fIsListenning(kFALSE),
34   fSettings(0),
35   fRecoTh(0)
36 {
37         fContext = new zmq::context_t;
38 }
39
40 AliRecoServer::~AliRecoServer()
41 {
42   Close(); // Full Close Server
43   delete fSettings;
44 }
45
46 void AliRecoServer::Close()
47 {
48   if(fIsListenning){
49         Info("AliRecoServer::Close", "Closing Server");
50         
51         StopReconstruction();
52         
53     delete fContext; fContext=0;
54      fIsListenning = kFALSE;
55   }
56
57 }
58
59 const char* AliRecoServer::GetError() const
60 {}
61
62 Bool_t AliRecoServer::IsListenning() const
63 {
64   return fIsListenning;
65 }
66
67 void AliRecoServer::ThreadFinished(Int_t status)
68 {
69         Emit("ThreadFinished(Int_t) ", status); 
70 }
71
72 Bool_t AliRecoServer::StartReconstruction(Int_t run, const char* input)
73 {
74         fCurrentRunId = run;
75         
76   // stop current reconstruction
77   StopReconstruction();
78
79         // re-read settings
80         if(fSettings) delete fSettings;
81         fSettings = new TEnv(Form("%s/MONITOR/%s", gSystem->Getenv("ALICE_ROOT"), ALIEVENTSERVER_CONF) );
82         
83         TString recoBaseDir = fSettings->GetValue("server.saveRecoDir", DEFAULT_SERVER_SAVE_RECO_DIR);
84         
85    // Create directories and logfile
86         TString logFile = Form("%s/log/run%d.log",
87                                recoBaseDir.Data(),
88                                run);
89         Info("DoStart","Reconstruction log will be written to %s",logFile.Data());
90         if( gSystem->RedirectOutput(logFile.Data())!=0){
91                 printf(Form("AliRecoServer::StartReconstruction [] Error while trying to redirect output to [%s]. Exiting...", logFile.Data()) );
92                 return kFALSE;
93         }
94
95         gSystem->cd(recoBaseDir.Data());
96
97         TString gdcs;
98         if (RetrieveGRP(run,gdcs) <= 0 || gdcs.IsNull()) 
99           return kFALSE;
100           
101         gSystem->mkdir(Form("run%d", run));
102         gSystem->cd(Form("run%d", run));
103
104   // Create Reco and Reco Thread
105   SetupReco(input);
106         fReco->InitRun(input);
107         
108         fRecoTh = new AliRecoServerThread(fContext, fReco);
109         fRecoTh->Start( Form("%s:%d", fSettings->GetValue("server.host", DEFAULT_SERVER_HOST), fSettings->GetValue("server.port", DEFAULT_SERVER_PORT)) );
110         fIsListenning = kTRUE;
111 }
112
113 void AliRecoServer::StopReconstruction()
114 {
115   if(!fRecoTh) return;
116   
117   fRecoTh->Stop();
118   
119         delete fReco; fReco = 0;
120         delete fCDBman; fCDBman = 0;
121         
122         // Emit signal
123         ThreadFinished(0);
124 }
125
126 Int_t AliRecoServer::RetrieveGRP(UInt_t run, TString &gdc)
127 {
128         if(!fSettings) return (-1);
129
130   // Retrieve GRP entry for given run from aldaqdb.
131         TString dbHost = fSettings->GetValue("logbook.host", DEFAULT_LOGBOOK_HOST);
132         Int_t dbPort =  fSettings->GetValue("logbook.port", DEFAULT_LOGBOOK_PORT);
133         TString dbName =  fSettings->GetValue("logbook.db", DEFAULT_LOGBOOK_DB);
134         TString user =  fSettings->GetValue("logbook.user", DEFAULT_LOGBOOK_USER);
135         TString password = fSettings->GetValue("logbook.pass", DEFAULT_LOGBOOK_PASS);
136         
137   Int_t ret=AliGRPPreprocessor::ReceivePromptRecoParameters(run, dbHost.Data(),
138                                                         dbPort, dbName.Data(),
139                                                         user.Data(), password.Data(),
140                                                         Form("local://%s",gSystem->pwd()),
141                                                         gdc);
142
143   if(ret>0) Info("RetrieveGRP","Last run of the same type is: %d",ret);
144   else if(ret==0) Warning("RetrieveGRP","No previous run of the same type found");
145   else if(ret<0) Error("Retrieve","Error code while retrieving GRP parameters returned: %d",ret);
146   return(ret);
147 }
148
149 void AliRecoServer::SetupReco(const char* input)
150 {
151         if(!fSettings) return;
152
153   //AliTPCRecoParam::SetUseTimeCalibration(kFALSE); //-- !probably should be set from conf file!
154
155         printf(Form("=========================[local://%s/..]===========\n",gSystem->pwd()));
156
157   /* Settings CDB */
158         fCDBman = AliCDBManager::Instance();
159   
160   fCDBman->SetDefaultStorage(fSettings->GetValue("cdb.defaultStorage", DEFAULT_CDB_STORAGE));
161   fCDBman->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath1", DEFAULT_CDB_SPEC_STORAGE_PATH1),  
162                                                                                                                                                                                                         fSettings->GetValue( "cdb.specificStorageValue1", DEFAULT_CDB_SPEC_STORAGE_VALUE1));
163
164         fCDBman->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath2", DEFAULT_CDB_SPEC_STORAGE_PATH2),  
165                                                                                                                                                                                                         fSettings->GetValue( "cdb.specificStorageValue2", DEFAULT_CDB_SPEC_STORAGE_VALUE2));
166   
167         fCDBman->SetSpecificStorage(fSettings->GetValue( "cdb.specificStoragePath3", DEFAULT_CDB_SPEC_STORAGE_PATH3),  
168                                                                                                                                                                                                         fSettings->GetValue( "cdb.specificStorageValue3", DEFAULT_CDB_SPEC_STORAGE_VALUE3));
169   
170   /* Reconstruction settings */
171   if(fReco) delete fReco;
172   
173         AliReconstruction* rec = new AliReconstruction;
174         
175   // QA options
176   rec->SetRunQA(fSettings->GetValue( "qa.runDetectors", DEFAULT_QA_RUN));
177   rec->SetRunGlobalQA(fSettings->GetValue( "qa.runGlobal", DEFAULT_QA_RUN_GLOBAL));
178   rec->SetQARefDefaultStorage(fSettings->GetValue( "qa.defaultStorage",DEFAULT_QAREF_STORAGE)) ;
179   rec->SetRunPlaneEff(fSettings->GetValue( "reco.runPlaneEff", DEFAULT_RECO_RUN_PLANE_EFF));
180
181   // AliReconstruction settings
182   rec->SetWriteESDfriend(fSettings->GetValue( "reco.writeESDfriend", DEFAULT_RECO_WRITE_ESDF));
183   rec->SetWriteAlignmentData(fSettings->GetValue( "reco.writeAlignment",DEFAULT_RECO_WRITE_ALIGN));
184   rec->SetInput(input); // reconstruct data from this input
185   rec->SetRunReconstruction(fSettings->GetValue( "reco.detectors", DEFAULT_RECO_DETECTORS));
186   rec->SetUseTrackingErrorsForAlignment("ITS"); //-- !should be set from conf file!
187
188   // switch off cleanESD
189   rec->SetCleanESD(fSettings->GetValue( "reco.cleanESD",DEFAULT_RECO_CLEAN_ESD));
190
191         fReco = rec;
192 }