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