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