Added two missing includes to allow macro compilation (thanks to Laurent for remarkin...
[u/mrichter/AliRoot.git] / ITS / ITSSPDFOda.cxx
CommitLineData
afd9bf7e 1/*
2Contact: annalisa.mastroserio@cern.ch
3Link: tydes.home.cern.ch/tydes/doc/CalibrationOverview/CalibrationAlgorithms/
4Run Type: DAQ_FO_UNIF_SCAN
5DA Type: LDC
6Number of events needed: Depending on scan type
7Input Files: spd_focalib_params, raw data
8Output Files: ./calibResults/ScanDCSconfigToFXS/*
9Trigger types used: PHYSICS
10*/
11
286382a3 12////////////////////////////////////////////////////////////////////////////////
13// This program can be compiled in two modes. //
14// //
15// 1. With the DAQ DA framework on. This is the default operating mode. //
16// Call this program with the name of the executable followed by the //
17// data files to process. //
18// //
19// 2. Without the DAQ DA framework on. Define the SPD_DA_OFF environment var. //
20// Call this program with the name of the executable followed by the //
21// runNr and the data files to process. //
22// //
23////////////////////////////////////////////////////////////////////////////////
24
25#ifndef SPD_DA_OFF
26extern "C" {
27#include <daqDA.h>
28}
29#endif
30#include "event.h"
31#include "monitor.h"
32#include "AliRawReaderDate.h"
33#include "AliITSRawStreamSPD.h"
34#include "AliITSOnlineSPDfoChip.h"
35#include "AliITSOnlineSPDfoInfo.h"
36#include "AliITSOnlineSPDfo.h"
37#include "AliITSOnlineSPDfoAnalyzer.h"
38#include "AliLog.h"
39#include <Riostream.h>
40#include <fstream>
41#include <TROOT.h>
42#include <TStopwatch.h>
43#include <TPluginManager.h>
44#include <TObjArray.h>
45#include <TClonesArray.h>
46#include <TObjString.h>
47#include <TString.h>
48#include <TFitter.h>
49#include <TFile.h>
50
51int main(int argc, char **argv) {
52 if (argc<2) {
53 printf("Wrong number of arguments\n");
54 return -1;
55 }
56
57
58 TStopwatch timer;
59 timer.Start();
60
61 // directory structure, hard coded
286382a3 62 char *saveDirDCSconfigToFXS= "./calibResults/ScanDCSconfigToFXS"; // may delete content
63 char *configFilesDir = "./configFiles"; // may delete content
64 char *saveDirIdsToFXS = "./calibResults/IdsToFXS";
afd9bf7e 65
66 // make sure the directory structure is correct:
67 system("mkdir ./calibResults >& /dev/null");
68 system("mkdir ./calibResults/ScanDCSconfigToFXS >& /dev/null");
69 system("mkdir ./calibResults/IdsToFXS >& /dev/null");
70 system("mkdir ./configFiles >& /dev/null");
71
72
286382a3 73 // parameters config files
74 TString thresholdsFileName = Form("%s/focalib_params.txt",configFilesDir);
75
76 TFitter *fitter = new TFitter(3);
77 TVirtualFitter::SetFitter(fitter);
78
79 // This line is needed in case of a stand-alone application w/o
80 // $ROOTSYS/etc/system.rootrc file
81 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
82 "*",
83 "TStreamerInfo",
84 "RIO",
85 "TStreamerInfo()");
86
87 // turn off annoying warning messages
88 new AliLog;
89 AliLog::Instance()->SetGlobalDebugLevel(-20);
90
91
92
93
94// ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********
95
96 //chip efficiency selection parameters (needed afterwards for the data analysis)
97 Int_t status = 0;
98#ifndef SPD_DA_OFF
99 TString idp = "spd_focalib_params";
100 status=daqDA_DB_getFile(idp.Data(),thresholdsFileName.Data());
101 if (status) {
102 printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),status);
103 TString rmCmd = Form("rm -f %s",thresholdsFileName.Data());
104 system(rmCmd.Data());
105 }
106#endif
107
108
109
110 // ********* STEP 1: Produce FO scan container files (Reference Data). ***********************************
111 int startSeg = 1;
112
113#ifndef SPD_DA_OFF
114 if (getenv("DATE_RUN_NUMBER")==0) {
115 printf("DATE_RUN_NUMBER not properly set.\n");
116 return -1;
117 }
118 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
119#else
120 int runNr = atoi(argv[1]);
121 startSeg = 2;
122#endif
123
124 Int_t evType =-1;
125 AliITSOnlineSPDfoInfo *info[20]; Int_t ntriggers[20]; Int_t vDB[20]; Bool_t iseq[20];
286382a3 126 AliITSOnlineSPDfo *fomanager[20];
127 TString s = "focalib";
128
129 for(Int_t equip =0; equip < 20; equip++ ) {
130 info[equip] = new AliITSOnlineSPDfoInfo();
131 info[equip]->SetRunNumber(runNr);
132 info[equip]->SetRouter(equip);
133 ntriggers[equip] = 0;
134 vDB[equip] =0;
135 iseq[equip]=kFALSE;
136 fomanager[equip] = new AliITSOnlineSPDfo(s,runNr,equip);
137 }
138
139
140 // loop over run segments
141 for (int segNr=startSeg; segNr<argc; segNr++) {
142
143 int status;
144
145 // define data source : this is argument 1
146 status=monitorSetDataSource( argv[segNr] );
147 if (status!=0) {
148 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
149 return -1;
150 }
151 // declare monitoring program
152 status=monitorDeclareMp("ITS_SPD_CAL");
153 if (status!=0) {
154 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
155 return -1;
156 }
157 // define wait event timeout - 1s max
158 monitorSetNowait();
159 monitorSetNoWaitNetworkTimeout(1000);
160
161 Int_t eventType;
162 UInt_t eventNr=0;
afd9bf7e 163
286382a3 164 // main loop (infinite)
165 for(;;) {
166
167 struct eventHeaderStruct *event;
168 eventTypeType eventT;
169
170 /* check shutdown condition */
171#ifndef SPD_DA_OFF
172 if (daqDA_checkShutdown()) {break;}
173#endif
174 /* get next event (blocking call until timeout) */
175 status=monitorGetEventDynamic((void **)&event);
176 if (status==MON_ERR_EOF) {
177 printf ("End of File detected\n");
178 break; /* end of monitoring file has been reached */
179 }
180 if (status!=0) {
181 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
182 break;
183 }
184 /* retry if got no event */
185 if (event==NULL) {
186 continue;
187 }
188
189 eventT=event->eventType;
190 eventType = (Int_t) eventT;
191
192 if (eventT == PHYSICS_EVENT) {
193
194 eventNr++;
195 //if(eventNr%5000 == 0 )printf(" eventNr %d\n",eventNr);
196
197 AliRawReader *reader = new AliRawReaderDate((void*)event);
198 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
199
200 for (UInt_t eqId=0; eqId<20; eqId++) {
201
202 reader->Reset();
203 reader->Select("ITSSPD",eqId,eqId);
204
205 if (str->ReadCalibHeader()>0) {
206
207 if(evType<0) evType = str->GetFOHtype();
208
209 if(!iseq[eqId]){ // create output files
210 fomanager[eqId]->CreateOutputFile();
211 fomanager[eqId]->SetFOscanParams(info[eqId]);
212 iseq[eqId]=kTRUE;
213 }
214
215 if(info[eqId]->GetNumDACindex()<1) {
216 Int_t ind =0;
217 while(str->GetFOHdacIndex(ind)>0) {
218 info[eqId]->AddDACindex(str->GetFOHdacIndex(ind));
219 ind++;
220 }
221 }
222
223 if(!ntriggers[eqId]) {
224 ntriggers[eqId] = str->GetFOHtriggers();
225 info[eqId]->SetNumTriggers(str->GetFOHtriggers());
226 }
227 if(!vDB[eqId]) {
228 vDB[eqId] = str->GetFOHglobalDBversion();
229 info[eqId]->SetDBversion(str->GetFOHglobalDBversion());
230 }
231
232 if(!fomanager[eqId]->GetNdacs()) fomanager[eqId]->SetNdacs(str->GetFOHnumDacs());
233
234 TArrayS dacvalues(str->GetFOHnumDacs());
afd9bf7e 235 for(Int_t n = 0; n<(Int_t)str->GetFOHnumDacs(); n++) dacvalues.AddAt(str->GetFOHdacValue(n),n);
286382a3 236
237 TArrayS dacs = fomanager[eqId]->CreateDACArray(dacvalues, info[eqId]->GetDACIndexArray());
238
239 for(Int_t ihs =0; ihs < 6; ihs++) { // needed in the header to access the HS and ChipId info (in data it is different)
240 for(Int_t ich =0; ich < 10; ich++){
241 if(!str->GetFOHchipPresent(ihs, ich)) continue;
14374702 242 info[eqId]->SetActiveChipsAndHS(ihs,ich);
286382a3 243 Short_t measure[4] = {str->GetFOHMatrixID(),str->GetFOHpixelRow(), str->GetFOHpixelCol(), str->GetFOHchipCount(ihs,ich)};
244 fomanager[eqId]->AddMeasurement(dacs,measure,ihs,ich);
245 } // chip loop
246 }// HS loop
247 }// if str->ReadHeader()>0;
248 }//if eqId
249 delete str;
250 delete reader;
251 }
252
253 // free resources
254 free(event);
255 }// infinite loop
256
257
258
259
260#ifndef SPD_DA_OFF
261 daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
262#else
263 printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
264#endif
265
266 }// loop over run segments
267
268
269 TString id[20], files[20];
270 for(Int_t ifile =0; ifile < 20; ifile++) {
271 if(iseq[ifile]){
272 id[ifile] = Form("SPD_ref_fo%02i",ifile);
273 files[ifile] = fomanager[ifile]->GetFile()->GetName();
274 fomanager[ifile]->WriteToFile();
275 }
276 delete fomanager[ifile];
277 }
278
279
280 // ANALYSIS part
281
282 for(Int_t iff =0; iff<20 ; iff++){
283 if(!iseq[iff]) continue;
284
285 AliITSOnlineSPDfoAnalyzer * analyzer = new AliITSOnlineSPDfoAnalyzer(Form("%i_%s%02i.root",runNr,s.Data(),iff));
286 analyzer->ReadParamsFromLocation(configFilesDir);
287 analyzer->Process();
288
289 TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,iff);
290 ofstream dcsfile;
291 dcsfile.open(dcsConfigFileName.Data());
292 dcsfile << "[SPD SCAN]\n";
293 dcsfile << "RunNumber=" << runNr << "\n";
294 dcsfile << "Type="<< evType <<"\n";
295 dcsfile << "Router=" << iff << "\n";
296 dcsfile << "ActualDetConfiguration=" << vDB[iff]<<"\n\n";
297 dcsfile << "[DACvalues]\n";
298
299 for(Int_t hs =0; hs<6; hs++){
300 for(Int_t ichip =0; ichip < 10; ichip++){
301 TArrayI dacs = analyzer->ChooseDACValues(hs,ichip);
302
303 if(dacs.GetSize() == 0) continue;
304 for(Int_t idac =0; idac < dacs.GetSize() - 1; idac++) { // -1 (the last one is the quality flag)
305 if(dacs.At(idac) >=0 ) {
14374702 306
286382a3 307 dcsfile << ((analyzer->GetFOHandler())->GetFOscanInfo())->GetDACindex(idac) << ",";
308 dcsfile << iff << ",";
309 dcsfile << hs << ",";
310 dcsfile << ichip << "=" ;
311 dcsfile << dacs.At(idac) << ",";
312 dcsfile << dacs.At(dacs.GetSize() - 1) << "\n";
313 }
314 }
315 }
316 }
317 dcsfile.close();
318 }
319
320
321 printf("Preparing DCS config files\n");
322 // send a tared file of all the dcsConfig text files
323 TString command = Form("cd %s; tar -cf dcsConfig.tar *",saveDirDCSconfigToFXS);
324 //printf("\n\n%s\n\n",command.Data());
afd9bf7e 325 system(command.Data());
286382a3 326 TString fileName = Form("%s/dcsConfig.tar",saveDirDCSconfigToFXS);
327 TString iddcs = "SPD_dcsConfig";
328
329#ifndef SPD_DA_OFF
330 status = daqDA_FES_storeFile(fileName.Data(),iddcs.Data());
331 if (status!=0) {
332 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
333 return -1;
334 }
335#endif
336
337
338 printf("Opening id list file\n");
339 TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
340 ofstream idsFXSfile;
341 idsFXSfile.open(idsFXSFileName.Data());
342
343 // send reference data to FXS
344 for (UInt_t eqId=0; eqId<20; eqId++) {
345 if(!iseq[eqId]) continue;
346 //printf("Preparing reference data for eq %d\n",eqId);
347
5e575679 348 TString idf = Form("SPD_ref_fo_%d",eqId);
286382a3 349#ifndef SPD_DA_OFF
350 status = daqDA_FES_storeFile(files[eqId].Data(),idf.Data());
351 if (status!=0) {
352 printf("Failed to export file %s , status %d\n",files[eqId].Data(),status);
353 return -1;
354 }
355#endif
356 idsFXSfile << Form("%s\n",idf.Data());
357 }
358
e737a317 359
360
361 printf("Preparing id list file\n");
362 idsFXSfile.close();
363 TString idlist = "SPD_id_list";
364#ifndef SPD_DA_OFF
365 status = daqDA_FES_storeFile(idsFXSFileName.Data(),idlist.Data());
366 if (status!=0) {
367 printf("Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);
368 return -1;
369 }
370#endif
371
372
373
286382a3 374 timer.Stop();
375 timer.Print();
376 printf("DA finished.\n");
377 return 0;
378}