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