]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/ITSSPDSCANda.cxx
Compilation with Root6
[u/mrichter/AliRoot.git] / ITS / ITSSPDSCANda.cxx
1 /*
2 Contact: henrik.tydesjo@cern.ch
3 Link: tydes.home.cern.ch/tydes/doc/CalibrationOverview/CalibrationAlgorithms/
4 Run Type: DAQ_MIN_TH_SCAN,DAQ_MEAN_TH_SCAN,DAQ_UNIFORMITY_SCAN,DAQ_NOISY_PIX_SCAN,DAQ_PIX_DELAY_SCAN
5 DA Type: LDC
6 Number of events needed: Depending on scan type
7 Input Files: spd_standal_params,spd_perm_noisy ,  ./calibResults/ScanNoisy/* ,  raw data
8 Output Files: ./calibResults/ScanReference/* ,  ./calibResults/ScanDCSconfigToFXS/* ,  ./calibResults/ScanNoisyToFXS/* ,  ./calibResults/ScanNoisy/*
9 Trigger types used: PHYSICS
10 */
11
12 ////////////////////////////////////////////////////////////////////////////////
13 // This program can be compiled in two modes.                                 //
14 //                                                                            //
15 // 1. With the DAQ DA framework on. This is the default operating mode.       //
16 // Call this program with the name of the executable followed by the          //
17 // data files to process.                                                     //
18 //                                                                            //
19 // 2. Without the DAQ DA framework on. Define the SPD_DA_OFF environment var. //
20 // Call this program with the name of the executable followed by the          //
21 // runNr and the data files to process.                                       //
22 //                                                                            //
23 ////////////////////////////////////////////////////////////////////////////////
24
25 #ifndef SPD_DA_OFF
26 extern "C" {
27 #include "daqDA.h"
28 }
29 #endif
30 #include "event.h"
31 #include "monitor.h"
32 #include "AliRawReaderDate.h"
33 #include "AliITSRawStreamSPD.h"
34 #include "AliITSOnlineSPDscan.h"
35 #include "AliITSOnlineSPDscanSingle.h"
36 #include "AliITSOnlineSPDscanMultiple.h"
37 #include "AliITSOnlineSPDscanMeanTh.h"
38 #include "AliITSOnlineSPDscanAnalyzer.h"
39 #include "AliITSOnlineCalibrationSPDhandler.h"
40 #include "AliLog.h"
41 #include <iostream>
42 #include <fstream>
43 #include <TROOT.h>
44 #include <TPluginManager.h>
45 #include <TObjArray.h>
46 #include <TObjString.h>
47 #include <TString.h>
48 #include <TFitter.h>
49 #include <cstdlib>
50
51 int main(int argc, char **argv) {
52   if (argc<2) {
53     printf("Wrong number of arguments\n");
54     return -1;
55   }
56
57   // directory structure, hard coded
58   char *saveDirNoisy         = "./calibResults/ScanNoisy";          // may NOT delete content
59   char *saveDirNoisyToFXS    = "./calibResults/ScanNoisyToFXS";     //     may delete content
60   char *saveDirDCSconfigToFXS= "./calibResults/ScanDCSconfigToFXS"; //     may delete content
61   char *saveDirRef           = "./calibResults/ScanReference";      //     may delete content
62   char *saveDirIdsToFXS      = "./calibResults/IdsToFXS";           //     may delete content
63   char *configFilesDir       = "./configFiles";                     //     may delete content
64
65   // make sure the directory structure is correct:
66   system("mkdir ./calibResults >& /dev/null");
67   system("mkdir ./calibResults/ScanNoisy >& /dev/null");
68   system("mkdir ./calibResults/ScanNoisyToFXS >& /dev/null");
69   system("mkdir ./calibResults/ScanDCSconfigToFXS >& /dev/null");
70   system("mkdir ./calibResults/ScanReference >& /dev/null");
71   system("mkdir ./calibResults/IdsToFXS >& /dev/null");
72   system("mkdir ./configFiles >& /dev/null");
73   // prameters config files
74   TString paramsFileName = Form("%s/standal_params.txt",configFilesDir);
75   TString permNoisyFileName = Form("%s/perm_noisy.txt",configFilesDir);
76
77   TFitter *fitter = new TFitter(3);
78   TVirtualFitter::SetFitter(fitter);
79
80   // This line is needed in case of a stand-alone application w/o
81   // $ROOTSYS/etc/system.rootrc file
82   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
83                                         "*",
84                                         "TStreamerInfo",
85                                         "RIO",
86                                         "TStreamerInfo()");
87
88   // turn off annoying warning messages
89   // NB: Should not be handled here
90   AliLog* logger = AliLog::GetRootLogger();
91   logger->SetGlobalDebugLevel(-20);
92
93   // calib scan types
94   enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY};
95
96
97   // ********* STEP 0: Get configuration files from db (if there are any) , then read parameters*********
98   UInt_t nrTuningParams = 0;
99   TObjArray paramNames;  paramNames.SetOwner(kTRUE);
100   TObjArray paramVals;  paramVals.SetOwner(kTRUE);
101   
102   // tuning parameters:
103   Int_t status = 0;
104 #ifndef SPD_DA_OFF
105   TString idp = "spd_standal_params";
106   status=daqDA_DB_getFile(idp.Data(),paramsFileName.Data());
107   if (status) {
108     printf("Failed to get config file %s: status=%d. Using default tuning parameters.\n",idp.Data(),status);
109     TString rmCmd = Form("rm -f %s",paramsFileName.Data());
110     system(rmCmd.Data());
111   }
112 #endif
113   if (status==0) {
114     ifstream paramsFile;
115     paramsFile.open(paramsFileName.Data(), ifstream::in);
116     if (paramsFile.fail()) {
117       printf("No config file (%s) present. Using default tuning parameters.\n",paramsFileName.Data());
118     }
119     else {
120       while(1) {
121         Char_t paramN[50];
122         Char_t paramV[50];
123         paramsFile >> paramN;
124         if (paramsFile.eof()) break;
125         paramsFile >> paramV;
126         paramNames.AddAtAndExpand(new TObjString(paramN),nrTuningParams);
127         paramVals.AddAtAndExpand(new TObjString(paramV),nrTuningParams);
128         nrTuningParams++;
129         if (paramsFile.eof()) break;
130       }
131       paramsFile.close();
132     }
133   }
134   //  for (UInt_t i=0; i<nrTuningParams; i++) {
135   //  //    printf("Entry %d: N=%s , V=%s\n",i,((TString*)paramNames.At(i))->Data(),((TString*)paramVals.At(i))->Data());
136   //    printf("Entry %d: N=%s , V=%s\n",i,((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
137   //  }
138
139   // perm noisy list:
140   Int_t permstatus = 0;
141 #ifndef SPD_DA_OFF
142   TString idn = "spd_perm_noisy";
143   permstatus=daqDA_DB_getFile(idn.Data(),permNoisyFileName.Data());
144   if (permstatus) {
145     printf("Failed to get config file %s: status=%d. No permanently noisy pixels will be added.\n",idn.Data(),permstatus);
146     TString rmCmd = Form("rm -f %s",permNoisyFileName.Data());
147     system(rmCmd.Data());
148   }
149 #endif
150
151
152
153
154   // ********* STEP 1: Produce scan container files (Reference Data). ***********************************
155   int startSeg = 1;
156
157 #ifndef SPD_DA_OFF
158   if (getenv("DATE_RUN_NUMBER")==0) {
159     printf("DATE_RUN_NUMBER not properly set.\n");
160     return -1;
161   }
162   int runNr = atoi(getenv("DATE_RUN_NUMBER"));
163 #else
164   int runNr = atoi(argv[1]);
165   startSeg = 2;
166 #endif
167
168   // container objects
169   AliITSOnlineSPDscan *scanObj[20];
170   Bool_t bScanInit[20];
171   for (UInt_t eqId=0; eqId<20; eqId++) {
172     scanObj[eqId]=NULL;
173     bScanInit[eqId]=kFALSE;
174   }
175   // header data variables
176   UInt_t routerNr[20];
177   Bool_t halfStaveScanned[20][6];
178   UInt_t type[20];
179   Bool_t dataFormat[20];
180   UInt_t triggers[20];
181   Bool_t chipPresent[20][6][10];
182   UInt_t dacStart[20];  
183   UInt_t dacEnd[20];
184   UInt_t dacStep[20];
185   UInt_t dacId[20];
186   UInt_t rowStart[20];  
187   UInt_t rowEnd[20];
188   UInt_t rowValue[20];
189   UInt_t dacValue[20];
190   UInt_t dacHigh[20][6];
191   UInt_t dacLow[20][6];
192   UInt_t TPAmp[20][6];
193   Bool_t minTHchipPresent[20][10];
194   // current scan step flag
195   Int_t currentStep[20];
196   for (UInt_t eqId=0; eqId<20; eqId++) currentStep[eqId] = 9999;
197
198   // loop over run segments
199   for (int segNr=startSeg; segNr<argc; segNr++) {
200
201     int status;
202
203     /* define data source : this is argument 1 */  
204     status=monitorSetDataSource( argv[segNr] );
205     if (status!=0) {
206       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
207       return -1;
208     }
209     /* declare monitoring program */
210     status=monitorDeclareMp("ITS_SPD_CAL");
211     if (status!=0) {
212       printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
213       return -1;
214     }
215     /* define wait event timeout - 1s max */
216     monitorSetNowait();
217     monitorSetNoWaitNetworkTimeout(1000);
218     
219     
220     UInt_t eventNr=0;
221
222     /* main loop (infinite) */
223     for(;;) {
224       
225       struct eventHeaderStruct *event;
226       eventTypeType eventT;
227       
228       /* check shutdown condition */
229 #ifndef SPD_DA_OFF
230       if (daqDA_checkShutdown()) {break;}
231 #endif
232       /* get next event (blocking call until timeout) */
233       status=monitorGetEventDynamic((void **)&event);
234       if (status==MON_ERR_EOF) {
235         printf ("End of File detected\n");
236         break; /* end of monitoring file has been reached */
237       }
238       if (status!=0) {
239         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
240         break;
241       }
242       /* retry if got no event */
243       if (event==NULL) {
244         continue;
245       }
246
247       eventT=event->eventType;
248       if (eventT == PHYSICS_EVENT) {
249         
250         eventNr++;
251         //      printf("eventNr %d\n",eventNr);
252         
253         AliRawReader *reader = new AliRawReaderDate((void*)event);
254         AliITSRawStreamSPD *str = new AliITSRawStreamSPD(reader);
255         
256         for (UInt_t eqId=0; eqId<20; eqId++) {
257           
258           reader->Reset();
259           reader->Select("ITSSPD",eqId,eqId);
260           
261           // Hit Event flags, specific for one event
262           Bool_t hitEventHSIncremented[6];
263           Bool_t hitEventChipIncremented[6][10];
264           for (UInt_t hs=0; hs<6; hs++) {
265             hitEventHSIncremented[hs] = kFALSE;
266             for (UInt_t chip=0; chip<10; chip++) {
267               hitEventChipIncremented[hs][chip] = kFALSE;
268             }
269           }
270           
271           if (str->ReadCalibHeader()>0) {
272             // first check the type:
273             if (bScanInit[eqId] && type[eqId]!=str->GetHtype()) {
274               printf("Calib header problem. Type changed (%d -> %d)!\n",type[eqId],str->GetHtype());
275             }
276
277             // read calib values
278             routerNr[eqId]    = str->GetHrouterNr();
279             type[eqId]        = str->GetHtype();
280             dataFormat[eqId]  = str->GetHdataFormat();
281             triggers[eqId]    = str->GetHtriggers();
282             dacStart[eqId]    = str->GetHdacStart();
283             dacEnd[eqId]      = str->GetHdacEnd();
284             dacStep[eqId]     = str->GetHdacStep();
285             dacId[eqId]       = str->GetHdacId();
286             rowStart[eqId]    = str->GetHrowStart();
287             rowEnd[eqId]      = str->GetHrowEnd();
288             rowValue[eqId]    = str->GetHrowValue();
289             dacValue[eqId]    = str->GetHdacValue(); // this will change below for MEANTH scan
290
291             for (UInt_t hs=0; hs<6; hs++) {
292               halfStaveScanned[eqId][hs] = str->GetHhalfStaveScanned(hs);
293               dacHigh[eqId][hs]          = str->GetHdacHigh(hs);
294               dacLow[eqId][hs]           = str->GetHdacLow(hs);
295               TPAmp[eqId][hs]            = str->GetHTPAmp(hs);
296               for (UInt_t chip=0; chip<10; chip++) {
297                 chipPresent[eqId][hs][chip] = str->GetHchipPresent(hs,chip);
298                 if (type[eqId]==MEANTH && chipPresent[eqId][hs][chip]) dacValue[eqId] = str->GetHdacLow(hs);
299               }
300             }
301             for (UInt_t chip=0; chip<10; chip++) {
302               minTHchipPresent[eqId][chip] = str->GetHminTHchipPresent(chip);
303             }
304
305             currentStep[eqId] = (dacValue[eqId]-dacStart[eqId])/dacStep[eqId];
306             if (type[eqId]==DELAY) {
307               currentStep[eqId]=currentStep[eqId]*2;
308               dacValue[eqId]=dacValue[eqId]*2;
309               if (dacHigh[eqId][0]==128) { // misc_ctrl value
310                 currentStep[eqId]=currentStep[eqId]+1;
311                 dacValue[eqId]=dacValue[eqId]+1;
312               }
313             }
314
315             // router nr check:
316             if (routerNr[eqId]!=eqId) {
317               printf("Router nr problem? Router nr %d != EqID %d\n",routerNr[eqId],eqId);
318             }
319
320             if (!bScanInit[eqId]) {
321               // initialize container object
322               TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
323               switch (type[eqId]) {
324               case NOISE:
325               case UNIMA:
326                 scanObj[eqId] = new AliITSOnlineSPDscanSingle(fileName.Data());
327                 ((AliITSOnlineSPDscanSingle*)scanObj[eqId])->ClearThis();
328                 bScanInit[eqId]=kTRUE;
329                 break;
330               case MINTH:
331               case DAC:
332               case DELAY:
333                 scanObj[eqId] = new AliITSOnlineSPDscanMultiple(fileName.Data());
334                 scanObj[eqId]->ClearThis();
335                 bScanInit[eqId]=kTRUE;
336                 break;
337               case MEANTH: 
338                 scanObj[eqId] = new AliITSOnlineSPDscanMeanTh(fileName.Data());
339                 scanObj[eqId]->ClearThis();
340                 bScanInit[eqId]=kTRUE;
341                 break;
342               default:
343                 printf("Unknown scan type: %d.\n",type[eqId]);
344               }
345               // some multiple scan data
346               if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
347                 ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacId(dacId[eqId]);
348               }
349               // some common data
350               scanObj[eqId]->SetRunNr((UInt_t)runNr);
351               scanObj[eqId]->SetRouterNr(routerNr[eqId]);
352               for (UInt_t hs=0; hs<6; hs++) {
353                 scanObj[eqId]->SetHalfStaveScanned(hs,halfStaveScanned[eqId][hs]);
354               }
355               scanObj[eqId]->SetType(type[eqId]);
356               scanObj[eqId]->SetDataFormat(dataFormat[eqId]);
357               for (Int_t hs=0; hs<6; hs++) {          
358                 for (UInt_t chip=0; chip<10; chip++) {
359                   scanObj[eqId]->SetChipPresent(hs,chip,chipPresent[eqId][hs][chip]);
360                 }
361               }
362               scanObj[eqId]->SetRowStart(rowStart[eqId]);
363               scanObj[eqId]->SetRowEnd(rowEnd[eqId]);
364               scanObj[eqId]->SetDacStart(dacStart[eqId]);
365               scanObj[eqId]->SetDacEnd(dacEnd[eqId]);
366               scanObj[eqId]->SetDacStep(dacStep[eqId]);
367             }
368
369             if (type[eqId]==MINTH) {
370               scanObj[eqId]->SetTriggers(currentStep[eqId],triggers[eqId]);
371             }
372             if (type[eqId]==UNIMA || type[eqId]==NOISE) {
373               if (currentStep[eqId]==9999) printf("SPDcalibratorStep1 (eq %d): single step\n",eqId);
374               currentStep[eqId]=0;
375             }
376             if (type[eqId]==MINTH || type[eqId]==MEANTH || type[eqId]==DAC || type[eqId]==DELAY) {
377               ((AliITSOnlineSPDscanMultiple*)scanObj[eqId])->SetDacValue(currentStep[eqId],dacValue[eqId]);
378               if (type[eqId]==DELAY) {
379                 printf("SPDcalibratorStep1 (eq %d): DAC %d/%d , step %d\n",eqId,dacValue[eqId]/2,dacHigh[eqId][0],currentStep[eqId]);
380               }
381               else {
382                 printf("SPDcalibratorStep1 (eq %d): DAC %d , step %d\n",eqId,dacValue[eqId],currentStep[eqId]);
383               }
384             }
385             if (type[eqId]==MEANTH) {
386               for (Int_t hs=0; hs<6; hs++) {
387                 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacLow(currentStep[eqId],hs,dacLow[eqId][hs]);
388                 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetDacHigh(currentStep[eqId],hs,dacHigh[eqId][hs]);
389                 ((AliITSOnlineSPDscanMeanTh*)scanObj[eqId])->SetTPAmp(currentStep[eqId],hs,TPAmp[eqId][hs]);
390               }
391             }
392
393
394           }
395
396           if (bScanInit[eqId]) {
397             while (str->Next()) {
398               UInt_t hs = str->GetHalfStaveNr();
399               UInt_t chip = str->GetChipAddr();
400               //***remove last condition when minthpresent put correctly in calib header?
401 #ifndef SPD_DA_OFF
402               if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
403 #else
404               if (type[eqId]!=MINTH || minTHchipPresent[eqId][chip] || runNr<=416900) {
405 #endif
406                 //*************************************************************************
407                 scanObj[eqId]->IncrementHits(currentStep[eqId],hs,chip,str->GetChipCol(),str->GetChipRow());
408                 
409                 if (!hitEventHSIncremented[hs]) {
410                   scanObj[eqId]->IncrementHitEventsTot(currentStep[eqId],hs);
411                   hitEventHSIncremented[hs]=kTRUE;
412                 }
413                 
414                 if (!hitEventChipIncremented[hs][chip]) {
415                   scanObj[eqId]->IncrementHitEvents(currentStep[eqId],hs,chip);
416                   hitEventChipIncremented[hs][chip]=kTRUE;
417                 }
418               }
419
420             }
421
422             if (type[eqId]!=MINTH) { // for minth, triggers are set from header info
423               scanObj[eqId]->IncrementTriggers(currentStep[eqId]);
424             }
425
426           }
427
428         }
429
430         delete str;
431         delete reader;
432
433       }
434
435       /* free resources */
436       free(event);
437
438     }
439
440 #ifndef SPD_DA_OFF
441     daqDA_progressReport((unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
442 #else
443     printf("progress: %d\n",(unsigned int)( ((Float_t)(segNr-startSeg+1))/(argc-startSeg)*50 ));
444 #endif
445
446   }
447
448   
449   // clean up scan objects (which also saves them) , check if something happened...
450     Bool_t somethingHappened = kFALSE;
451   for (UInt_t eqId=0; eqId<20; eqId++) {
452     if (scanObj[eqId]!=NULL) {
453       delete scanObj[eqId];
454       somethingHappened = kTRUE;
455     }
456   }
457   if (!somethingHappened) {
458     printf("WARNING: No data processed. Are the calibration headers missing?\n");
459   }
460
461
462
463
464   // ********* STEP 2: Analyze scan container files. ************************************************
465
466   // clear noisyToFXS and DCSconfigToFXS dirs:
467   TString command = Form("cd %s; rm -f *",saveDirNoisyToFXS);
468   system(command.Data());
469   TString command2 = Form("cd %s; rm -f *",saveDirDCSconfigToFXS);
470   system(command2.Data());
471   UInt_t nrNoisyFilesProduced=0;
472   UInt_t nrDCSconfigFilesProduced=0;
473
474   AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
475   AliITSOnlineSPDscanAnalyzer *analyzer = NULL;
476   AliITSOnlineCalibrationSPDhandler* handlerPermNoisy = NULL;
477   // fill permanent noisy list to add later...
478   if (permstatus==0) { 
479     handlerPermNoisy = new AliITSOnlineCalibrationSPDhandler();
480     UInt_t permNoisy = handlerPermNoisy->ReadNoisyFromText(permNoisyFileName.Data(),240); // 240 = read for all modules
481     if (permNoisy>0) {
482       printf("%d noisy pixels read from permanent list.\n",permNoisy);
483     }
484   }
485
486   Bool_t reset_made = kFALSE;
487
488   // *** *** *** start loop over equipments (eq_id)
489   for (int eqId=0; eqId<20; eqId++) {
490
491     // create analyzer for this eq
492     TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
493     analyzer = new AliITSOnlineSPDscanAnalyzer(fileName.Data(),handler);
494
495     // configure analyzer with tuning parameters etc:
496     for (UInt_t i=0; i<nrTuningParams; i++) {
497       analyzer->SetParam(((TObjString*)paramNames.At(i))->GetString().Data(),((TObjString*)paramVals.At(i))->GetString().Data());
498     }
499
500     UInt_t type  = analyzer->GetType();
501     UInt_t dacId = analyzer->GetDacId();
502     UInt_t routerNr = analyzer->GetRouterNr();
503     if (type!=99) {
504       if (type==DAC) {
505         printf("SPD scan calibrator Step2: eqId %d, type %d, dacId %d\n",eqId,type,dacId);
506       }
507       else printf("SPD scan calibrator Step2: eqId %d type %d\n",eqId,type);  
508     }
509
510
511
512     // algorithms for the different types of scans:
513
514     if (type==UNIMA) {
515
516     }
517
518     else if (type==NOISE) {
519       // read previous noisy list (clear if overwriting)
520       handler->SetFileLocation(saveDirNoisy);
521       if (analyzer->IsOverWriteSet() && !reset_made) {
522         handler->ResetNoisy();
523         handler->WriteToFilesAlways();
524         reset_made=kTRUE;
525       }
526       else {
527         handler->ReadFromFiles();
528       }
529       if (analyzer->ProcessNoisyPixels(/*saveDirNoisy*/)) {
530         if (permstatus==0) {
531           handler->AddNoisyFrom(handlerPermNoisy);
532         }
533         // init dcs config text file
534         TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
535         ofstream dcsfile;
536         dcsfile.open(dcsConfigFileName.Data());
537         dcsfile << "[SPD SCAN]\n";
538         dcsfile << "RunNumber=" << runNr << "\n";
539         dcsfile << "Type=" << type << "\n";
540         dcsfile << "Router=" << routerNr << "\n";
541         dcsfile << "ActualDetConfiguration=" << "0,-1,-1\n"; // dummy values for now
542         dcsfile << "[NOISY]\n";
543         nrDCSconfigFilesProduced++;
544
545         for (UInt_t hs=0; hs<6; hs++) {
546           for (UInt_t chip=0; chip<10; chip++) {
547             if (analyzer->IsChipPresent(hs,chip) || analyzer->IsOverWriteSet()) {
548               dcsfile << "-" << eqId << "," << hs << "," << chip << "\n";
549               UInt_t nrNoisy = handler->GetNrNoisyC(eqId,hs,chip);
550               for (UInt_t ind=0; ind<nrNoisy; ind++) {
551                 UInt_t col = handler->GetNoisyColAtC(eqId,hs,chip,ind);
552                 UInt_t row = handler->GetNoisyRowAtC(eqId,hs,chip,ind);
553                 dcsfile << col << "," << row << "\n";
554               }
555             }
556           }
557         }
558         handler->SetFileLocation(saveDirNoisy);
559         handler->WriteNoisyToFile(eqId);
560         handler->SetFileLocation(saveDirNoisyToFXS);
561         handler->WriteNoisyToFile(eqId);
562         nrNoisyFilesProduced++;
563         
564         dcsfile.close();
565       }
566     }
567
568     else if (type==MINTH || (type==DAC && dacId==39)) {
569       // init dcs config text file
570       TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
571       ofstream dcsfile;
572       dcsfile.open(dcsConfigFileName.Data());
573       dcsfile << "[SPD SCAN]\n";
574       dcsfile << "RunNumber=" << runNr << "\n";
575       dcsfile << "Type=" << type << "\n";
576       dcsfile << "Router=" << routerNr << "\n";
577       dcsfile << "ActualDetConfiguration=" << "0,-1,-1\n"; // dummy values for now
578       dcsfile << "[DACvalues]\n";
579       nrDCSconfigFilesProduced++;
580       for (UInt_t hs=0; hs<6; hs++) {
581         for (UInt_t chipNr=0; chipNr<10; chipNr++) {
582           Int_t minTh = -1;
583           if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
584             minTh = analyzer->GetMinTh(hs,chipNr);
585             if (minTh!=-1) {
586               dcsfile << "39," << eqId << "," << hs << "," << chipNr << "=" << minTh << "\n";
587             }
588             else {
589               printf("MinTh failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
590             }
591           }
592         }
593       }
594       dcsfile.close();
595     }
596
597     else if (type==DELAY) {
598       // init dcs config text file
599       TString dcsConfigFileName = Form("%s/dcsConfig_run_%d_eq_%d.txt",saveDirDCSconfigToFXS,runNr,eqId);
600       ofstream dcsfile;
601       dcsfile.open(dcsConfigFileName.Data());
602       dcsfile << "[SPD SCAN]\n";
603       dcsfile << "RunNumber=" << runNr << "\n";
604       dcsfile << "Type=" << type << "\n";
605       dcsfile << "Router=" << routerNr << "\n";
606       dcsfile << "ActualDetCoonfiguration=" << "0,-1,-1\n"; // dummy values for now
607       dcsfile << "[DACvalues]\n";
608       nrDCSconfigFilesProduced++;
609       for (UInt_t hs=0; hs<6; hs++) {
610         for (UInt_t chipNr=0; chipNr<10; chipNr++) {
611           Int_t clockCycle = -1;
612           Int_t delayCtrl = -1;
613           Int_t miscCtrl = -1;
614           if (analyzer->GetOnlineScan()->GetChipPresent(hs,chipNr)) {
615             clockCycle = analyzer->GetDelay(hs,chipNr);
616             delayCtrl = clockCycle/2;
617             miscCtrl = 192;
618             if (clockCycle!=-1) {
619               if (clockCycle%2==1) miscCtrl = 128;
620               dcsfile << "42," << eqId << "," << hs << "," << chipNr << "=" << delayCtrl << "\n";
621               dcsfile << "43," << eqId << "," << hs << "," << chipNr << "=" << miscCtrl << "\n";
622             }
623             else {
624               printf("Delay failed for Eq %d , HS %d , Chip %d\n",eqId,hs,chipNr);
625             }
626           }
627         }
628       }
629       dcsfile.close();
630     }
631
632     delete analyzer;
633
634 #ifndef SPD_DA_OFF
635     daqDA_progressReport((unsigned int)(50+(eqId+1)*2.5));
636 #else
637     printf("progress: %d\n",(unsigned int)(50+(eqId+1)*2.5));
638 #endif
639
640   }
641   // *** *** *** end loop over equipments (eq_id)
642
643
644   delete handler;
645   if (handlerPermNoisy!=NULL) {
646     delete handlerPermNoisy;
647   }
648
649   printf("Opening id list file\n");
650   TString idsFXSFileName = Form("%s/FXSids_run_%d.txt",saveDirIdsToFXS,runNr);
651   ofstream idsFXSfile;
652   idsFXSfile.open(idsFXSFileName.Data());
653
654
655
656   // send noisy data to FXS
657   if (nrNoisyFilesProduced>0) {
658     printf("Preparing noisy files\n");
659     // send a tared file of all new noisy maps
660     TString command = Form("cd %s; tar -cf noisy_scan.tar *",saveDirNoisyToFXS);
661     //    printf("\n\n%s\n\n",command.Data());
662     system(command.Data());
663     TString fileName = Form("%s/noisy_scan.tar",saveDirNoisyToFXS);
664     TString id = "SPD_scan_noisy";
665 #ifndef SPD_DA_OFF
666     Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
667     if (status!=0) {
668       printf("Failed to export file %s , status %d\n",fileName.Data(),status);
669       return -1;
670     }
671 #endif
672     idsFXSfile << Form("%s\n",id.Data());
673   }
674
675   // send dcs config files to FXS
676   if (nrDCSconfigFilesProduced>0) {
677     printf("Preparing DCS config files\n");
678     // send a tared file of all the dcsConfig text files
679     TString command = Form("cd %s; tar -cf dcsConfig.tar *",saveDirDCSconfigToFXS);
680     //    printf("\n\n%s\n\n",command.Data());
681     system(command.Data());
682     TString fileName = Form("%s/dcsConfig.tar",saveDirDCSconfigToFXS);
683     TString id = "SPD_dcsConfig";
684 #ifndef SPD_DA_OFF
685     Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
686     if (status!=0) {
687       printf("Failed to export file %s , status %d\n",fileName.Data(),status);
688       return -1;
689     }
690 #endif
691     //idsFXSfile << Form("%s\n",id.Data()); // do NOT write this id (this is not for preprocessor)
692   }
693
694   // send reference data to FXS
695   for (UInt_t eqId=0; eqId<20; eqId++) {
696     if (bScanInit[eqId]) {
697       printf("Preparing reference data for eq %d\n",eqId);
698       TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",saveDirRef,runNr,eqId);
699       TString id = Form("SPD_ref_scan_%d",eqId);
700 #ifndef SPD_DA_OFF
701       Int_t status = daqDA_FES_storeFile(fileName.Data(),id.Data());
702       if (status!=0) {
703         printf("Failed to export file %s , status %d\n",fileName.Data(),status);
704         return -1;
705       }
706 #endif
707       idsFXSfile << Form("%s\n",id.Data());
708     }
709   }
710
711
712   printf("Preparing id list file\n");
713   idsFXSfile.close();
714   TString id = "SPD_id_list";
715 #ifndef SPD_DA_OFF
716   status = daqDA_FES_storeFile(idsFXSFileName.Data(),id.Data());
717   if (status!=0) {
718     printf("Failed to export file %s , status %d\n",idsFXSFileName.Data(),status);
719     return -1;
720   }
721 #endif
722
723
724   printf("DA finished.\n");
725
726   return 0;
727 }