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("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("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("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();
133 // Read silent=dead+inactive info from previous calibrations
134 handler->SetFileLocation(saveDirDead);
135 handler->ReadSilentFromFiles();
136 printf("Nr dead pixels before: %d\n",handler->GetNrDead());
141 // ********* STEP 1: Produce phys container files (Reference Data). ***********************************
144 if (getenv("DATE_RUN_NUMBER")==0) {
145 printf("DATE_RUN_NUMBER not properly set.\n");
148 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
150 int runNr = atoi(argv[1]);
156 AliITSOnlineSPDphys *physObj[20];
157 Bool_t bPhysInit[20];
158 for (UInt_t eq=0; eq<20; eq++) {
160 bPhysInit[eq]=kFALSE;
164 // loop over run segments in case of offline mode
166 for (int segNr=startSeg; segNr<argc; segNr++) {
171 /* define data source : */
173 status=monitorSetDataSource( argv[1] ); // should be "^SPD" in order to get full detector online
175 status=monitorSetDataSource( argv[segNr] );
178 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
181 /* declare monitoring program */
182 status=monitorDeclareMp("ITS_SPD_PHYS");
184 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
187 /* define wait event timeout - 1s max */
189 monitorSetNoWaitNetworkTimeout(1000);
194 /* main loop (infinite) */
197 struct eventHeaderStruct *event;
198 eventTypeType eventT;
200 /* check shutdown condition */
202 if (daqDA_checkShutdown()) {break;}
205 /* get next event (blocking call until timeout) */
206 status=monitorGetEventDynamic((void **)&event);
207 if (status==MON_ERR_EOF) {
208 printf ("End of File detected\n");
209 break; /* end of monitoring file has been reached */
213 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
217 /* retry if got no event */
222 eventT=event->eventType;
223 if (eventT == PHYSICS_EVENT){
225 // printf("eventNr %d\n",eventNr);
227 AliRawReader *reader = new AliRawReaderDate((void*)event);
228 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
230 while (str->Next()) {
232 Int_t eq = reader->GetDDLID();
233 // check that this hs is active in handler object
234 if (!(handler->IsActiveEq(eq))) {
235 printf("Info: Found Eq (%d) , previously inactive\n",eq);
236 handler->ActivateEq(eq);
238 if (eq>=0 && eq<20) {
239 if (!bPhysInit[eq]) { // this code is duplicated for the moment... (see also below)
240 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
241 physObj[eq] = new AliITSOnlineSPDphys(fileName.Data());
242 physObj[eq]->AddRunNr(runNr);
243 physObj[eq]->SetEqNr(eq);
247 UInt_t hs = str->GetHalfStaveNr();
248 // check that this hs is active in handler object
249 if (!(handler->IsActiveHS(eq,hs))) {
250 printf("Info: Found HS (%d,%d) , previously inactive\n",eq,hs);
251 handler->ActivateHS(eq,hs);
253 UInt_t chip = str->GetChipAddr();
254 // check that this chip is active in handler object
255 if (!(handler->IsActiveChip(eq,hs,chip))) {
256 printf("Info: Found Chip (%d,%d,%d) , previously inactive\n",eq,hs,chip);
257 handler->ActivateChip(eq,hs,chip);
259 physObj[eq]->IncrementHits(hs,chip,str->GetChipCol(),str->GetChipRow());
265 // check which eq and hs are active, for first event only
267 for (UInt_t eq=0; eq<20; eq++) {
268 // activate Eq and HSs in handler object
269 if (str->IsActiveEq(eq)) {
270 handler->ActivateEq(eq);
271 for (UInt_t hs=0; hs<6; hs++) {
272 if (str->IsActiveHS(eq,hs)) {
273 handler->ActivateHS(eq,hs);
274 for (UInt_t chip=0; chip<10; chip++) {
275 if (str->IsActiveChip(eq,hs,chip)) {
276 handler->ActivateChip(eq,hs,chip);
279 handler->ActivateChip(eq,hs,chip,kFALSE);
284 handler->ActivateHS(eq,hs,kFALSE);
287 if (!bPhysInit[eq]) { // this code is duplicated for the moment... (see also above)
288 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
289 physObj[eq] = new AliITSOnlineSPDphys(fileName.Data());
290 physObj[eq]->AddRunNr(runNr);
291 physObj[eq]->SetEqNr(eq);
296 handler->ActivateEq(eq,kFALSE);
302 for (UInt_t eq=0; eq<20; eq++) {
304 physObj[eq]->IncrementNrEvents();
322 printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
326 // clean up phys objects (also saves them)
327 for (UInt_t eq=0; eq<20; eq++) {
328 if (physObj[eq]!=NULL) delete physObj[eq];
335 // ********* STEP 2: Analyze phys container files. ************************************************
337 // time_t timeStamp = time(NULL);
338 // printf("*** Start step2 , %d\n",time(NULL) - timeStamp);
340 // clear noisyToFXS dir:
342 command = Form("cd %s; rm -f *",saveDirNoisyToFXS);
343 system(command.Data());
344 // clear deadToFXS dir:
345 command = Form("cd %s; rm -f *",saveDirDeadToFXS);
346 system(command.Data());
349 UInt_t firstRunNrDead = runNr;
352 UInt_t nrEnoughStatNoisy = 0;
353 UInt_t nrEqActiveNoisy = 0;
354 Bool_t eqActiveNoisy[20];
356 // *** *** *** start loop over equipments (eq_id)
357 for (UInt_t eq=0; eq<20; eq++) {
358 eqActiveNoisy[eq] = kFALSE;
360 // create analyzer for this eq
361 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
362 AliITSOnlineSPDphysAnalyzer *noisyAnalyzer = new AliITSOnlineSPDphysAnalyzer(fileName.Data(),handler);
364 // check data in container
365 if (noisyAnalyzer->GetEqNr() != eq) {
366 if (noisyAnalyzer->GetEqNr() != 999) {
367 printf("Error: Mismatching EqId in Container data and filename (%d!=%d). Skipping.\n",
368 noisyAnalyzer->GetEqNr(),eq);
370 delete noisyAnalyzer;
375 eqActiveNoisy[eq] = kTRUE;
377 // configure analyzer with tuning parameters etc:
378 for (UInt_t i=0; i<nrTuningParams; i++) {
379 noisyAnalyzer->SetParam(((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
382 printf("SPD phys STEP 2: Noisy search for eq %d\n",eq);
384 // search for noisy pixels:
385 nrEnoughStatNoisy += noisyAnalyzer->ProcessNoisyPixels();
387 // copy this phys obj to temporary dead reference dir to process after noisy search
388 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
389 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
390 physObj->AddPhys(noisyAnalyzer->GetOnlinePhys());
391 if (physObj->GetNrRuns()>0) {
392 UInt_t firstRunNr = physObj->GetRunNr(0);
393 if (firstRunNrDead>firstRunNr) {
394 firstRunNrDead=firstRunNr;
397 // remove noisy pixels from dead hitmap
398 for (UInt_t hs=0; hs<6; hs++) {
399 for (UInt_t chip=0; chip<10; chip++) {
400 for (UInt_t ind=0; ind<handler->GetNrNoisyC(eq,hs,chip); ind++) {
401 UInt_t col = handler->GetNoisyColAtC(eq,hs,chip,ind);
402 UInt_t row = handler->GetNoisyRowAtC(eq,hs,chip,ind);
403 physObj->AddHits(hs,chip,col,row,-noisyAnalyzer->GetOnlinePhys()->GetHits(hs,chip,col,row));
409 delete noisyAnalyzer;
412 daqDA_progressReport((unsigned int)((eq+1)*2.5));
414 printf("progress: %d\n",(unsigned int)(50+(eq+1)*1.25));
417 // *** *** *** end loop over equipments (eq_id)
419 printf("Noisy search finished. %d noisy pixels found. %d chips had enough statistics.\n",
420 handler->GetNrNoisy(),nrEnoughStatNoisy);
421 handler->SetFileLocation(saveDirNoisyToFXS);
422 handler->WriteNoisyToFiles();
430 UInt_t nrEnoughStatChips = 0;
431 UInt_t nrDeadChips = 0;
432 UInt_t nrInefficientChips = 0;
433 UInt_t nrEqActiveDead = 0;
434 Bool_t eqActiveDead[20];
436 // *** *** *** start loop over equipments (eq_id)
437 for (UInt_t eq=0; eq<20; eq++) {
438 eqActiveDead[eq] = kFALSE;
440 // setup analyzer for dead search
441 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
442 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
443 AliITSOnlineSPDphysAnalyzer* deadAnalyzer = new AliITSOnlineSPDphysAnalyzer(physObj,handler);
444 // check data in container
445 if (deadAnalyzer->GetEqNr() != eq) {
446 if (deadAnalyzer->GetEqNr() != 999) {
447 printf("Error: Mismatching EqId in Dead Container data and filename (%d!=%d). Skipping.\n",
448 deadAnalyzer->GetEqNr(),eq);
451 nrDeadChips+=60; // since this eq is inactive...
456 eqActiveDead[eq] = kTRUE;
458 // configure analyzer with tuning parameters etc:
459 for (UInt_t i=0; i<nrTuningParams; i++) {
460 deadAnalyzer->SetParam(((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
463 printf("SPD phys STEP 2: Dead search for eq %d\n",eq);
465 // search for dead pixels:
466 nrEnoughStatChips += deadAnalyzer->ProcessDeadPixels();
467 nrDeadChips += deadAnalyzer->GetNrDeadChips();
468 nrInefficientChips += deadAnalyzer->GetNrInefficientChips();
474 daqDA_progressReport((unsigned int)(50+(eq+1)*2.5));
476 printf("progress: %d\n",(unsigned int)(75+(eq+1)*1.25));
479 // *** *** *** end loop over equipments (eq_id)
484 printf("Dead search finished.\n");
485 printf("%d single dead pixels , %d dead or inactive pixels in total.\n",handler->GetNrDead(),handler->GetNrSilent());
486 printf("%d chips had enough statistics. %d chips are dead. %d chips are inefficient.\n",nrEnoughStatChips,nrDeadChips,nrInefficientChips);
487 handler->SetFileLocation(saveDirDead);
488 handler->WriteSilentToFilesAlways();
489 handler->SetFileLocation(saveDirDeadToFXS);
490 handler->WriteSilentToFilesAlways();
493 printf("Opening id list file\n");
494 TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
496 idsFXSfile.open(idsFXSFileName.Data());
499 // send (dead) reference data for this run to FXS - only if there is no chip in category "needsMoreStat"
500 if (nrEnoughStatChips+nrDeadChips+nrInefficientChips == 1200) {
501 printf("Dead calibration is complete.\n"); // calibration is complete
502 printf("Preparing dead reference data\n");
503 // send reference data for dead pixels to FXS
504 TString tarFiles = "";
505 for (UInt_t eq=0; eq<20; eq++) {
506 if (eqActiveDead[eq]) {
507 printf("Preparing dead pixels for eq %d\n",eq);
508 // move file to ref dir
509 TString fileName = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
510 TString newFileName = Form("%s/SPDphys_dead_run_%d_%d_eq_%d.root",saveDirDeadRef,firstRunNrDead,runNr,eq);
511 TString command = Form("mv -f %s %s",fileName.Data(),newFileName.Data());
512 system(command.Data());
514 tarFiles.Append(Form("SPDphys_dead_run_%d_%d_eq_%d.root ",firstRunNrDead,runNr,eq));
517 TString send_command = Form("cd %s; tar -cf ref_phys_dead.tar %s",saveDirDeadRef,tarFiles.Data());
518 system(send_command.Data());
519 TString fileName = Form("%s/ref_phys_dead.tar",saveDirDeadRef);
520 TString id = "SPD_ref_phys_dead";
522 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
524 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
528 idsFXSfile << Form("%s\n",id.Data());
533 // send (noisy) reference data for this run to FXS
534 printf("Preparing noisy reference data\n");
535 TString tarFiles = "";
536 for (UInt_t eq=0; eq<20; eq++) {
537 if (eqActiveNoisy[eq]) {
538 tarFiles.Append(Form("SPDphys_run_%d_eq_%d.root ",runNr,eq));
541 TString send_command = Form("cd %s; tar -cf ref_phys.tar %s",saveDirNoisyRef,tarFiles.Data());
542 system(send_command.Data());
543 TString fileName = Form("%s/ref_phys.tar",saveDirNoisyRef);
544 TString id = "SPD_ref_phys";
546 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
548 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
552 idsFXSfile << Form("%s\n",id.Data());
557 // send dead pixels to FXS
558 printf("Preparing dead files\n");
559 // send a tared file of all the dead files
560 send_command = Form("cd %s; tar -cf dead_phys.tar *",saveDirDeadToFXS);
561 // printf("\n\n%s\n\n",command.Data());
562 system(send_command.Data());
563 fileName = Form("%s/dead_phys.tar",saveDirDeadToFXS);
564 id = "SPD_phys_dead";
566 Int_t send_status = daqDA_FES_storeFile(fileName.Data(),id.Data());
567 if (send_status!=0) {
568 printf("Failed to export file %s , status %d\n",fileName.Data(),send_status);
572 idsFXSfile << Form("%s\n",id.Data());
576 // send noisy pixels to FXS
577 if (handler->GetNrNoisy()>0) { // there must be at least one file created
578 printf("Preparing noisy files\n");
579 // send a tared file of all the noisy files
580 TString command = Form("cd %s; tar -cf noisy_phys.tar *",saveDirNoisyToFXS);
581 // printf("\n\n%s\n\n",command.Data());
582 system(command.Data());
583 TString fileName = Form("%s/noisy_phys.tar",saveDirNoisyToFXS);
584 TString id = "SPD_phys_noisy";
586 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
588 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
592 idsFXSfile << Form("%s\n",id.Data());
597 // send ids file to FXS
601 status = daqDA_FES_storeFile(idsFXSFileName.Data(),id.Data());
603 printf("Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);
613 // printf("*** End step2 , %d\n",time(NULL) - timeStamp);