1 ////////////////////////////////////////////////////////////////////////////////
2 // This program can be run in two modes. //
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. //
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. //
12 ////////////////////////////////////////////////////////////////////////////////
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 "AliITSOnlineCalibrationSPDhandler.h"
31 #include <TPluginManager.h>
32 #include <TObjArray.h>
35 int main(int argc, char **argv) {
37 printf("Wrong number of arguments\n");
41 // make directory structure (if not here already):
42 system("mkdir ./calibResults >& /dev/null");
43 system("mkdir ./calibResults/Noisy >& /dev/null");
44 system("mkdir ./calibResults/NoisyToFXS >& /dev/null");
45 system("mkdir ./calibResults/DCSconfigToFXS >& /dev/null");
46 system("mkdir ./calibResults/Parameters >& /dev/null");
47 system("mkdir ./calibResults/Reference >& /dev/null");
48 char *saveDirNoisy = "./calibResults/Noisy";
49 char *saveDirNoisyToFXS = "./calibResults/NoisyToFXS";
50 char *saveDirDCSconfigToFXS= "./calibResults/DCSconfigToFXS";
51 char *saveDirParameters = "./calibResults/Parameters";
52 char *saveDirRef = "./calibResults/Reference";
54 char *paramsFileName = "./standal_params.txt";
55 char *permNoisyFileName = "./perm_noisy.txt";
57 // This line is needed in case of a stand-alone application w/o
58 // $ROOTSYS/etc/system.rootrc file
59 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
65 // turn off annoying warning messages
67 AliLog::Instance()->SetGlobalDebugLevel(-20);
70 enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY};
73 // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********
74 UInt_t nrTuningParams = 0;
75 TObjArray paramNames; paramNames.SetOwner(kTRUE);
76 TObjArray paramVals; paramVals.SetOwner(kTRUE);
81 TString idp = "spd_standal_params";
82 status=daqDA_DB_getFile(idp.Data(),paramsFileName);
84 printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),status);
89 paramsFile.open(paramsFileName, ifstream::in);
90 if (paramsFile.fail()) {
91 printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName);
98 if (paramsFile.eof()) break;
100 TString* paramNS = new TString(paramN);
101 TString* paramVS = new TString(paramV);
102 paramNames.AddAtAndExpand((TObject*)paramNS,nrTuningParams);
103 paramVals.AddAtAndExpand((TObject*)paramVS,nrTuningParams);
105 if (paramsFile.eof()) break;
110 // for (UInt_t i=0; i<nrTuningParams; i++) {
111 // printf("Entry %d: N=%s , V=%s\n",i,((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
115 Int_t permstatus = 0;
117 TString idn = "spd_perm_noisy";
118 permstatus=daqDA_DB_getFile(idn.Data(),permNoisyFileName);
120 printf("Failed to get config file %s: status=%d. No additional noisy pixels added.\n",idn.Data(),permstatus);
127 // ********* STEP 1: Produce scan container files (Reference Data). ***********************************
131 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
133 int runNr = atoi(argv[1]);
138 AliITSOnlineSPDscan *scanObj[20];
139 Bool_t bScanInit[20];
140 for (UInt_t eqId=0; eqId<20; eqId++) {
142 bScanInit[eqId]=kFALSE;
144 // header data variables
146 Bool_t halfStaveScanned[20][6];
148 Bool_t dataFormat[20];
150 Bool_t chipPresent[20][6][10];
159 UInt_t dacHigh[20][6];
160 UInt_t dacLow[20][6];
162 Bool_t minTHchipPresent[20][10];
163 // current scan step flag
164 Int_t currentStep[20];
165 for (UInt_t eqId=0; eqId<20; eqId++) currentStep[eqId] = 9999;
167 // loop over run segments
168 for (int segNr=startSeg; segNr<argc; segNr++) {
173 /* define data source : this is argument 1 */
174 status=monitorSetDataSource( argv[segNr] );
176 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
179 /* declare monitoring program */
180 status=monitorDeclareMp("ITS_SPD_CAL");
182 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
185 /* define wait event timeout - 1s max */
187 monitorSetNoWaitNetworkTimeout(1000);
190 struct eventHeaderStruct *event;
191 eventTypeType eventT;
195 /* main loop (infinite) */
198 /* check shutdown condition */
200 if (daqDA_checkShutdown()) {break;}
202 /* get next event (blocking call until timeout) */
203 status=monitorGetEventDynamic((void **)&event);
204 if (status==MON_ERR_EOF) {
205 printf ("End of File detected\n");
206 break; /* end of monitoring file has been reached */
209 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
212 /* retry if got no event */
214 printf("event==NULL\n");
218 eventT=event->eventType;
219 if (eventT == PHYSICS_EVENT){
223 // printf("eventNr %d\n",eventNr);
225 AliRawReader *reader = new AliRawReaderDate((void*)event);
226 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
229 for (UInt_t eqId=0; eqId<20; eqId++) {
232 reader->Select("ITSSPD",eqId,eqId);
234 // Hit Event flags, specific for one event
235 Bool_t hitEventHSIncremented[6];
236 Bool_t hitEventChipIncremented[6][10];
237 for (UInt_t hs=0; hs<6; hs++) {
238 hitEventHSIncremented[hs] = kFALSE;
239 for (UInt_t chip=0; chip<10; chip++) {
240 hitEventChipIncremented[hs][chip] = kFALSE;
244 if (str->ReadCalibHeader()) {
245 // first check the type:
246 if (bScanInit[eqId] && type[eqId]!=str->GetHtype()) {
247 printf("Calib header problem. Type changed (%d -> %d)!\n",type[eqId],str->GetHtype());
251 routerNr[eqId] = str->GetHrouterNr();
252 type[eqId] = str->GetHtype();
253 dataFormat[eqId] = str->GetHdataFormat();
254 triggers[eqId] = str->GetHtriggers();
255 dacStart[eqId] = str->GetHdacStart();
256 dacEnd[eqId] = str->GetHdacEnd();
257 dacStep[eqId] = str->GetHdacStep();
258 dacId[eqId] = str->GetHdacId();
259 rowStart[eqId] = str->GetHrowStart();
260 rowEnd[eqId] = str->GetHrowEnd();
261 rowValue[eqId] = str->GetHrowValue();
262 dacValue[eqId] = str->GetHdacValue();
263 for (UInt_t hs=0; hs<6; hs++) {
264 halfStaveScanned[eqId][hs] = str->GetHhalfStaveScanned(hs);
265 dacHigh[eqId][hs] = str->GetHdacHigh(hs);
266 dacLow[eqId][hs] = str->GetHdacLow(hs);
267 TPAmp[eqId][hs] = str->GetHTPAmp(hs);
268 for (UInt_t chip=0; chip<10; chip++) {
269 chipPresent[eqId][hs][chip] = str->GetHchipPresent(hs,chip);
272 for (UInt_t chip=0; chip<10; chip++) {
273 minTHchipPresent[eqId][chip] = str->GetHminTHchipPresent(chip);
276 currentStep[eqId] = (dacValue[eqId]-dacStart[eqId])/dacStep[eqId];
277 if (type[eqId]==DELAY) {
278 currentStep[eqId]=currentStep[eqId]*2;
279 dacValue[eqId]=dacValue[eqId]*2;
280 if (dacHigh[eqId][0]==128) { // misc_ctrl value
281 currentStep[eqId]=currentStep[eqId]+1;
282 dacValue[eqId]=dacValue[eqId]+1;
287 if (routerNr[eqId]!=eqId) {
288 printf("Router nr problem? Router nr %d != EqID %d\n",routerNr[eqId],eqId);
291 if (!bScanInit[eqId]) {
292 // initialize container object
293 TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
294 switch (type[eqId]) {
297 scanObj[eqId] = new AliITSOnlineSPDscanSingle(fileName.Data());
298 ((AliITSOnlineSPDscanSingle*)scanObj[eqId])->ClearThis();
299 bScanInit[eqId]=kTRUE;
304 scanObj[eqId] = new AliITSOnlineSPDscanMultiple(fileName.Data());
305 scanObj[eqId]->ClearThis();
306 bScanInit[eqId]=kTRUE;
309 scanObj[eqId] = new AliITSOnlineSPDscanMeanTh(fileName.Data());
310 scanObj[eqId]->ClearThis();
311 bScanInit[eqId]=kTRUE;
314 printf("Unknown scan type: %d.\n",type[eqId]);
316 // some multiple scan data
317 if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
318 ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacId(dacId[eqId]);
321 scanObj[eqId]->SetRunNr((UInt_t)runNr);
322 scanObj[eqId]->SetRouterNr(routerNr[eqId]);
323 for (UInt_t hs=0; hs<6; hs++) {
324 scanObj[eqId]->SetHalfStaveScanned(hs,halfStaveScanned[eqId][hs]);
326 scanObj[eqId]->SetType(type[eqId]);
327 scanObj[eqId]->SetDataFormat(dataFormat[eqId]);
328 for (Int_t hs=0; hs<6; hs++) {
330 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
331 Bool_t halfStavePresent = str->GetHalfStavePresent(hs);
332 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
334 for (UInt_t chip=0; chip<10; chip++) {
335 scanObj[eqId]->SetChipPresent(hs,chip,chipPresent[eqId][hs][chip]);
337 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
338 if (halfStavePresent) scanObj[eqId]->SetChipPresent(hs,chip,kTRUE);
339 // remove later when the chip present is set correctly !!!!!!!!!!!!!!!!!!!!!!!!!!!
343 scanObj[eqId]->SetRowStart(rowStart[eqId]);
344 scanObj[eqId]->SetRowEnd(rowEnd[eqId]);
345 scanObj[eqId]->SetDacStart(dacStart[eqId]);
346 scanObj[eqId]->SetDacEnd(dacEnd[eqId]);
347 scanObj[eqId]->SetDacStep(dacStep[eqId]);
350 if (type[eqId]==MINTH) {
351 scanObj[eqId]->SetTriggers(currentStep[eqId],triggers[eqId]);
353 if (type[eqId]==UNIMA || type[eqId]==NOISE) {
354 if (currentStep[eqId]==9999) printf("SPDcalibratorStep1 (eq %d): single step\n",eqId);
357 if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
358 ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacValue(currentStep[eqId],dacValue[eqId]);
359 if (type[eqId]==DELAY) {
360 printf("SPDcalibratorStep1 (eq %d): DAC %d/%d , step %d\n",eqId,dacValue[eqId]/2,dacHigh[eqId][0],currentStep[eqId]);
363 printf("SPDcalibratorStep1 (eq %d): DAC %d , step %d\n",eqId,dacValue[eqId],currentStep[eqId]);
366 if (type[eqId]==MEANTH) {
367 for (Int_t hs=0; hs<6; hs++) {
368 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacLow(currentStep[eqId],hs,dacLow[eqId][hs]);
369 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacHigh(currentStep[eqId],hs,dacHigh[eqId][hs]);
370 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetTPAmp(currentStep[eqId],hs,TPAmp[eqId][hs]);
377 if (bScanInit[eqId]) {
378 while (str->Next()) {
379 UInt_t hs = str->GetHalfStaveNr();
380 UInt_t chip = str->GetChipAddr();
381 //***remove last condition when minthpresent put correctly in calib header?
383 if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
385 if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
387 //*************************************************************************
388 scanObj[eqId]->IncrementHits(currentStep[eqId],hs,chip,str->GetChipCol(),str->GetChipRow());
390 if (!hitEventHSIncremented[hs]) {
391 scanObj[eqId]->IncrementHitEventsTot(currentStep[eqId],hs);
392 hitEventHSIncremented[hs]=kTRUE;
395 if (!hitEventChipIncremented[hs][chip]) {
396 scanObj[eqId]->IncrementHitEvents(currentStep[eqId],hs,chip);
397 hitEventChipIncremented[hs][chip]=kTRUE;
403 if (type[eqId]!=MINTH) { // for minth, triggers are set from header info
404 scanObj[eqId]->IncrementTriggers(currentStep[eqId]);
421 status = monitorLogout();
423 printf("monitorLogout() failed : %s\n",monitorDecodeError(status));
428 daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
430 printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
435 // clean up scan objects
436 for (UInt_t eqId=0; eqId<20; eqId++) {
437 if (scanObj[eqId]!=NULL) delete scanObj[eqId];
444 // ********* STEP 2: Analyze scan container files. ************************************************
446 // clear noisyToFXS and DCSconfigToFXS dirs:
447 TString command = Form("cd %s; rm -f *",saveDirNoisyToFXS);
448 system(command.Data());
449 TString command2 = Form("cd %s; rm -f *",saveDirDCSconfigToFXS);
450 system(command2.Data());
451 UInt_t nrNoisyFilesProduced=0;
452 UInt_t nrDCSconfigFilesProduced=0;
454 AliITSOnlineSPDscanAnalyzer *analyzer;
456 // *** *** *** start loop over equipments (eq_id)
457 for (int eqId=0; eqId<20; eqId++) {
459 // create analyzer for this eq
460 TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
461 analyzer = new AliITSOnlineSPDscanAnalyzer(fileName.Data());
463 // configure analyzer with tuning parameters etc:
464 for (UInt_t i=0; i<nrTuningParams; i++) {
465 analyzer->SetParam(((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
468 UInt_t type = analyzer->GetType();
469 UInt_t dacId = analyzer->GetDacId();
470 UInt_t routerNr = analyzer->GetRouterNr();
473 printf("SPD calibrator Step2: eqId %d, type %d, dacId %d\n",eqId,type,dacId);
475 else printf("SPD calibrator Step2: eqId %d type %d\n",eqId,type);
480 // algorithms for the different types of scans:
486 else if (type==NOISE) {
487 if (analyzer->ProcessNoisyPixels(saveDirNoisy)) {
488 // init dcs config text file
489 TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
491 dcsfile.open(dcsConfigFileName.Data());
492 dcsfile << "[SPD SCAN]\n";
493 dcsfile << "RunNumber=" << runNr << "\n";
494 dcsfile << "Type=" << type << "\n";
495 dcsfile << "Router=" << routerNr << "\n";
496 dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
497 dcsfile << "[NOISY]\n";
498 nrDCSconfigFilesProduced++;
499 for (UInt_t iModule=0; iModule<12; iModule++) {
500 UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,iModule);
501 AliITSOnlineCalibrationSPDhandler *handler = analyzer->GetOnlineCalibrationHandler(module);
502 if (permstatus==0) { // add permanent noisy list
503 UInt_t newNoisy = handler->ReadNoisyFromText(permNoisyFileName);
505 printf("%d additional noisy pixels added from permanent list.\n",newNoisy);
508 if (analyzer->SaveDeadNoisyPixels(module,saveDirNoisy)) {
509 UInt_t nrNoisy = handler->GetNrNoisy(module);
510 UInt_t headkey=20*10*6;
511 for (UInt_t ind=0; ind<nrNoisy; ind++) {
512 UInt_t newkey = handler->GetNoisyEqIdAt(module,ind)*10*6 +
513 handler->GetNoisyHSAt(module,ind)*10 +
514 handler->GetNoisyChipAt(module,ind);
515 if (newkey!=headkey) { // print eqId,hs,chip_header
517 dcsfile << "-" << newkey/(6*10) << "," << (newkey%(6*10))/10 << "," << (newkey%(6*10))%10 << "\n";
519 dcsfile << handler->GetNoisyColAt(module,ind) << "," << handler->GetNoisyRowAt(module,ind) << "\n";
521 nrNoisyFilesProduced++;
522 TString command = Form("cp %s/SPD_DeadNoisy_%d.root %s/.",saveDirNoisy,module,saveDirNoisyToFXS);
523 system(command.Data());
530 else if (type==MINTH || (type==DAC && dacId==39)) {
531 // init dcs config text file
532 TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
534 dcsfile.open(dcsConfigFileName.Data());
535 dcsfile << "[SPD SCAN]\n";
536 dcsfile << "RunNumber=" << runNr << "\n";
537 dcsfile << "Type=" << type << "\n";
538 dcsfile << "Router=" << routerNr << "\n";
539 dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
540 dcsfile << "[DACvalues]\n";
541 nrDCSconfigFilesProduced++;
542 TString ofileName = Form("%s/minth_eq_%d.txt",saveDirParameters,eqId);
544 ofile.open (ofileName.Data());
545 for (UInt_t hs=0; hs<6; hs++) {
546 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
548 if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
549 minTh = analyzer->GetMinTh(hs,chipNr);
551 dcsfile << "39," << eqId << "," << hs << "," << chipNr << "=" << minTh << "\n";
554 printf("MinTh failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
566 else if (type==DELAY) {
567 // init dcs config text file
568 TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
570 dcsfile.open(dcsConfigFileName.Data());
571 dcsfile << "[SPD SCAN]\n";
572 dcsfile << "RunNumber=" << runNr << "\n";
573 dcsfile << "Type=" << type << "\n";
574 dcsfile << "Router=" << routerNr << "\n";
575 dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
576 dcsfile << "[DACvalues]\n";
577 nrDCSconfigFilesProduced++;
578 TString ofileName = Form("%s/delay_eq_%d.txt",saveDirParameters,eqId);
580 ofile.open (ofileName.Data());
581 for (UInt_t hs=0; hs<6; hs++) {
582 for (UInt_t chipNr=0; chipNr<10; chipNr++) {
583 Int_t clockCycle = -1;
584 Int_t delayCtrl = -1;
586 if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
587 clockCycle = analyzer->GetDelay(hs,chipNr);
588 delayCtrl = clockCycle/2;
590 if (clockCycle!=-1) {
591 if (clockCycle%2==1) miscCtrl = 128;
592 dcsfile << "42," << eqId << "," << hs << "," << chipNr << "=" << delayCtrl << "\n";
593 dcsfile << "43," << eqId << "," << hs << "," << chipNr << "=" << miscCtrl << "\n";
596 printf("Delay failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
599 ofile << delayCtrl << "/" << miscCtrl;
611 daqDA_progressReport((unsigned int)(50+(eqId+1)*2.5));
613 printf("progress: %d\n",(unsigned int)(50+(eqId+1)*2.5));
617 // *** *** *** end loop over equipments (eq_id)
620 if (nrNoisyFilesProduced>0) {
621 // send a tared file of all new noisy maps
622 TString command = Form("cd %s; tar -cf noisy.tar *",saveDirNoisyToFXS);
623 printf("\n\n%s\n\n",command.Data());
624 system(command.Data());
626 TString fileName = Form("%s/noisy.tar",saveDirNoisyToFXS);
627 TString id = "SPD_noisy";
628 Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
630 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
636 if (nrDCSconfigFilesProduced>0) {
637 // send a tared file of all the dcsConfig text files
638 TString command = Form("cd %s; tar -cf dcsConfig.tar *",saveDirDCSconfigToFXS);
639 // printf("\n\n%s\n\n",command.Data());
640 system(command.Data());
642 TString fileName = Form("%s/dcsConfig.tar",saveDirDCSconfigToFXS);
643 TString id = "SPD_dcsConfig";
644 Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
646 printf("Failed to export file %s , status %d\n",fileName.Data(),status);