2 Contact: henrik.tydesjo@cern.ch
3 Link: tydes.home.cern.ch/tydes/doc/CalibrationOverview/CalibrationAlgorithms/
6 Number of events needed: Depending on muliplicity per event
7 Input Files: spd_physics_params , ./calibResults/DeadReferenceTmp/* , ./calibResults/DeadToFXS/*
8 Output Files: ./calibResults/NoisyReference/* , ./calibResults/DeadReference/* , ./calibResults/NoisyToFXS/* , ./calibResults/DeadReferenceTmp/*,./calibResults/DeadToFXS/*
9 Trigger types used: PHYSICS
12 ////////////////////////////////////////////////////////////////////////////////
13 // This program can be compiled in two modes. //
15 // 1. Online. With the DAQ DA framework. This is the default operating mode. //
17 // 2. Offline. Without the DAQ DA framework. Define the SPD_DA_OFF //
18 // environment var. Call this program with the name of the executable //
19 // followed by the runNr and the data files to process. //
21 ////////////////////////////////////////////////////////////////////////////////
30 #include "AliRawReaderDate.h"
31 #include "AliITSRawStreamSPD.h"
32 #include "AliITSOnlineSPDphys.h"
33 #include "AliITSOnlineSPDphysAnalyzer.h"
34 #include "AliITSOnlineCalibrationSPDhandler.h"
40 #include <TPluginManager.h>
41 #include <TObjArray.h>
42 #include <TObjString.h>
46 int main(int argc, char **argv) {
48 printf("SPD DA ERROR: Wrong number of arguments\n");
52 // directory structure, hard coded
53 char *saveDirDead = "./calibResults/Dead"; // may NOT delete content
54 char *saveDirDeadToFXS = "./calibResults/DeadToFXS"; // may delete content
55 char *saveDirDeadRef = "./calibResults/DeadReference"; // may delete content
56 char *saveDirDeadRefTmp = "./calibResults/DeadReferenceTmp"; // may NOT delete content
57 char *saveDirNoisyToFXS = "./calibResults/NoisyToFXS"; // may delete content
58 char *saveDirNoisyRef = "./calibResults/NoisyReference"; // may delete content
59 char *saveDirIdsToFXS = "./calibResults/IdsToFXS"; // may delete content
60 char *configFilesDir = "./configFiles"; // may delete content
61 // make sure the directory structure is put up correctly:
62 system("mkdir ./calibResults >& /dev/null");
63 system("mkdir ./calibResults/Dead >& /dev/null");
64 system("mkdir ./calibResults/DeadToFXS >& /dev/null");
65 system("mkdir ./calibResults/DeadReference >& /dev/null");
66 system("mkdir ./calibResults/DeadReferenceTmp >& /dev/null");
67 system("mkdir ./calibResults/NoisyToFXS >& /dev/null");
68 system("mkdir ./calibResults/NoisyReference >& /dev/null");
69 system("mkdir ./calibResults/IdsToFXS >& /dev/null");
70 system("mkdir ./configFiles >& /dev/null");
71 // parameters config file
72 TString paramsFileName = Form("%s/physics_params.txt",configFilesDir);
74 // This line is needed in case of a stand-alone application w/o
75 // $ROOTSYS/etc/system.rootrc file
76 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
82 // turn off annoying warning messages
84 AliLog::Instance()->SetGlobalDebugLevel(-20);
87 // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********
88 UInt_t nrTuningParams = 0;
89 TObjArray paramNames; paramNames.SetOwner(kTRUE);
90 TObjArray paramVals; paramVals.SetOwner(kTRUE);
95 TString idp = "spd_physics_params";
96 par_status=daqDA_DB_getFile(idp.Data(),paramsFileName.Data());
98 printf("SPD DA: Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),par_status);
103 paramsFile.open(paramsFileName.Data(), ifstream::in);
104 if (paramsFile.fail()) {
105 printf("SPD DA: No config file (%s) present. Using default tuning parameters.\n",paramsFileName.Data());
111 paramsFile >> paramN;
112 if (paramsFile.eof()) break;
113 paramsFile >> paramV;
114 paramNames.AddAtAndExpand(new TObjString(paramN),nrTuningParams);
115 paramVals.AddAtAndExpand(new TObjString(paramV),nrTuningParams);
117 if (paramsFile.eof()) break;
122 // for (UInt_t i=0; i<nrTuningParams; i++) {
123 // // printf("Entry %d: N=%s , V=%s\n",i,((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
124 // printf("Entry %d: N=%s , V=%s\n",i,((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
131 // create calibration handler (needed already at step 1 in order to fill which eq,hs,chips are active
132 AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
134 // Read silent=dead+inactive info from previous calibrations
136 // remove previous files and read from DB instead
137 TString commandRemoveDead = Form("cd %s; rm -f *",saveDirDead);
138 system(commandRemoveDead.Data());
139 for (UInt_t eq=0; eq<20; eq++) {
140 Int_t getPreviousDead_status = 0;
141 TString idpd = Form("spd_previous_dead_%d",eq);
142 TString fileName = Form("%s/SPD_Dead_%d.root",saveDirDead,eq);
143 getPreviousDead_status = daqDA_DB_getFile(idpd.Data(),fileName.Data());
144 // printf("daqDA_DB_getFile(%s,%s)\n",idpd.Data(),fileName.Data());
146 printf("SPD DA: Failed to get dead file %s: status=%d. Dead search will start from zero for this eq.\n",idpd.Data(),getPreviousDead_status);
150 handler->SetFileLocation(saveDirDead);
151 handler->ReadSilentFromFiles();
152 printf("SPD DA: Number of single dead pixels from previous runs: %d\n",handler->GetNrDead());
157 // ********* STEP 1: Produce phys container files (Reference Data). ***********************************
160 if (getenv("DATE_RUN_NUMBER")==0) {
161 printf("SPD DA ERROR: DATE_RUN_NUMBER not properly set.\n");
164 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
166 int runNr = atoi(argv[1]);
172 AliITSOnlineSPDphys *physObj[20];
173 Bool_t bPhysInit[20];
174 for (UInt_t eq=0; eq<20; eq++) {
176 bPhysInit[eq]=kFALSE;
182 // loop over run segments in case of offline mode
184 for (int segNr=startSeg; segNr<argc; segNr++) {
189 /* define data source : */
191 status=monitorSetDataSource( argv[1] ); // should be "^SPD" in order to get full detector online
193 status=monitorSetDataSource( argv[segNr] );
196 printf("SPD DA ERROR: monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
199 /* declare monitoring program */
200 status=monitorDeclareMp("ITS_SPD_PHYS");
202 printf("SPD DA ERROR: monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
205 /* define wait event timeout - 1s max */
207 monitorSetNoWaitNetworkTimeout(1000);
210 /* main loop (infinite) */
213 struct eventHeaderStruct *event;
214 eventTypeType eventT;
216 /* check shutdown condition */
218 if (daqDA_checkShutdown()) {break;}
221 /* get next event (blocking call until timeout) */
222 status=monitorGetEventDynamic((void **)&event);
223 if (status==MON_ERR_EOF) {
224 printf ("SPD DA: End of File detected\n");
225 break; /* end of monitoring file has been reached */
229 printf("SPD DA: monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
233 /* retry if got no event */
238 eventT=event->eventType;
239 if (eventT == PHYSICS_EVENT){
241 // printf("eventNr %d\n",eventNr);
243 AliRawReader *reader = new AliRawReaderDate((void*)event);
244 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
246 while (str->Next()) {
248 Int_t eq = reader->GetDDLID();
249 // check that this hs is active in handler object
250 if (!(handler->IsActiveEq(eq))) {
251 printf("SPD DA: Found Eq (%d) , previously inactive\n",eq);
252 handler->ActivateEq(eq);
254 if (eq>=0 && eq<20) {
255 if (!bPhysInit[eq]) { // this code is duplicated for the moment... (see also below)
256 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
257 physObj[eq] = new AliITSOnlineSPDphys(fileName.Data());
258 physObj[eq]->AddRunNr(runNr);
259 physObj[eq]->SetEqNr(eq);
263 UInt_t hs = str->GetHalfStaveNr();
264 // check that this hs is active in handler object
265 if (!(handler->IsActiveHS(eq,hs))) {
266 printf("SPD DA: Found HS (%d,%d) , previously inactive\n",eq,hs);
267 handler->ActivateHS(eq,hs);
269 UInt_t chip = str->GetChipAddr();
270 // check that this chip is active in handler object
271 if (!(handler->IsActiveChip(eq,hs,chip))) {
272 printf("SPD DA: Found Chip (%d,%d,%d) , previously inactive\n",eq,hs,chip);
273 handler->ActivateChip(eq,hs,chip);
275 physObj[eq]->IncrementHits(hs,chip,str->GetChipCol(),str->GetChipRow());
281 // check which eq and hs are active, for first event only
283 for (UInt_t eq=0; eq<20; eq++) {
284 // activate Eq and HSs in handler object
285 if (str->IsActiveEq(eq)) {
286 handler->ActivateEq(eq);
287 for (UInt_t hs=0; hs<6; hs++) {
288 if (str->IsActiveHS(eq,hs)) {
289 handler->ActivateHS(eq,hs);
290 for (UInt_t chip=0; chip<10; chip++) {
291 if (str->IsActiveChip(eq,hs,chip)) {
292 handler->ActivateChip(eq,hs,chip);
295 handler->ActivateChip(eq,hs,chip,kFALSE);
300 handler->ActivateHS(eq,hs,kFALSE);
303 if (!bPhysInit[eq]) { // this code is duplicated for the moment... (see also above)
304 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
305 physObj[eq] = new AliITSOnlineSPDphys(fileName.Data());
306 physObj[eq]->AddRunNr(runNr);
307 physObj[eq]->SetEqNr(eq);
312 handler->ActivateEq(eq,kFALSE);
318 for (UInt_t eq=0; eq<20; eq++) {
320 physObj[eq]->IncrementNrEvents();
338 printf("SPD DA: progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
342 // clean up phys objects (also saves them)
343 for (UInt_t eq=0; eq<20; eq++) {
344 if (physObj[eq]!=NULL) delete physObj[eq];
347 printf("SPD DA: %d events collected for this run.\n",eventNr);
352 // ********* STEP 2: Analyze phys container files. ************************************************
354 // time_t timeStamp = time(NULL);
355 // printf("*** Start step2 , %d\n",time(NULL) - timeStamp);
357 // clear noisyToFXS dir:
359 command = Form("cd %s; rm -f *",saveDirNoisyToFXS);
360 system(command.Data());
361 // clear deadToFXS dir:
362 command = Form("cd %s; rm -f *",saveDirDeadToFXS);
363 system(command.Data());
366 UInt_t firstRunNrDead = runNr;
369 UInt_t nrEnoughStatNoisy = 0;
370 UInt_t nrEqActiveNoisy = 0;
371 Bool_t eqActiveNoisy[20];
373 // *** *** *** start loop over equipments (eq_id)
374 for (UInt_t eq=0; eq<20; eq++) {
375 eqActiveNoisy[eq] = kFALSE;
377 // create analyzer for this eq
378 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
379 AliITSOnlineSPDphysAnalyzer *noisyAnalyzer = new AliITSOnlineSPDphysAnalyzer(fileName.Data(),handler);
381 // check data in container
382 if (noisyAnalyzer->GetEqNr() != eq) {
383 if (noisyAnalyzer->GetEqNr() != 999) {
384 printf("SPD DA ERROR: Mismatching EqId in Container data and filename (%d!=%d). Skipping eq.\n",
385 noisyAnalyzer->GetEqNr(),eq);
387 delete noisyAnalyzer;
392 eqActiveNoisy[eq] = kTRUE;
394 // configure analyzer with tuning parameters etc:
395 for (UInt_t i=0; i<nrTuningParams; i++) {
396 noisyAnalyzer->SetParam(((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
399 printf("SPD DA: SPD phys STEP 2: Noisy search for eq %d\n",eq);
401 // search for noisy pixels:
402 nrEnoughStatNoisy += noisyAnalyzer->ProcessNoisyPixels();
404 // copy this phys obj to temporary dead reference dir to process after noisy search
405 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
406 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
407 physObj->AddPhys(noisyAnalyzer->GetOnlinePhys());
408 if (physObj->GetNrRuns()>0) {
409 UInt_t firstRunNr = physObj->GetRunNr(0);
410 if (firstRunNrDead>firstRunNr) {
411 firstRunNrDead=firstRunNr;
414 // remove noisy pixels from dead hitmap
415 for (UInt_t hs=0; hs<6; hs++) {
416 for (UInt_t chip=0; chip<10; chip++) {
417 for (UInt_t ind=0; ind<handler->GetNrNoisyC(eq,hs,chip); ind++) {
418 UInt_t col = handler->GetNoisyColAtC(eq,hs,chip,ind);
419 UInt_t row = handler->GetNoisyRowAtC(eq,hs,chip,ind);
420 physObj->AddHits(hs,chip,col,row,-noisyAnalyzer->GetOnlinePhys()->GetHits(hs,chip,col,row));
426 delete noisyAnalyzer;
429 daqDA_progressReport((unsigned int)((eq+1)*2.5));
431 printf("SPD DA: progress: %d\n",(unsigned int)(50+(eq+1)*1.25));
434 // *** *** *** end loop over equipments (eq_id)
436 printf("SPD DA: Noisy search finished. %d noisy pixels found. %d chips had enough statistics.\n",
437 handler->GetNrNoisy(),nrEnoughStatNoisy);
438 handler->SetFileLocation(saveDirNoisyToFXS);
439 UInt_t nrNoisyFilesProduced = handler->WriteNoisyToFiles();
447 UInt_t nrEnoughStatChips = 0;
448 UInt_t nrDeadChips = 0;
449 UInt_t nrInefficientChips = 0;
450 UInt_t nrEqActiveDead = 0;
451 Bool_t eqActiveDead[20];
453 // *** *** *** start loop over equipments (eq_id)
454 for (UInt_t eq=0; eq<20; eq++) {
455 eqActiveDead[eq] = kFALSE;
457 // setup analyzer for dead search
458 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
459 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
460 AliITSOnlineSPDphysAnalyzer* deadAnalyzer = new AliITSOnlineSPDphysAnalyzer(physObj,handler);
461 // check data in container
462 if (deadAnalyzer->GetEqNr() != eq) {
463 if (deadAnalyzer->GetEqNr() != 999) {
464 printf("SPD DA ERROR: Mismatching EqId in Dead Container data and filename (%d!=%d). Skipping eq.\n",
465 deadAnalyzer->GetEqNr(),eq);
468 nrDeadChips+=60; // since this eq is inactive...
473 eqActiveDead[eq] = kTRUE;
475 // configure analyzer with tuning parameters etc:
476 for (UInt_t i=0; i<nrTuningParams; i++) {
477 deadAnalyzer->SetParam(((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
480 printf("SPD DA: SPD phys STEP 2: Dead search for eq %d\n",eq);
482 // search for dead pixels:
483 nrEnoughStatChips += deadAnalyzer->ProcessDeadPixels();
484 nrDeadChips += deadAnalyzer->GetNrDeadChips();
485 nrInefficientChips += deadAnalyzer->GetNrInefficientChips();
491 daqDA_progressReport((unsigned int)(50+(eq+1)*2.5));
493 printf("SPD DA: progress: %d\n",(unsigned int)(75+(eq+1)*1.25));
496 // *** *** *** end loop over equipments (eq)
501 printf("SPD DA: Dead search finished.\n");
502 printf("SPD DA: %d single dead pixels , %d dead or inactive pixels in total.\n",handler->GetNrDead(),handler->GetNrSilent());
503 printf("SPD DA: %d chips had enough statistics. %d chips are dead. %d chips are inefficient.\n",nrEnoughStatChips,nrDeadChips,nrInefficientChips);
504 handler->SetFileLocation(saveDirDead);
505 handler->WriteSilentToFilesAlways();
506 handler->SetFileLocation(saveDirDeadToFXS);
507 handler->WriteSilentToFilesAlways();
510 printf("SPD DA: Opening id list file\n");
511 TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
513 idsFXSfile.open(idsFXSFileName.Data());
516 // store dead+inactive pixels in DB, used as starting point for later runs
518 for (UInt_t eq=0; eq<20; eq++) {
519 Int_t storePreviousDead_status = 0;
520 TString idpd = Form("spd_previous_dead_%d",eq);
521 TString fileName = Form("%s/SPD_Dead_%d.root",saveDirDead,eq);
522 storePreviousDead_status = daqDA_DB_storeFile(fileName.Data(),idpd.Data());
523 // printf("daqDA_DB_storeFile(%s,%s)\n",fileName.Data(),idpd.Data());
524 if (storePreviousDead_status) {
525 printf("SPD DA ERROR: Failed to store dead file %s in daqDetDB: status=%d.\n",idpd.Data(),storePreviousDead_status);
531 // send (dead) reference data for this run to FXS - only if there is no chip in category "needsMoreStat"
532 if (nrEnoughStatChips>0 && nrEnoughStatChips+nrDeadChips+nrInefficientChips == 1200) {
533 printf("SPD DA: Dead calibration is complete.\n");
534 printf("SPD DA: Preparing dead reference data\n");
535 // send reference data for dead pixels to FXS
536 TString tarFiles = "";
537 for (UInt_t eq=0; eq<20; eq++) {
538 if (eqActiveDead[eq]) {
539 printf("SPD DA: Preparing dead pixels for eq %d\n",eq);
540 // move file to ref dir
541 TString fileName = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
542 TString newFileName = Form("%s/SPDphys_dead_run_%d_%d_eq_%d.root",saveDirDeadRef,firstRunNrDead,runNr,eq);
543 TString command = Form("mv -f %s %s",fileName.Data(),newFileName.Data());
544 system(command.Data());
546 tarFiles.Append(Form("SPDphys_dead_run_%d_%d_eq_%d.root ",firstRunNrDead,runNr,eq));
549 if (tarFiles.Length() > 0) { // make sure there are any files to send
550 TString send_command = Form("cd %s; tar -cf ref_phys_dead.tar %s",saveDirDeadRef,tarFiles.Data());
551 system(send_command.Data());
552 TString fileName = Form("%s/ref_phys_dead.tar",saveDirDeadRef);
553 TString id = "SPD_ref_phys_dead";
555 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
557 printf("SPD DA ERROR: Failed to export file %s , status %d\n",fileName.Data(),status);
561 idsFXSfile << Form("%s\n",id.Data());
567 // send (noisy) reference data for this run to FXS
568 printf("SPD DA: Preparing noisy reference data\n");
569 TString tarFiles = "";
570 for (UInt_t eq=0; eq<20; eq++) {
571 if (eqActiveNoisy[eq]) {
572 tarFiles.Append(Form("SPDphys_run_%d_eq_%d.root ",runNr,eq));
575 if (tarFiles.Length() > 0) { // make sure there are any files to send
576 TString send_command = Form("cd %s; tar -cf ref_phys.tar %s",saveDirNoisyRef,tarFiles.Data());
577 system(send_command.Data());
578 TString fileName = Form("%s/ref_phys.tar",saveDirNoisyRef);
579 TString id = "SPD_ref_phys";
581 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
583 printf("SPD DA ERROR: Failed to export file %s , status %d\n",fileName.Data(),status);
587 idsFXSfile << Form("%s\n",id.Data());
592 // send dead pixels to FXS
593 printf("SPD DA: Preparing dead files for FXS\n");
594 // send a tared file of all the dead files
595 TString send_command = Form("cd %s; tar -cf dead_phys.tar *",saveDirDeadToFXS);
596 // printf("\n\n%s\n\n",command.Data());
597 system(send_command.Data());
598 TString fileName = Form("%s/dead_phys.tar",saveDirDeadToFXS);
599 TString id = "SPD_phys_dead";
601 Int_t send_status = daqDA_FES_storeFile(fileName.Data(),id.Data());
602 if (send_status!=0) {
603 printf("SPD DA ERROR: Failed to export file %s , status %d\n",fileName.Data(),send_status);
607 idsFXSfile << Form("%s\n",id.Data());
611 // send noisy pixels to FXS
612 if (nrNoisyFilesProduced > 0) { // make sure there is at least one file created
613 printf("SPD DA: Preparing noisy files for FXS\n");
614 // send a tared file of all the noisy files
615 TString command = Form("cd %s; tar -cf noisy_phys.tar *",saveDirNoisyToFXS);
616 // printf("\n\n%s\n\n",command.Data());
617 system(command.Data());
618 TString fileName = Form("%s/noisy_phys.tar",saveDirNoisyToFXS);
619 TString id = "SPD_phys_noisy";
621 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
623 printf("SPD DA ERROR: Failed to export file %s , status %d\n",fileName.Data(),status);
627 idsFXSfile << Form("%s\n",id.Data());
632 // send ids file to FXS
636 status = daqDA_FES_storeFile(idsFXSFileName.Data(),id.Data());
638 printf("SPD DA ERROR: Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);
648 // printf("*** End step2 , %d\n",time(NULL) - timeStamp);