]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ITS/ITSSPDda.cxx
Bug fix
[u/mrichter/AliRoot.git] / ITS / ITSSPDda.cxx
CommitLineData
1fd93b67 1////////////////////////////////////////////////////////////////////////////////
2// This program can be run in two modes. //
3// //
4// 1. With the DAQ DA framework on. This is the default operating mode. //
5// Call this program with the name of the executable followed by the //
6// data files to process. //
7// //
8// 2. Without the DAQ DA framework on. Define the SPD_DA_OFF environment var. //
9// Call this program with the name of the executable followed by the //
10// runNr and the data files to process. //
11// //
12////////////////////////////////////////////////////////////////////////////////
3ec5024c 13
14extern "C" {
15#include "daqDA.h"
16}
17#include "event.h"
18#include "monitor.h"
19#include "AliRawReaderDate.h"
20#include "AliITSRawStreamSPD.h"
21#include "AliITSOnlineSPDscan.h"
22#include "AliITSOnlineSPDscanSingle.h"
23#include "AliITSOnlineSPDscanMultiple.h"
24#include "AliITSOnlineSPDscanMeanTh.h"
25#include "AliITSOnlineSPDscanAnalyzer.h"
26#include "AliLog.h"
27#include <iostream>
28#include <fstream>
29#include <TROOT.h>
30#include <TPluginManager.h>
31
32
33int main(int argc, char **argv) {
34 if (argc<2) {
35 printf("Wrong number of arguments\n");
36 return -1;
37 }
38
1fd93b67 39 // make directory structure (if not here already):
40 system("mkdir ./calibResults >& /dev/null");
41 system("mkdir ./calibResults/Noisy >& /dev/null");
42 system("mkdir ./calibResults/NoisyToFXS >& /dev/null");
43 system("mkdir ./calibResults/Parameters >& /dev/null");
44 system("mkdir ./calibResults/Reference >& /dev/null");
45 char *saveDirNoisy = "./calibResults/Noisy";
46#ifndef SPD_DA_OFF
47 char *saveDirNoisyToFXS = "./calibResults/NoisyToFXS";
3ec5024c 48#endif
1fd93b67 49 char *saveDirParameters = "./calibResults/Parameters";
50 char *saveDirRef = "./calibResults/Reference";
3ec5024c 51
52 // This line is needed in case of a stand-alone application w/o
53 // $ROOTSYS/etc/system.rootrc file
54 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
55 "*",
56 "TStreamerInfo",
57 "RIO",
58 "TStreamerInfo()");
59
60 // turn off annoying warning messages
61 new AliLog;
1fd93b67 62 AliLog::Instance()->SetGlobalDebugLevel(-20);
3ec5024c 63
64 // calib scan types
65 enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY};
66
67
68 // ********* STEP 1: Produce scan container files. ***********************************
69 int startSeg = 1;
70
1fd93b67 71#ifndef SPD_DA_OFF
3ec5024c 72 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
73#else
74 int runNr = atoi(argv[1]);
75 startSeg = 2;
76#endif
77
1fd93b67 78#ifndef SPD_DA_OFF
79 UInt_t nrNoisyFilesProduced=0;
80#endif
81
3ec5024c 82 // container objects
83 AliITSOnlineSPDscan *scanObj[20];
84 Bool_t bScanInit[20];
85 for (UInt_t eqId=0; eqId<20; eqId++) {
86 scanObj[eqId]=NULL;
87 bScanInit[eqId]=kFALSE;
88 }
89 // header data variables
90 UInt_t routerNr[20];
91 Bool_t halfStaveScanned[20][6];
92 UInt_t type[20];
93 Bool_t dataFormat[20];
94 UInt_t triggers[20];
95 Bool_t chipPresent[20][6][10];
96 UInt_t dacStart[20];
97 UInt_t dacEnd[20];
98 UInt_t dacStep[20];
99 UInt_t dacId[20];
100 UInt_t rowStart[20];
101 UInt_t rowEnd[20];
102 UInt_t rowValue[20];
103 UInt_t dacValue[20];
104 UInt_t dacHigh[20][6];
105 UInt_t dacLow[20][6];
106 UInt_t TPAmp[20][6];
1fd93b67 107 Bool_t minTHchipPresent[20][10];
3ec5024c 108 // current scan step flag
109 Int_t currentStep[20];
110 for (UInt_t eqId=0; eqId<20; eqId++) currentStep[eqId] = 9999;
111
112 // loop over run segments
113 for (int segNr=startSeg; segNr<argc; segNr++) {
114
115
116 int status;
117
118 /* define data source : this is argument 1 */
119 status=monitorSetDataSource( argv[segNr] );
120 if (status!=0) {
121 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
122 return -1;
123 }
124 /* declare monitoring program */
125 status=monitorDeclareMp("ITS_SPD_CAL");
126 if (status!=0) {
127 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
128 return -1;
129 }
130 /* define wait event timeout - 1s max */
131 monitorSetNowait();
132 monitorSetNoWaitNetworkTimeout(1000);
133
134
135 struct eventHeaderStruct *event;
136 eventTypeType eventT;
137 UInt_t eventNr=0;
138
139
140 /* main loop (infinite) */
141 for(;;) {
142
143 /* check shutdown condition */
1fd93b67 144#ifndef SPD_DA_OFF
3ec5024c 145 if (daqDA_checkShutdown()) {break;}
1fd93b67 146#endif
3ec5024c 147 /* get next event (blocking call until timeout) */
148 status=monitorGetEventDynamic((void **)&event);
149 if (status==MON_ERR_EOF) {
150 printf ("End of File detected\n");
151 break; /* end of monitoring file has been reached */
152 }
153 if (status!=0) {
154 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
155 break;
156 }
157 /* retry if got no event */
158 if (event==NULL) {
159 printf("event==NULL\n");
160 continue;
161 }
162
163 eventT=event->eventType;
1fd93b67 164 if (eventT == PHYSICS_EVENT){
3ec5024c 165
166
167 eventNr++;
1fd93b67 168 // printf("eventNr %d\n",eventNr);
3ec5024c 169
170 AliRawReader *reader = new AliRawReaderDate((void*)event);
171 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
172
173
174 for (UInt_t eqId=0; eqId<20; eqId++) {
175
176 reader->Reset();
177 reader->Select("ITSSPD",eqId,eqId);
178
179 // Hit Event flags, specific for one event
180 Bool_t hitEventHSIncremented[6];
181 Bool_t hitEventChipIncremented[6][10];
182 for (UInt_t hs=0; hs<6; hs++) {
183 hitEventHSIncremented[hs] = kFALSE;
184 for (UInt_t chip=0; chip<10; chip++) {
185 hitEventChipIncremented[hs][chip] = kFALSE;
186 }
187 }
188
189 if (str->ReadCalibHeader()) {
190 // first check the type:
191 if (bScanInit[eqId] && type[eqId]!=str->GetHtype()) {
192 printf("Calib header problem. Type changed (%d -> %d)!\n",type[eqId],str->GetHtype());
193 }
194
195 // read calib values
1fd93b67 196 routerNr[eqId] = str->GetHrouterNr();
197 type[eqId] = str->GetHtype();
198 dataFormat[eqId] = str->GetHdataFormat();
199 triggers[eqId] = str->GetHtriggers();
200 dacStart[eqId] = str->GetHdacStart();
201 dacEnd[eqId] = str->GetHdacEnd();
202 dacStep[eqId] = str->GetHdacStep();
203 dacId[eqId] = str->GetHdacId();
204 rowStart[eqId] = str->GetHrowStart();
205 rowEnd[eqId] = str->GetHrowEnd();
206 rowValue[eqId] = str->GetHrowValue();
207 dacValue[eqId] = str->GetHdacValue();
3ec5024c 208 for (UInt_t hs=0; hs<6; hs++) {
1fd93b67 209 halfStaveScanned[eqId][hs] = str->GetHhalfStaveScanned(hs);
210 dacHigh[eqId][hs] = str->GetHdacHigh(hs);
211 dacLow[eqId][hs] = str->GetHdacLow(hs);
212 TPAmp[eqId][hs] = str->GetHTPAmp(hs);
3ec5024c 213 for (UInt_t chip=0; chip<10; chip++) {
1fd93b67 214 chipPresent[eqId][hs][chip] = str->GetHchipPresent(hs,chip);
3ec5024c 215 }
216 }
1fd93b67 217 for (UInt_t chip=0; chip<10; chip++) {
218 minTHchipPresent[eqId][chip] = str->GetHminTHchipPresent(chip);
219 }
3ec5024c 220
221 currentStep[eqId] = (dacValue[eqId]-dacStart[eqId])/dacStep[eqId];
222 if (type[eqId]==DELAY) {
223 currentStep[eqId]=currentStep[eqId]*2;
224 dacValue[eqId]=dacValue[eqId]*2;
225 if (dacHigh[eqId][0]==128) { // misc_ctrl value
226 currentStep[eqId]=currentStep[eqId]+1;
227 dacValue[eqId]=dacValue[eqId]+1;
228 }
229 }
230
231 // router nr check:
232 if (routerNr[eqId]!=eqId) {
233 printf("Router nr problem? Router nr %d != EqID %d\n",routerNr[eqId],eqId);
234 }
235
236 if (!bScanInit[eqId]) {
237 // initialize container object
238 Char_t fileName[200];
239 sprintf(fileName,"%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
240 switch (type[eqId]) {
241 case NOISE:
242 case UNIMA:
243 scanObj[eqId] = new AliITSOnlineSPDscanSingle(fileName);
244 ((AliITSOnlineSPDscanSingle*)scanObj[eqId])->ClearThis();
245 bScanInit[eqId]=kTRUE;
246 break;
247 case MINTH:
248 case DAC:
249 case DELAY:
250 scanObj[eqId] = new AliITSOnlineSPDscanMultiple(fileName);
251 scanObj[eqId]->ClearThis();
252 bScanInit[eqId]=kTRUE;
253 break;
254 case MEANTH:
255 scanObj[eqId] = new AliITSOnlineSPDscanMeanTh(fileName);
256 scanObj[eqId]->ClearThis();
257 bScanInit[eqId]=kTRUE;
258 break;
259 default:
260 printf("Unknown scan type: %d.\n",type[eqId]);
261 }
262 // some multiple scan data
263 if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
264 ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacId(dacId[eqId]);
265 }
266 // some common data
267 scanObj[eqId]->SetRunNr((UInt_t)runNr);
268 scanObj[eqId]->SetRouterNr(routerNr[eqId]);
269 for (UInt_t hs=0; hs<6; hs++) {
270 scanObj[eqId]->SetHalfStaveScanned(hs,halfStaveScanned[eqId][hs]);
271 }
272 scanObj[eqId]->SetType(type[eqId]);
273 scanObj[eqId]->SetDataFormat(dataFormat[eqId]);
274 for (Int_t hs=0; hs<6; hs++) {
275
276 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
277 Bool_t halfStavePresent = str->GetHalfStavePresent(hs);
278 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
279
280 for (UInt_t chip=0; chip<10; chip++) {
281 scanObj[eqId]->SetChipPresent(hs,chip,chipPresent[eqId][hs][chip]);
282
283 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
284 if (halfStavePresent) scanObj[eqId]->SetChipPresent(hs,chip,kTRUE);
285 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
286
287 }
288 }
289 scanObj[eqId]->SetRowStart(rowStart[eqId]);
290 scanObj[eqId]->SetRowEnd(rowEnd[eqId]);
291 scanObj[eqId]->SetDacStart(dacStart[eqId]);
292 scanObj[eqId]->SetDacEnd(dacEnd[eqId]);
293 scanObj[eqId]->SetDacStep(dacStep[eqId]);
294 }
295
296 if (type[eqId]==MINTH) {
297 scanObj[eqId]->SetTriggers(currentStep[eqId],triggers[eqId]);
298 }
299 if (type[eqId]==UNIMA || type[eqId]==NOISE) {
300 if (currentStep[eqId]==9999) printf("SPDcalibratorStep1 (eq %d): single step\n",eqId);
301 currentStep[eqId]=0;
302 }
303 if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
304 ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacValue(currentStep[eqId],dacValue[eqId]);
305 if (type[eqId]==DELAY) {
306 printf("SPDcalibratorStep1 (eq %d): DAC %d/%d , step %d\n",eqId,dacValue[eqId]/2,dacHigh[eqId][0],currentStep[eqId]);
307 }
308 else {
309 printf("SPDcalibratorStep1 (eq %d): DAC %d , step %d\n",eqId,dacValue[eqId],currentStep[eqId]);
310 }
311 }
312 if (type[eqId]==MEANTH) {
313 for (Int_t hs=0; hs<6; hs++) {
314 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacLow(currentStep[eqId],hs,dacLow[eqId][hs]);
315 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacHigh(currentStep[eqId],hs,dacHigh[eqId][hs]);
316 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetTPAmp(currentStep[eqId],hs,TPAmp[eqId][hs]);
317 }
318 }
319
320
321 }
322
323 if (bScanInit[eqId]) {
324 while (str->Next()) {
325 UInt_t hs = str->GetHalfStaveNr();
326 UInt_t chip = str->GetChipAddr();
1fd93b67 327#ifndef SPD_DA_OFF
328 if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip]) {
329#else
330 if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
331#endif
3ec5024c 332 scanObj[eqId]->IncrementHits(currentStep[eqId],hs,chip,str->GetChipCol(),str->GetChipRow());
333
334 if (!hitEventHSIncremented[hs]) {
335 scanObj[eqId]->IncrementHitEventsTot(currentStep[eqId],hs);
336 hitEventHSIncremented[hs]=kTRUE;
337 }
338
339 if (!hitEventChipIncremented[hs][chip]) {
340 scanObj[eqId]->IncrementHitEvents(currentStep[eqId],hs,chip);
341 hitEventChipIncremented[hs][chip]=kTRUE;
342 }
343 }
344
345 }
346
347 if (type[eqId]!=MINTH) { // for minth, triggers are set from header info
348 scanObj[eqId]->IncrementTriggers(currentStep[eqId]);
349 }
350
351 }
352
353 }
354
355 delete str;
356 delete reader;
357
358 }
359
360 /* free resources */
361 free(event);
362
363 }
364
365 status = monitorLogout();
366 if (status != 0) {
367 printf("monitorLogout() failed : %s\n",monitorDecodeError(status));
368 return -1;
369 }
370
1fd93b67 371#ifndef SPD_DA_OFF
3ec5024c 372 daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
373#else
374 printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
375#endif
376
377 }
378
379 // clean up scan objects
380 for (UInt_t eqId=0; eqId<20; eqId++) {
381 if (scanObj[eqId]!=NULL) delete scanObj[eqId];
382 }
383
384
385
386
387
388 // ********* STEP 2: Analyze scan container files. ***********************************
1fd93b67 389#ifndef SPD_DA_OFF
3ec5024c 390 // clear noisyToFXS dir:
391 Char_t command[200];
392 sprintf(command,"cd %s; rm -f *",saveDirNoisyToFXS);
393 system(command);
394#endif
395
396 AliITSOnlineSPDscanAnalyzer *analyzer;
397
398 // *** *** *** start loop over equipments (eq_id)
399 for (int eqId=0; eqId<20; eqId++) {
400
401 Char_t fileName[200];
402 sprintf(fileName,"%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
403 analyzer = new AliITSOnlineSPDscanAnalyzer(fileName);
404
405 Int_t type = analyzer->GetType();
406 Int_t dacId = analyzer->GetDacId();
407 if (type!=99) {
408 if (type==DAC) {
409 printf("SPD calibrator Step2: eqId %d, type %d, dacId %d\n",eqId,type,dacId);
410 }
411 else printf("SPD calibrator Step2: eqId %d type %d\n",eqId,type);
412 }
413
414
415
416
417 if (type==UNIMA) {
418 }
419 else if (type==NOISE) {
420 if (analyzer->ProcessNoisyPixels(saveDirNoisy)) {
421 for (UInt_t module=0; module<240; module++) {
422 if (analyzer->SaveDeadNoisyPixels(module,saveDirNoisy)) {
1fd93b67 423#ifndef SPD_DA_OFF
424 nrNoisyFilesProduced++;
3ec5024c 425 Char_t command[100];
426 sprintf(command,"cp %s/SPD_DeadNoisy_%d.root %s/.",saveDirNoisy,module,saveDirNoisyToFXS);
427 system(command);
428#endif
429 }
430 }
431 }
432 }
433// else if (type==DAC && dacId==42) {
434// Char_t ofileName[100];
435// sprintf(ofileName,"%s/delay_eq_%d.txt",saveDirParameters,eqId);
436// ofstream ofile;
437// ofile.open (ofileName);
438// for (UInt_t hs=0; hs<6; hs++) {
439// for (UInt_t chipNr=0; chipNr<10; chipNr++) {
440// ofile << analyzer->GetDelay(hs,chipNr);
441// ofile << "\t";
442// }
443// ofile << "\n";
444// }
445// ofile.close();
1fd93b67 446//#ifndef SPD_DA_OFF
3ec5024c 447// Char_t id[20];
448// sprintf(id,"SPD_delay_%d",eqId);
449// Int_t status = daqDA_FES_storeFile(ofileName,id);
450// if (status) {
451// printf("Failed to export file %s , status %d\n",ofileName,status);
452// }
453//#endif
454// }
455 else if (type==MINTH || (type==DAC && dacId==39)) {
456 Char_t ofileName[100];
457 sprintf(ofileName,"%s/minth_eq_%d.txt",saveDirParameters,eqId);
458 ofstream ofile;
459 ofile.open (ofileName);
460 for (UInt_t hs=0; hs<6; hs++) {
461 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
462 ofile << analyzer->GetMinTh(hs,chipNr);
463 ofile << "\t";
464 }
465 ofile << "\n";
466 }
467 ofile.close();
1fd93b67 468#ifndef SPD_DA_OFF
3ec5024c 469 Char_t id[20];
470 sprintf(id,"SPD_minth_%d",eqId);
471 Int_t status = daqDA_FES_storeFile(ofileName,id);
472 if (status) {
473 printf("Failed to export file %s , status %d\n",ofileName,status);
474 }
475#endif
476 }
477 else if (type==DELAY) {
478 Char_t ofileName[100];
479 sprintf(ofileName,"%s/delay_eq_%d.txt",saveDirParameters,eqId);
480 ofstream ofile;
481 ofile.open (ofileName);
482 for (UInt_t hs=0; hs<6; hs++) {
483 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
484 UInt_t clockCycle = analyzer->GetDelay(hs,chipNr);
485 UInt_t delayCtrl = clockCycle/2;
486 UInt_t miscCtrl = 192;
487 if (clockCycle%2==1) miscCtrl = 128;
488 ofile << delayCtrl << "/" << miscCtrl;
489 ofile << "\t";
490 }
491 ofile << "\n";
492 }
493 ofile.close();
1fd93b67 494#ifndef SPD_DA_OFF
3ec5024c 495 Char_t id[20];
496 sprintf(id,"SPD_delay_%d",eqId);
497 Int_t status = daqDA_FES_storeFile(ofileName,id);
498 if (status) {
499 printf("Failed to export file %s , status %d\n",ofileName,status);
500 }
501#endif
502 }
503
504
505 delete analyzer;
506
1fd93b67 507#ifndef SPD_DA_OFF
3ec5024c 508 daqDA_progressReport((unsigned int)(50+(eqId+1)*2.5));
509#else
510 printf("progress: %d\n",(unsigned int)(50+(eqId+1)*2.5));
511#endif
512
513 }
514 // *** *** *** end loop over equipments (eq_id)
515
516
1fd93b67 517#ifndef SPD_DA_OFF
518 if (nrNoisyFilesProduced>0) {
519 // send a tared file of all new noisy maps
520 sprintf(command,"cd %s; tar -cf noisy.tar *",saveDirNoisyToFXS);
521 printf("\n\n%s\n\n",command);
522 system(command);
523 Char_t fileName[200];
524 sprintf(fileName,"%s/noisy.tar",saveDirNoisyToFXS);
525 Char_t id[20];
526 sprintf(id,"SPD_noisy");
527 Int_t status = daqDA_FES_storeFile(fileName,id);
528 if (status!=0) {
529 printf("Failed to export file %s , status %d\n",fileName,status);
530 return -1;
531 }
3ec5024c 532 }
533#endif
534
535
536 return 0;
537}