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