to avoid AliFatal, for MOOD and displays, use of ideal geometry parameters is permitt...
[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. This is the default operating mode.       //
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. 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.                                       //
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 "AliITSOnlineCalibrationSPDhandler.h"
27 #include "AliLog.h"
28 #include <iostream>
29 #include <fstream>
30 #include <TROOT.h>
31 #include <TPluginManager.h>
32 #include <TObjArray.h>
33 #include <TString.h>
34
35 int main(int argc, char **argv) {
36   if (argc<2) {
37     printf("Wrong number of arguments\n");
38     return -1;
39   }
40
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";
53
54   char *paramsFileName       = "./standal_params.txt";
55   char *permNoisyFileName    = "./perm_noisy.txt";
56
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",
60                                         "*",
61                                         "TStreamerInfo",
62                                         "RIO",
63                                         "TStreamerInfo()");
64
65   // turn off annoying warning messages
66   new AliLog;
67   AliLog::Instance()->SetGlobalDebugLevel(-20);
68
69   // calib scan types
70   enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY};
71
72
73   // ********* STEP 0: Get configuration files from db (if there are any) *******************************
74   UInt_t nrTuningParams = 0;
75   TObjArray paramNames;  paramNames.SetOwner(kTRUE);
76   TObjArray paramVals;  paramVals.SetOwner(kTRUE);
77   
78   // tuning parameters:
79   Int_t status = 0;
80 #ifndef SPD_DA_OFF
81   TString idp = "spd_standal_params";
82   status=daqDA_DB_getFile(idp.Data(),paramsFileName);
83   if (status) {
84     printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),status);
85   }
86 #endif
87   if (status==0) {
88     ifstream paramsFile;
89     paramsFile.open(paramsFileName, ifstream::in);
90     if (paramsFile.fail()) {
91       printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName);
92     }
93     else {
94       while(1) {
95         Char_t paramN[50];
96         Char_t paramV[50];
97         paramsFile >> paramN;
98         if (paramsFile.eof()) break;
99         paramsFile >> paramV;
100         TString* paramNS = new TString(paramN);
101         TString* paramVS = new TString(paramV);
102         paramNames.AddAtAndExpand((TObject*)paramNS,nrTuningParams);
103         paramVals.AddAtAndExpand((TObject*)paramVS,nrTuningParams);
104         nrTuningParams++;
105         if (paramsFile.eof()) break;
106       }
107       paramsFile.close();
108     }
109   }
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());
112   //  }
113
114   // perm noisy list:
115   Int_t permstatus = 0;
116 #ifndef SPD_DA_OFF
117   TString idn = "spd_perm_noisy";
118   permstatus=daqDA_DB_getFile(idn.Data(),permNoisyFileName);
119   if (permstatus) {
120     printf("Failed to get config file %s: status=%d. No additional noisy pixels added.\n",idn.Data(),permstatus);
121   }
122 #endif
123
124
125
126
127   // ********* STEP 1: Produce scan container files (Reference Data). ***********************************
128   int startSeg = 1;
129
130 #ifndef SPD_DA_OFF
131   int runNr = atoi(getenv("DATE_RUN_NUMBER"));
132 #else
133   int runNr = atoi(argv[1]);
134   startSeg = 2;
135 #endif
136
137   // container objects
138   AliITSOnlineSPDscan *scanObj[20];
139   Bool_t bScanInit[20];
140   for (UInt_t eqId=0; eqId<20; eqId++) {
141     scanObj[eqId]=NULL;
142     bScanInit[eqId]=kFALSE;
143   }
144   // header data variables
145   UInt_t routerNr[20];
146   Bool_t halfStaveScanned[20][6];
147   UInt_t type[20];
148   Bool_t dataFormat[20];
149   UInt_t triggers[20];
150   Bool_t chipPresent[20][6][10];
151   UInt_t dacStart[20];  
152   UInt_t dacEnd[20];
153   UInt_t dacStep[20];
154   UInt_t dacId[20];
155   UInt_t rowStart[20];  
156   UInt_t rowEnd[20];
157   UInt_t rowValue[20];
158   UInt_t dacValue[20];
159   UInt_t dacHigh[20][6];
160   UInt_t dacLow[20][6];
161   UInt_t TPAmp[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;
166
167   // loop over run segments
168   for (int segNr=startSeg; segNr<argc; segNr++) {
169
170
171     int status;
172
173     /* define data source : this is argument 1 */  
174     status=monitorSetDataSource( argv[segNr] );
175     if (status!=0) {
176       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
177       return -1;
178     }
179     /* declare monitoring program */
180     status=monitorDeclareMp("ITS_SPD_CAL");
181     if (status!=0) {
182       printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
183       return -1;
184     }
185     /* define wait event timeout - 1s max */
186     monitorSetNowait();
187     monitorSetNoWaitNetworkTimeout(1000);
188
189
190     struct eventHeaderStruct *event;
191     eventTypeType eventT;
192     UInt_t eventNr=0;
193
194
195     /* main loop (infinite) */
196     for(;;) {
197
198       /* check shutdown condition */
199 #ifndef SPD_DA_OFF
200       if (daqDA_checkShutdown()) {break;}
201 #endif
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 */
207       }
208       if (status!=0) {
209         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
210         break;
211       }
212       /* retry if got no event */
213       if (event==NULL) {
214         printf("event==NULL\n");
215         continue;
216       }
217
218       eventT=event->eventType;
219       if (eventT == PHYSICS_EVENT){
220
221
222         eventNr++;
223         //      printf("eventNr %d\n",eventNr);
224
225         AliRawReader *reader = new AliRawReaderDate((void*)event);
226         AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
227
228
229         for (UInt_t eqId=0; eqId<20; eqId++) {
230
231           reader->Reset();
232           reader->Select("ITSSPD",eqId,eqId);
233
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;
241             }
242           }
243
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());
248             }
249
250             // read calib values
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);
270               }
271             }
272             for (UInt_t chip=0; chip<10; chip++) {
273               minTHchipPresent[eqId][chip] = str->GetHminTHchipPresent(chip);
274             }
275
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;
283               }
284             }
285
286             // router nr check:
287             if (routerNr[eqId]!=eqId) {
288               printf("Router nr problem? Router nr %d != EqID %d\n",routerNr[eqId],eqId);
289             }
290
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]) {
295               case NOISE:
296               case UNIMA:
297                 scanObj[eqId] = new AliITSOnlineSPDscanSingle(fileName.Data());
298                 ((AliITSOnlineSPDscanSingle*)scanObj[eqId])->ClearThis();
299                 bScanInit[eqId]=kTRUE;
300                 break;
301               case MINTH:
302               case DAC:
303               case DELAY:
304                 scanObj[eqId] = new AliITSOnlineSPDscanMultiple(fileName.Data());
305                 scanObj[eqId]->ClearThis();
306                 bScanInit[eqId]=kTRUE;
307                 break;
308               case MEANTH: 
309                 scanObj[eqId] = new AliITSOnlineSPDscanMeanTh(fileName.Data());
310                 scanObj[eqId]->ClearThis();
311                 bScanInit[eqId]=kTRUE;
312                 break;
313               default:
314                 printf("Unknown scan type: %d.\n",type[eqId]);
315               }
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]);
319               }
320               // some common data
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]);
325               }
326               scanObj[eqId]->SetType(type[eqId]);
327               scanObj[eqId]->SetDataFormat(dataFormat[eqId]);
328               for (Int_t hs=0; hs<6; hs++) {
329               
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 !!!!!!!!!!!!!!!!!!!!!!!!!!!
333
334                 for (UInt_t chip=0; chip<10; chip++) {
335                   scanObj[eqId]->SetChipPresent(hs,chip,chipPresent[eqId][hs][chip]);
336
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 !!!!!!!!!!!!!!!!!!!!!!!!!!!
340
341                 }
342               }
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]);
348             }
349
350             if (type[eqId]==MINTH) {
351               scanObj[eqId]->SetTriggers(currentStep[eqId],triggers[eqId]);
352             }
353             if (type[eqId]==UNIMA || type[eqId]==NOISE) {
354               if (currentStep[eqId]==9999) printf("SPDcalibratorStep1 (eq %d): single step\n",eqId);
355               currentStep[eqId]=0;
356             }
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]);
361               }
362               else {
363                 printf("SPDcalibratorStep1 (eq %d): DAC %d , step %d\n",eqId,dacValue[eqId],currentStep[eqId]);
364               }
365             }
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]);
371               }
372             }
373
374
375           }
376
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?
382 #ifndef SPD_DA_OFF
383               if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
384 #else
385               if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
386 #endif
387               //*************************************************************************
388                 scanObj[eqId]->IncrementHits(currentStep[eqId],hs,chip,str->GetChipCol(),str->GetChipRow());
389
390                 if (!hitEventHSIncremented[hs]) {
391                   scanObj[eqId]->IncrementHitEventsTot(currentStep[eqId],hs);
392                   hitEventHSIncremented[hs]=kTRUE;
393                 }
394             
395                 if (!hitEventChipIncremented[hs][chip]) {
396                   scanObj[eqId]->IncrementHitEvents(currentStep[eqId],hs,chip);
397                   hitEventChipIncremented[hs][chip]=kTRUE;
398                 }
399               }
400
401             }
402
403             if (type[eqId]!=MINTH) { // for minth, triggers are set from header info
404               scanObj[eqId]->IncrementTriggers(currentStep[eqId]);
405             }
406
407           }
408
409         }
410
411         delete str;
412         delete reader;
413
414       }
415
416       /* free resources */
417       free(event);
418
419     }
420
421     status = monitorLogout();
422     if (status != 0) {
423       printf("monitorLogout() failed : %s\n",monitorDecodeError(status));
424       return -1;
425     }
426     
427 #ifndef SPD_DA_OFF
428     daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
429 #else
430     printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
431 #endif
432
433   }
434
435   // clean up scan objects
436   for (UInt_t eqId=0; eqId<20; eqId++) {
437     if (scanObj[eqId]!=NULL) delete scanObj[eqId];
438   }
439
440
441
442
443
444   // ********* STEP 2: Analyze scan container files. ************************************************
445
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;
453
454   AliITSOnlineSPDscanAnalyzer *analyzer;
455
456   // *** *** *** start loop over equipments (eq_id)
457   for (int eqId=0; eqId<20; eqId++) {
458
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());
462
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());
466     }
467
468     Int_t type  = analyzer->GetType();
469     Int_t dacId = analyzer->GetDacId();
470     if (type!=99) {
471       if (type==DAC) {
472         printf("SPD calibrator Step2: eqId %d, type %d, dacId %d\n",eqId,type,dacId);
473       }
474       else printf("SPD calibrator Step2: eqId %d type %d\n",eqId,type);  
475     }
476     
477
478     // algorithms for the different types of scans:
479
480     if (type==UNIMA) {
481
482     }
483
484     else if (type==NOISE) {
485       if (analyzer->ProcessNoisyPixels(saveDirNoisy)) {
486         // init dcs config text file
487         TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
488         ofstream dcsfile;
489         dcsfile.open(dcsConfigFileName.Data());
490         dcsfile << "[SPD SCAN]\n";
491         dcsfile << "RunNumber=" << runNr << "\n";
492         dcsfile << "Type=" << type << "\n";
493         dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
494         dcsfile << "[NOISY]\n";
495         nrDCSconfigFilesProduced++;
496         for (UInt_t iModule=0; iModule<12; iModule++) {
497           UInt_t module = AliITSRawStreamSPD::GetModuleNumber(eqId,iModule);      
498           AliITSOnlineCalibrationSPDhandler *handler = analyzer->GetOnlineCalibrationHandler(module);
499           if (permstatus==0) { // add permanent noisy list
500             UInt_t newNoisy = handler->ReadNoisyFromText(permNoisyFileName);
501             if (newNoisy>0) {
502               printf("%d additional noisy pixels added from permanent list.\n",newNoisy);
503             }
504           }
505           if (analyzer->SaveDeadNoisyPixels(module,saveDirNoisy)) {
506             UInt_t nrNoisy = handler->GetNrNoisy(module);
507             UInt_t headkey=20*10*6;
508             for (UInt_t ind=0; ind<nrNoisy; ind++) {
509               UInt_t newkey = handler->GetNoisyEqIdAt(module,ind)*10*6 +
510                 handler->GetNoisyHSAt(module,ind)*10 +
511                 handler->GetNoisyChipAt(module,ind);
512               if (newkey!=headkey) { // print eqId,hs,chip_header
513                 headkey = newkey;
514                 dcsfile << "-" << newkey/(6*10) << "," << (newkey%(6*10))/10 << "," << (newkey%(6*10))%10 << "\n";
515               }
516               dcsfile << handler->GetNoisyColAt(module,ind) << "," << handler->GetNoisyRowAt(module,ind) << "\n";
517             }
518             nrNoisyFilesProduced++;
519             TString command = Form("cp %s/SPD_DeadNoisy_%d.root %s/.",saveDirNoisy,module,saveDirNoisyToFXS);
520             system(command.Data());
521           }
522         }
523         dcsfile.close();
524       }
525     }
526
527     else if (type==MINTH || (type==DAC && dacId==39)) {
528       // init dcs config text file
529       TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
530       ofstream dcsfile;
531       dcsfile.open(dcsConfigFileName.Data());
532       dcsfile << "[SPD SCAN]\n";
533       dcsfile << "RunNumber=" << runNr << "\n";
534       dcsfile << "Type=" << type << "\n";
535       dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
536       dcsfile << "[DACvalues]\n";
537       nrDCSconfigFilesProduced++;
538       TString ofileName = Form("%s/minth_eq_%d.txt",saveDirParameters,eqId);
539       ofstream ofile;
540       ofile.open (ofileName.Data());
541       for (UInt_t hs=0; hs<6; hs++) {
542         for (UInt_t chipNr=0; chipNr<10; chipNr++) {
543           Int_t minTh = -1;
544           if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
545             minTh = analyzer->GetMinTh(hs,chipNr);
546             if (minTh!=-1) {
547               dcsfile << "39," << eqId << "," << hs << "," << chipNr << "=" << minTh << "\n";
548             }
549             else {
550               printf("MinTh failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
551             }
552           }
553           ofile << minTh;
554           ofile << "\t";
555         }
556         ofile << "\n";
557       }
558       ofile.close();
559       dcsfile.close();
560     }
561
562     else if (type==DELAY) {
563       // init dcs config text file
564       TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
565       ofstream dcsfile;
566       dcsfile.open(dcsConfigFileName.Data());
567       dcsfile << "[SPD SCAN]\n";
568       dcsfile << "RunNumber=" << runNr << "\n";
569       dcsfile << "Type=" << type << "\n";
570       dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
571       dcsfile << "[DACvalues]\n";
572       nrDCSconfigFilesProduced++;
573       TString ofileName = Form("%s/delay_eq_%d.txt",saveDirParameters,eqId);
574       ofstream ofile;
575       ofile.open (ofileName.Data());
576       for (UInt_t hs=0; hs<6; hs++) {
577         for (UInt_t chipNr=0; chipNr<10; chipNr++) {
578           Int_t clockCycle = -1;
579           Int_t delayCtrl = -1;
580           Int_t miscCtrl = -1;
581           if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
582             clockCycle = analyzer->GetDelay(hs,chipNr);
583             delayCtrl = clockCycle/2;
584             miscCtrl = 192;
585             if (clockCycle!=-1) {
586               if (clockCycle%2==1) miscCtrl = 128;
587               dcsfile << "42," << eqId << "," << hs << "," << chipNr << "=" << delayCtrl << "\n";
588               dcsfile << "43," << eqId << "," << hs << "," << chipNr << "=" << miscCtrl << "\n";
589             }
590             else {
591               printf("Delay failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
592             }
593           }
594           ofile << delayCtrl << "/" << miscCtrl;
595           ofile << "\t";
596         }
597         ofile << "\n";
598       }
599       ofile.close();
600       dcsfile.close();
601     }
602
603     delete analyzer;
604
605 #ifndef SPD_DA_OFF
606     daqDA_progressReport((unsigned int)(50+(eqId+1)*2.5));
607 #else
608     printf("progress: %d\n",(unsigned int)(50+(eqId+1)*2.5));
609 #endif
610
611   }
612   // *** *** *** end loop over equipments (eq_id)
613
614
615   if (nrNoisyFilesProduced>0) {
616     // send a tared file of all new noisy maps
617     TString command = Form("cd %s; tar -cf noisy.tar *",saveDirNoisyToFXS);
618     printf("\n\n%s\n\n",command.Data());
619     system(command.Data());
620 #ifndef SPD_DA_OFF
621     TString fileName = Form("%s/noisy.tar",saveDirNoisyToFXS);
622     TString id = "SPD_noisy";
623     Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
624     if (status!=0) {
625       printf("Failed to export file %s , status %d\n",fileName.Data(),status);
626       return -1;
627     }
628 #endif
629   }
630
631   if (nrDCSconfigFilesProduced>0) {
632     // send a tared file of all the dcsConfig text files
633     TString command = Form("cd %s; tar -cf dcsConfig.tar *",saveDirDCSconfigToFXS);
634     //    printf("\n\n%s\n\n",command.Data());
635     system(command.Data());
636 #ifndef SPD_DA_OFF
637     TString fileName = Form("%s/dcsConfig.tar",saveDirDCSconfigToFXS);
638     TString id = "SPD_dcsConfig";
639     Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
640     if (status!=0) {
641       printf("Failed to export file %s , status %d\n",fileName.Data(),status);
642       return -1;
643     }
644 #endif
645   }
646
647
648
649   return 0;
650 }