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"
39 #include <TPluginManager.h>
40 #include <TObjArray.h>
43 int main(int argc, char **argv) {
45 printf("Wrong number of arguments\n");
49 // directory structure, hard coded
50 char *saveDirDead = "./calibResults/Dead"; // may NOT delete content
51 char *saveDirDeadToFXS = "./calibResults/DeadToFXS"; // may delete content
52 char *saveDirDeadRef = "./calibResults/DeadReference"; // may delete content
53 char *saveDirDeadRefTmp = "./calibResults/DeadReferenceTmp"; // may NOT delete content
54 char *saveDirNoisyToFXS = "./calibResults/NoisyToFXS"; // may delete content
55 char *saveDirNoisyRef = "./calibResults/NoisyReference"; // may delete content
56 char *saveDirIdsToFXS = "./calibResults/IdsToFXS"; // may delete content
57 char *configFilesDir = "./configFiles"; // may delete content
58 // make sure the directory structure is put up correctly:
59 system("mkdir ./calibResults >& /dev/null");
60 system("mkdir ./calibResults/Dead >& /dev/null");
61 system("mkdir ./calibResults/DeadToFXS >& /dev/null");
62 system("mkdir ./calibResults/DeadReference >& /dev/null");
63 system("mkdir ./calibResults/DeadReferenceTmp >& /dev/null");
64 system("mkdir ./calibResults/NoisyToFXS >& /dev/null");
65 system("mkdir ./calibResults/NoisyReference >& /dev/null");
66 system("mkdir ./calibResults/IdsToFXS >& /dev/null");
67 system("mkdir ./configFiles >& /dev/null");
68 // parameters config file
69 TString paramsFileName = Form("%s/physics_params.txt",configFilesDir);
71 // This line is needed in case of a stand-alone application w/o
72 // $ROOTSYS/etc/system.rootrc file
73 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
79 // turn off annoying warning messages
81 AliLog::Instance()->SetGlobalDebugLevel(-20);
84 // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********
85 UInt_t nrTuningParams = 0;
86 TObjArray paramNames; paramNames.SetOwner(kTRUE);
87 TObjArray paramVals; paramVals.SetOwner(kTRUE);
92 TString idp = "spd_physics_params";
93 par_status=daqDA_DB_getFile(idp.Data(),paramsFileName.Data());
95 printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),par_status);
100 paramsFile.open(paramsFileName.Data(), ifstream::in);
101 if (paramsFile.fail()) {
102 printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName.Data());
108 paramsFile >> paramN;
109 if (paramsFile.eof()) break;
110 paramsFile >> paramV;
111 TString* paramNS = new TString(paramN);
112 TString* paramVS = new TString(paramV);
113 paramNames.AddAtAndExpand((TObject*)paramNS,nrTuningParams);
114 paramVals.AddAtAndExpand((TObject*)paramVS,nrTuningParams);
116 if (paramsFile.eof()) break;
121 // for (UInt_t i=0; i<nrTuningParams; i++) {
122 // printf("Entry %d: N=%s , V=%s\n",i,((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
129 // create calibration handler (needed already at step 1 in order to fill which eq,hs,chips are active
130 AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
131 // Read silent=dead+inactive info from previous calibrations
132 handler->SetFileLocation(saveDirDead);
133 handler->ReadSilentFromFiles();
134 //!!! // start by deactivating all eq - should then be activated when found in raw data
135 //!!! for (UInt_t eq=0; eq<20; eq++) {
136 //!!! handler->ActivateEq(eq,kFALSE);
142 // ********* STEP 1: Produce phys container files (Reference Data). ***********************************
145 if (getenv("DATE_RUN_NUMBER")==0) {
146 printf("DATE_RUN_NUMBER not properly set.\n");
149 int runNr = atoi(getenv("DATE_RUN_NUMBER"));
151 int runNr = atoi(argv[1]);
157 AliITSOnlineSPDphys *physObj[20];
158 Bool_t bPhysInit[20];
159 for (UInt_t eqId=0; eqId<20; eqId++) {
161 bPhysInit[eqId]=kFALSE;
165 // loop over run segments in case of offline mode
167 for (int segNr=startSeg; segNr<argc; segNr++) {
172 /* define data source : */
174 status=monitorSetDataSource( argv[1] ); // should be "^SPD" in order to get full detector online
176 status=monitorSetDataSource( argv[segNr] );
179 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
182 /* declare monitoring program */
183 status=monitorDeclareMp("ITS_SPD_PHYS");
185 printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
188 /* define wait event timeout - 1s max */
190 monitorSetNoWaitNetworkTimeout(1000);
195 /* main loop (infinite) */
198 struct eventHeaderStruct *event;
199 eventTypeType eventT;
201 /* check shutdown condition */
203 if (daqDA_checkShutdown()) {break;}
206 /* get next event (blocking call until timeout) */
207 status=monitorGetEventDynamic((void **)&event);
208 if (status==MON_ERR_EOF) {
209 printf ("End of File detected\n");
210 break; /* end of monitoring file has been reached */
214 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
218 /* retry if got no event */
223 eventT=event->eventType;
224 if (eventT == PHYSICS_EVENT){
226 // printf("eventNr %d\n",eventNr);
228 AliRawReader *reader = new AliRawReaderDate((void*)event);
229 AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
231 while (str->Next()) {
233 Int_t eqId = reader->GetDDLID();
234 // check that this hs is active in handler object
235 if (!(handler->IsActiveEq(eqId))) {
236 printf("Warning: Found Eq (%d) , previously inactive in this run!\n",eqId);
237 handler->ActivateEq(eqId);
239 if (eqId>=0 && eqId<20) {
240 if (!bPhysInit[eqId]) { // this code is duplicated for the moment... (see also below)
241 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eqId);
242 physObj[eqId] = new AliITSOnlineSPDphys(fileName.Data());
243 physObj[eqId]->AddRunNr(runNr);
244 physObj[eqId]->SetEqNr(eqId);
245 bPhysInit[eqId]=kTRUE;
248 UInt_t hs = str->GetHalfStaveNr();
249 // check that this hs is active in handler object
250 if (!(handler->IsActiveHS(eqId,hs))) {
251 printf("Warning: Found HS (%d,%d) , previously inactive in this run!\n",eqId,hs);
252 handler->ActivateHS(eqId,hs);
254 UInt_t chip = str->GetChipAddr();
255 // check that this chip is active in handler object
256 if (!(handler->IsActiveChip(eqId,hs,chip))) {
257 printf("Info: Found Chip (%d,%d,%d) , inactive in previous run.\n",eqId,hs,chip);
258 handler->ActivateChip(eqId,hs,chip);
260 physObj[eqId]->IncrementHits(hs,chip,str->GetChipCol(),str->GetChipRow());
266 // check which eq and hs are active for first event only
268 for (UInt_t eqId=0; eqId<20; eqId++) {
269 // activate Eq and HSs in handler object
270 if (str->IsActiveEq(eqId)) {
271 handler->ActivateEq(eqId);
272 for (UInt_t hs=0; hs<6; hs++) {
273 handler->ActivateHS(eqId,hs,str->IsActiveHS(eqId,hs));
275 if (!bPhysInit[eqId]) { // this code is duplicated for the moment... (see also above)
276 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eqId);
277 physObj[eqId] = new AliITSOnlineSPDphys(fileName.Data());
278 physObj[eqId]->AddRunNr(runNr);
279 physObj[eqId]->SetEqNr(eqId);
280 bPhysInit[eqId]=kTRUE;
284 handler->ActivateEq(eqId,kFALSE);
290 for (UInt_t eq=0; eq<20; eq++) {
292 physObj[eq]->IncrementNrEvents();
310 printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
314 // clean up phys objects (also saves them)
315 for (UInt_t eq=0; eq<20; eq++) {
316 if (physObj[eq]!=NULL) delete physObj[eq];
323 // ********* STEP 2: Analyze phys container files. ************************************************
325 // clear noisyToFXS dir:
327 command = Form("cd %s; rm -f *",saveDirNoisyToFXS);
328 system(command.Data());
329 // clear deadToFXS dir:
330 command = Form("cd %s; rm -f *",saveDirDeadToFXS);
331 system(command.Data());
334 UInt_t firstRunNrDead = runNr;
337 UInt_t nrEnoughStatNoisy = 0;
338 UInt_t nrEqActiveNoisy = 0;
339 Bool_t eqActiveNoisy[20];
341 // *** *** *** start loop over equipments (eq_id)
342 for (UInt_t eq=0; eq<20; eq++) {
343 eqActiveNoisy[eq] = kFALSE;
345 // create analyzer for this eq
346 TString fileName = Form("%s/SPDphys_run_%d_eq_%d.root",saveDirNoisyRef,runNr,eq);
347 AliITSOnlineSPDphysAnalyzer *noisyAnalyzer = new AliITSOnlineSPDphysAnalyzer(fileName.Data(),handler);
349 // check data in container
350 if (noisyAnalyzer->GetEqNr() != eq) {
351 if (noisyAnalyzer->GetEqNr() != 999) {
352 printf("Error: Mismatching EqId in Container data and filename (%d!=%d). Skipping.\n",
353 noisyAnalyzer->GetEqNr(),eq);
355 delete noisyAnalyzer;
360 eqActiveNoisy[eq] = kTRUE;
362 // configure analyzer with tuning parameters etc:
363 for (UInt_t i=0; i<nrTuningParams; i++) {
364 noisyAnalyzer->SetParam(((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
367 printf("SPD phys STEP 2: Noisy search for eq %d\n",eq);
369 // search for noisy pixels:
370 nrEnoughStatNoisy += noisyAnalyzer->ProcessNoisyPixels();
372 // copy this phys obj to temporary dead reference dir to process after noisy search
373 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
374 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
375 physObj->AddPhys(noisyAnalyzer->GetOnlinePhys());
376 if (physObj->GetNrRuns()>0) {
377 UInt_t firstRunNr = physObj->GetRunNr(0);
378 if (firstRunNrDead>firstRunNr) {
379 firstRunNrDead=firstRunNr;
382 // remove noisy pixels from dead hitmap
383 for (UInt_t hs=0; hs<6; hs++) {
384 for (UInt_t chip=0; chip<10; chip++) {
385 for (UInt_t ind=0; ind<handler->GetNrNoisyC(eq,hs,chip); ind++) {
386 UInt_t col = handler->GetNoisyColAtC(eq,hs,chip,ind);
387 UInt_t row = handler->GetNoisyRowAtC(eq,hs,chip,ind);
388 physObj->AddHits(hs,chip,col,row,-noisyAnalyzer->GetOnlinePhys()->GetHits(hs,chip,col,row));
394 delete noisyAnalyzer;
397 daqDA_progressReport((unsigned int)((eq+1)*2.5));
399 printf("progress: %d\n",(unsigned int)(50+(eq+1)*1.25));
402 // *** *** *** end loop over equipments (eq_id)
404 printf("Noisy search finished. %d noisy pixels found. %d chips (%d) had enough statistics.\n",
405 handler->GetNrNoisy(),nrEnoughStatNoisy,nrEqActiveNoisy*60);
406 handler->SetFileLocation(saveDirNoisyToFXS);
407 handler->WriteNoisyToFiles();
416 UInt_t nrEnoughStatChips = 0;
417 UInt_t nrDeadChips = 0;
418 UInt_t nrInefficientChips = 0;
419 UInt_t nrEqActiveDead = 0;
420 Bool_t eqActiveDead[20];
422 // *** *** *** start loop over equipments (eq_id)
423 for (UInt_t eq=0; eq<20; eq++) {
424 eqActiveDead[eq] = kFALSE;
426 // setup analyzer for dead search
427 TString fileNameDead = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
428 AliITSOnlineSPDphys* physObj = new AliITSOnlineSPDphys(fileNameDead.Data());
429 AliITSOnlineSPDphysAnalyzer* deadAnalyzer = new AliITSOnlineSPDphysAnalyzer(physObj,handler);
430 // check data in container
431 if (deadAnalyzer->GetEqNr() != eq) {
432 if (deadAnalyzer->GetEqNr() != 999) {
433 printf("Error: Mismatching EqId in Dead Container data and filename (%d!=%d). Skipping.\n",
434 deadAnalyzer->GetEqNr(),eq);
437 //!!! nrDeadChips+=60; // since this eq is inactive...
442 eqActiveDead[eq] = kTRUE;
444 // configure analyzer with tuning parameters etc:
445 for (UInt_t i=0; i<nrTuningParams; i++) {
446 deadAnalyzer->SetParam(((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
449 printf("SPD phys STEP 2: Dead search for eq %d\n",eq);
451 // search for dead pixels:
452 nrEnoughStatChips += deadAnalyzer->ProcessDeadPixels();
453 nrDeadChips += deadAnalyzer->GetNrDeadChips();
454 nrInefficientChips += deadAnalyzer->GetNrInefficientChips();
459 daqDA_progressReport((unsigned int)(50+(eq+1)*2.5));
461 printf("progress: %d\n",(unsigned int)(75+(eq+1)*1.25));
464 // *** *** *** end loop over equipments (eq_id)
467 printf("Dead search finished. %d dead pixels in total.\n%d chips (%d) had enough statistics. %d chips were dead. %d chips were inefficient.\n",handler->GetNrDead(),nrEnoughStatChips,nrEqActiveDead*60,nrDeadChips,nrInefficientChips);
468 handler->SetFileLocation(saveDirDead);
469 handler->WriteSilentToFilesAlways();
470 handler->SetFileLocation(saveDirDeadToFXS);
471 handler->WriteSilentToFilesAlways();
474 printf("Opening id list file\n");
475 TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
477 idsFXSfile.open(idsFXSFileName.Data());
480 // send (dead) reference data for this run to FXS - only if there is no chip in category "needsMoreStat"
481 if (nrEnoughStatChips+nrDeadChips+nrInefficientChips == nrEqActiveDead*60) {
482 printf("Dead calibration is complete.\n"); // calibration is complete
483 printf("Preparing dead reference data\n");
484 // send reference data for dead pixels to FXS
485 TString tarFiles = "";
486 for (UInt_t eq=0; eq<20; eq++) {
487 if (eqActiveDead[eq]) {
488 printf("Preparing dead pixels for eq %d\n",eq);
489 // move file to ref dir
490 TString fileName = Form("%s/SPDphys_dead_run_0_0_eq_%d.root",saveDirDeadRefTmp,eq);
491 TString newFileName = Form("%s/SPDphys_dead_run_%d_%d_eq_%d.root",saveDirDeadRef,firstRunNrDead,runNr,eq);
492 TString command = Form("mv -f %s %s",fileName.Data(),newFileName.Data());
493 system(command.Data());
495 tarFiles.Append(Form("SPDphys_dead_run_%d_%d_eq_%d.root ",firstRunNrDead,runNr,eq));
498 TString send_command = Form("cd %s; tar -cf ref_phys_dead.tar %s",saveDirDeadRef,tarFiles.Data());
499 system(send_command.Data());
500 TString fileName = Form("%s/ref_phys_dead.tar",saveDirDeadRef);
501 TString id = "SPD_ref_phys_dead";
503 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
505 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
509 idsFXSfile << Form("%s\n",id.Data());
513 // send (noisy) reference data for this run to FXS
514 printf("Preparing noisy reference data\n");
515 TString tarFiles = "";
516 for (UInt_t eq=0; eq<20; eq++) {
517 if (eqActiveNoisy[eq]) {
518 tarFiles.Append(Form("SPDphys_run_%d_eq_%d.root ",runNr,eq));
521 TString send_command = Form("cd %s; tar -cf ref_phys.tar %s",saveDirNoisyRef,tarFiles.Data());
522 system(send_command.Data());
523 TString fileName = Form("%s/ref_phys.tar",saveDirNoisyRef);
524 TString id = "SPD_ref_phys";
526 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
528 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
532 idsFXSfile << Form("%s\n",id.Data());
535 // send dead pixels to FXS
536 printf("Preparing dead files\n");
537 // send a tared file of all the dead files
538 send_command = Form("cd %s; tar -cf dead_phys.tar *",saveDirDeadToFXS);
539 // printf("\n\n%s\n\n",command.Data());
540 system(send_command.Data());
541 fileName = Form("%s/dead_phys.tar",saveDirDeadToFXS);
542 id = "SPD_phys_dead";
544 Int_t send_status = daqDA_FES_storeFile(fileName.Data(),id.Data());
545 if (send_status!=0) {
546 printf("Failed to export file %s , status %d\n",fileName.Data(),send_status);
550 idsFXSfile << Form("%s\n",id.Data());
553 // send noisy pixels to FXS
554 if (handler->GetNrNoisy()>0) { // there must be at least one file created
555 printf("Preparing noisy files\n");
556 // send a tared file of all the noisy files
557 TString command = Form("cd %s; tar -cf noisy_phys.tar *",saveDirNoisyToFXS);
558 // printf("\n\n%s\n\n",command.Data());
559 system(command.Data());
560 TString fileName = Form("%s/noisy_phys.tar",saveDirNoisyToFXS);
561 TString id = "SPD_phys_noisy";
563 status = daqDA_FES_storeFile(fileName.Data(),id.Data());
565 printf("Failed to export file %s , status %d\n",fileName.Data(),status);
569 idsFXSfile << Form("%s\n",id.Data());
573 // send ids file to FXS
577 status = daqDA_FES_storeFile(idsFXSFileName.Data(),id.Data());
579 printf("Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);