8c73a6f963ee69249070f3638b39d40a0fd9ae21
[u/mrichter/AliRoot.git] / TOF / AliTOFPreprocessor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18
19 //#include <Riostream.h>
20 //#include <stdio.h>
21 //#include <stdlib.h>
22
23 #include <TFile.h>
24 #include <TH2S.h>
25 #include <TMath.h>
26 #include <TObjArray.h>
27 #include <TObjString.h>
28 #include <TTimeStamp.h>
29
30 #include "AliCDBMetaData.h"
31 #include "AliCDBEntry.h"
32 #include "AliLog.h"
33 #include "AliTOFChannelOnlineArray.h"
34 #include "AliTOFChannelOnlineStatusArray.h"
35 #include "AliTOFDataDCS.h"
36 #include "AliTOFGeometry.h"
37 #include "AliTOFPreprocessor.h"
38 #include "AliTOFFEEReader.h"
39 #include "AliTOFRawStream.h"
40 #include "AliTOFCableLengthMap.h"
41 #include "AliTOFcalibHisto.h"
42
43
44 // TOF preprocessor class.
45 // It takes data from DCS and passes them to the class AliTOFDataDCS, which
46 // processes them. The result is then written to the CDB.
47 // Analogously, it takes data form DAQ (both at Run level and inclusive - 
48 // of all the runs - level, processes them, and stores both Reference Data
49 // and Online Calibration files in the CDB. 
50 // Processing of Pulser/Noise Run data and of TOF FEE DCS map included also.
51
52 // return codes:
53 // return=0 : all ok
54 // return=1 : no DCS input data Map
55 // return=2 : no DCS input data processing
56 // return=3 : no DCS processed data was stored in Ref Data
57 // return=4 : no DAQ input for Ref Data
58 // return=5 : failed to store DAQ Ref Data
59 // return=6 : failed to retrieve DAQ data for calibration 
60 // return=7 : problems in processing histos in the input DAQ file 
61 // return=8 : failed to store Online Delays
62 // return=9 : failed to store Reference Data for Pulser
63 // return=10: failed to retrieve Pulser data 
64 // return=11: failed to store Pulser map in OCDB
65 // return=12: failed to store Reference Data for Noise
66 // return=13: failed to retrieve Noise data 
67 // return=14: failed to store Noise map in OCDB
68 // return=15: failed to retrieve FEE data from FXS
69 // return=16: failed to retrieve FEE data from OCDB
70 // return=17: failed to store FEE data in OCDB
71 // return=18: failed to store FEE reference data in OCDB
72 // return=20: failed in retrieving status variable
73
74 ClassImp(AliTOFPreprocessor)
75
76 const Int_t    AliTOFPreprocessor::fgkBinRangeAve = 13;    // number of bins where to calculate the mean 
77 const Double_t AliTOFPreprocessor::fgkIntegralThr = 100;   // min number of entries to perform computation of delay per channel 
78 const Double_t AliTOFPreprocessor::fgkThrPar      = 0.013; // parameter used to trigger the calculation of the delay
79
80 //_____________________________________________________________________________
81
82 AliTOFPreprocessor::AliTOFPreprocessor(AliShuttleInterface* shuttle) :
83   AliPreprocessor("TOF", shuttle),
84   fData(0),
85   fCal(0),
86   fNChannels(0),
87   fStoreRefData(kTRUE),
88   fFDRFlag(kFALSE),
89   fStatus(0),
90   fMatchingWindow(0),
91   fLatencyWindow(0)
92 {
93   // constructor
94   AddRunType("PHYSICS");
95   AddRunType("PULSER");
96   AddRunType("NOISE");
97
98 }
99
100 //_____________________________________________________________________________
101
102 AliTOFPreprocessor::~AliTOFPreprocessor()
103 {
104   // destructor
105 }
106
107 //______________________________________________________________________________
108 void AliTOFPreprocessor::Initialize(Int_t run, UInt_t startTime,
109         UInt_t endTime)
110 {
111   // Creates AliTOFDataDCS object
112
113   AliPreprocessor::Initialize(run, startTime, endTime);
114
115         AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s \n\tStartTime DCS Query %s \n\tEndTime DCS Query %s", run,
116                 TTimeStamp(startTime).AsString(),
117                 TTimeStamp(endTime).AsString(), ((TTimeStamp)GetStartTimeDCSQuery()).AsString(), ((TTimeStamp)GetEndTimeDCSQuery()).AsString()));
118
119         fData = new AliTOFDataDCS(fRun, fStartTime, fEndTime, GetStartTimeDCSQuery(), GetEndTimeDCSQuery());
120         fNChannels = AliTOFGeometry::NSectors()*(2*(AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB())+AliTOFGeometry::NStripA())*AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX();
121 }
122 //_____________________________________________________________________________
123 Bool_t AliTOFPreprocessor::ProcessDCS(){
124
125   // check whether DCS should be processed or not...
126
127   TString runType = GetRunType();
128   Log(Form("RunType %s",runType.Data()));
129
130   if (runType != "PHYSICS"){
131     return kFALSE;
132   }
133
134   return kTRUE;
135 }
136 //_____________________________________________________________________________
137
138 UInt_t AliTOFPreprocessor::ProcessDCSDataPoints(TMap* dcsAliasMap)
139 {
140   // Fills data into a AliTOFDataDCS object
141
142
143   Log("Processing DCS DP");
144   TH1::AddDirectory(0);
145
146   Bool_t resultDCSMap=kFALSE;
147   Bool_t resultDCSStore=kFALSE;
148
149   // processing DCS
150
151   fData->SetFDRFlag(fFDRFlag);
152   
153   if (!dcsAliasMap){
154     Log("No DCS map found: TOF exiting from Shuttle");
155     if (fData){
156             delete fData;
157             fData = 0;
158     }
159     return 1;// return error Code for DCS input data not found 
160   }
161   else {
162
163   // The processing of the DCS input data is forwarded to AliTOFDataDCS
164     resultDCSMap=fData->ProcessData(*dcsAliasMap);
165     if(!resultDCSMap){
166       Log("Some problems occurred while processing DCS data, TOF exiting from Shuttle");
167       if (fData){
168               delete fData;
169               fData = 0;
170       }
171       return 2;// return error Code for processed DCS data not stored 
172     }
173     else{
174       AliCDBMetaData metaDataDCS;
175       metaDataDCS.SetBeamPeriod(0);
176       metaDataDCS.SetResponsible("Chiara Zampolli");
177       metaDataDCS.SetComment("This preprocessor fills an AliTOFDataDCS object.");
178       AliInfo("Storing DCS Data");
179       resultDCSStore = StoreReferenceData("Calib","DCSData",fData, &metaDataDCS);
180       if (!resultDCSStore){
181         Log("Some problems occurred while storing DCS data results in Reference Data, TOF exiting from Shuttle");
182         if (fData){
183                 delete fData;
184                 fData = 0;
185         }
186         return 3;// return error Code for processed DCS data not stored 
187                  // in reference data
188         
189       }
190     }
191   }
192   if (fData){
193           delete fData;
194           fData = 0;
195   }
196   
197   return 0;
198 }
199 //_____________________________________________________________________________
200
201 UInt_t AliTOFPreprocessor::ProcessOnlineDelays()
202 {
203   // Processing data from DAQ for online calibration 
204
205   Bool_t updateOCDB = kFALSE;
206   Log("Processing DAQ delays");
207
208   // reading configuration map 
209   TString compDelays = "kFALSE";
210   Int_t deltaStartingRun = fRun;
211   Int_t startingRun = fRun-deltaStartingRun;
212   Int_t binRangeAve = fgkBinRangeAve;
213   Double_t integralThr = fgkIntegralThr;
214   Double_t thrPar = fgkThrPar;
215
216   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","Config");
217   if (!cdbEntry) {
218           Log(Form("No Configuration entry found in CDB, using default values: ComputingDelays = %s, StartingRun = %i",compDelays.Data(), startingRun));
219   }
220   else {
221           TMap *configMap = (TMap*)cdbEntry->GetObject();
222           if (!configMap){
223                   Log(Form("No map found in Config entry in CDB, using default values: ComputingDelays = %s, StartingRun = %i",compDelays.Data(), startingRun));
224           }
225           else{
226                   TObjString *strDelays = (TObjString*)configMap->GetValue("ComputingDelays");
227                   if (strDelays) {
228                           compDelays = (TString) strDelays->GetString();
229                   }
230                   else {
231                           Log(Form("No ComputingDelays value found in Map from Config entry in CDB, using default value: ComputingDelays =  %s",compDelays.Data()));
232                   }
233                   TObjString *strRun = (TObjString*)configMap->GetValue("StartingRun");
234                   if (strRun) {
235                           TString tmpstr = strRun->GetString();
236                           startingRun = tmpstr.Atoi();
237                           deltaStartingRun = fRun - startingRun;
238                   }
239                   else {
240                           Log(Form("No StartingRun value found in Map from Config entry in CDB, using default value: StartingRun = %i",startingRun));
241                   }
242                   TObjString *strBinRangeAve = (TObjString*)configMap->GetValue("BinRangeAve");
243                   if (strBinRangeAve) {
244                           TString tmpstr = strBinRangeAve->GetString();
245                           binRangeAve = tmpstr.Atoi();
246                   }
247                   else {
248                           Log(Form("No BinRangeAve value found in Map from Config entry in CDB, using default value: BinRangeAve = %i",binRangeAve));
249                   }
250                   TObjString *strIntegralThr = (TObjString*)configMap->GetValue("IntegralThr");
251                   if (strIntegralThr) {
252                           TString tmpstr = strIntegralThr->GetString();
253                           integralThr = tmpstr.Atof();
254                   }
255                   else {
256                           Log(Form("No IntegralThr value found in Map from Config entry in CDB, using default value: IntegralThr = %i",integralThr));
257                   }
258                   TObjString *strThrPar = (TObjString*)configMap->GetValue("ThrPar");
259                   if (strThrPar) {
260                           TString tmpstr = strThrPar->GetString();
261                           thrPar = tmpstr.Atof();
262                   }
263                   else {
264                           Log(Form("No ThrPar value found in Map from Config entry in CDB, using default value: ThrPar = %i",thrPar));
265                   }
266           }
267   }
268   if (compDelays == "kTRUE") fFDRFlag = kFALSE;
269   else fFDRFlag = kTRUE;
270
271   delete cdbEntry;
272   cdbEntry = 0x0;
273
274   Log(Form("ComputingDelays = %s, StartingRun = %i",compDelays.Data(),startingRun));
275
276   /* init array with current calibration, if any */
277   fCal = new AliTOFChannelOnlineArray(fNChannels);  
278   AliTOFChannelOnlineArray *curCal = NULL;
279
280   AliCDBEntry *cdbEntry2 = GetFromOCDB("Calib","ParOnlineDelay");
281   if (!cdbEntry2 || !cdbEntry2->GetObject()) {
282     /* no CDB entry found. set update flag */
283     Log("     ************ WARNING ************");
284     Log("No CDB ParOnlineDelay entry found, creating a new one!");
285     Log("     *********************************");
286     updateOCDB = kTRUE;
287   }
288   else {
289     Log("Found previous ParOnlineDelay entry. Using it to init calibration");
290     curCal = (AliTOFChannelOnlineArray *)cdbEntry2->GetObject();
291     for (Int_t i = 0; i < fNChannels; i++)
292       fCal->SetDelay(i, curCal->GetDelay(i));
293   }
294  
295
296   TH1::AddDirectory(0);
297
298   Bool_t resultDAQRef=kFALSE;
299   Bool_t resultTOFPP=kFALSE;
300   TH2S *h2 = 0x0;
301   // processing DAQ
302   
303   TFile * daqFile=0x0;
304   
305   if(fStoreRefData){
306     //retrieving data at Run level
307           TList* list = GetFileSources(kDAQ, "RUNLevel");
308           if (list !=0x0 && list->GetEntries()!=0)
309                   {
310                           AliInfo("The following sources produced files with the id RUNLevel");
311                           list->Print();
312                           for (Int_t jj=0;jj<list->GetEntries();jj++){
313                                   TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
314                                   AliInfo(Form("found source %s", str->String().Data()));
315                                   // file to be stored run per run
316                                   TString fileNameRun = GetFile(kDAQ, "RUNLevel", str->GetName());
317                                   if (fileNameRun.Length()>0){
318                                           AliInfo(Form("Got the file %s, now we can store the Reference Data for the current Run.", fileNameRun.Data()));
319                                           daqFile = new TFile(fileNameRun.Data(),"READ");
320                                           h2 = (TH2S*) daqFile->Get("htof");
321                                           AliCDBMetaData metaDataHisto;
322                                           metaDataHisto.SetBeamPeriod(0);
323                                           metaDataHisto.SetResponsible("Chiara Zampolli");
324                                           metaDataHisto.SetComment("This preprocessor stores the array of histos object as Reference Data.");
325                                           AliInfo("Storing Reference Data");
326                                           resultDAQRef = StoreReferenceData("Calib","DAQData",h2, &metaDataHisto);
327                                           if (!resultDAQRef){
328                                                   Log("some problems occurred::No Reference Data stored, TOF exiting from Shuttle");
329                                                   delete h2;
330                                                   delete list;
331                                                   delete fCal;
332                                                   fCal=0x0;
333                                                   return 5;//return error code for failure in storing Ref Data 
334                                           }
335                                           daqFile->Close();
336                                           delete daqFile;
337                                   }
338                                   
339                                   else{
340                                           Log("The input data file from DAQ (run-level) was not found, TOF exiting from Shuttle "); 
341                                           delete list;
342                                           delete fCal;
343                                           fCal=0x0;
344                                           return 4;//return error code for failure in retrieving Ref Data 
345                                   }
346                           }
347                           delete list;
348                   }
349           else{
350                   Log("The input data file list from DAQ (run-level) was not found, TOF exiting from Shuttle "); 
351                   delete fCal;
352                   fCal=0x0;
353                   return 4;//return error code for failure in retrieving Ref Data 
354           }     
355   }
356
357
358   //Total files, with cumulative histos
359   
360   TList* listTot = GetFileSources(kDAQ, "DELAYS");
361   if (listTot !=0x0 && listTot->GetEntries()!=0)
362           {
363                   AliInfo("The following sources produced files with the id DELAYS");
364                   listTot->Print();
365                   for (Int_t jj=0;jj<listTot->GetEntries();jj++){
366                           TObjString * str = dynamic_cast<TObjString*> (listTot->At(jj));
367                           AliInfo(Form("found source %s", str->String().Data()));
368                           
369                           // file with summed histos, to extract calib params
370                           TString fileName = GetFile(kDAQ, "DELAYS", str->GetName());
371                           if (fileName.Length()>0){
372                                   AliInfo(Form("Got the file %s, now we can extract some values.", fileName.Data()));
373                                   
374                                   daqFile = new TFile(fileName.Data(),"READ");
375                                   if (h2) delete h2;
376                                   h2 = (TH2S*) daqFile->Get("htoftot");
377                                   if (!h2){
378                                           Log("some problems occurred:: No histo retrieved, TOF exiting from Shuttle");
379                                           delete listTot;
380                                           delete daqFile;
381                                           delete fCal;
382                                           fCal=0x0;
383                                           return 7; //return error code for histograms not existing/junky
384                                   }
385                                   else {
386                                           static const Int_t kSize=h2->GetNbinsX();
387                                           static const Int_t kNBins=h2->GetNbinsY();
388                                           static const Double_t kXBinmin=h2->GetYaxis()->GetBinLowEdge(1);
389                                           if (kSize != fNChannels){
390                                                   Log(" number of bins along x different from number of pads, found only a subset of the histograms, TOF exiting from Shuttle");
391                                                   delete listTot;
392                                                   delete h2;
393                                                   delete daqFile;
394                                                   delete fCal;
395                                                   fCal=0x0;
396                                                   return 7; //return error code for histograms not existing/junky
397                                           }
398                                           Int_t nNotStatistics = 0; // number of channel with not enough statistics
399
400                                           /* FDR flag set. do not compute delays, use nominal cable delays */
401                                           if (fFDRFlag) {
402
403                                             Log(" Not computing delays according to flag set in Config entry in OCDB!");
404                                             FillWithCosmicCalibration(fCal);
405
406                                             /* check whether the new calibration is different from the previous one */
407                                             if (curCal) { /* well, check also whether we have a previous calibration */
408                                               for (Int_t i = 0; i < fNChannels; i++) {
409                                                 if (fCal->GetDelay(i) != curCal->GetDelay(i)) {
410                                                   updateOCDB = kTRUE;
411                                                   break;
412                                                 }
413                                               }
414                                             }
415                                             else /* otherwise update OCDB */
416                                               updateOCDB = kTRUE;
417
418                                           }
419
420                                           else {  // computing delays if not in FDR runs
421
422                                             updateOCDB = kTRUE; /* always update OCDB when computing delays */
423
424                                                   for (Int_t ich=0;ich<kSize;ich++){
425                                                           /* check whether channel has been read out during current run.
426                                                            * if the status is bad it means it has not been read out.
427                                                            * in this case skip channel in order to not affect the mean */ 
428                                                           if (fStatus->GetHWStatus(ich) == AliTOFChannelOnlineStatusArray::kTOFHWBad){
429                                                                   AliDebug(2,Form(" Channel %i found bad according to FEEmap, (HW status = %i), skipping from delay computing",ich, (Int_t)fStatus->GetHWStatus(ich)));
430                                                                   continue;
431                                                           }
432                                                           AliDebug(2,Form(" Channel %i found ok according to FEEmap, starting delay computing",ich));
433                                                           TH1S *h1 = new TH1S("h1","h1",kNBins,kXBinmin-0.5,kNBins*1.+kXBinmin-0.5);
434                                                           for (Int_t ibin=0;ibin<kNBins;ibin++){
435                                                                   h1->SetBinContent(ibin+1,h2->GetBinContent(ich+1,ibin+1));
436                                                           }
437                                                           if(h1->Integral()<integralThr) {
438                                                                   nNotStatistics++;
439                                                                   Log(Form(" Not enough statistics for bin %i, skipping this channel",ich));  // printing message only if not in FDR runs
440                                                                   delete h1;
441                                                                   h1=0x0;
442                                                                   continue;
443                                                           }
444                                                           Bool_t found=kFALSE; 
445                                                           Float_t minContent=h1->Integral()*thrPar; 
446                                                           Int_t nbinsX = h1->GetNbinsX();
447                                                           Int_t startBin=1;
448                                                           for (Int_t j=1; j<=nbinsX; j++){
449                                                                   if ((
450                                                                        h1->GetBinContent(j) +     
451                                                                        h1->GetBinContent(j+1)+
452                                                                        h1->GetBinContent(j+2)+ 
453                                                                        h1->GetBinContent(j+3))>minContent){
454                                                                           found=kTRUE;
455                                                                           startBin=j;
456                                                                           break;
457                                                                   }
458                                                           }
459                                                           if(!found) AliInfo(Form("WARNING!!! no start of fit found for histo # %i",ich));
460                                                           // Now calculate the mean over the interval. 
461                                                           Double_t mean = 0;
462                                                           Double_t sumw2 = 0;
463                                                           Double_t nent = 0;
464                                                           for(Int_t k=0;k<binRangeAve;k++){
465                                                                   mean=mean+h1->GetBinCenter(startBin+k)*h1->GetBinContent(startBin+k);                 
466                                                                   nent=nent+h1->GetBinContent(startBin+k);                 
467                                                                   sumw2=sumw2+(h1->GetBinCenter(startBin+k))*(h1->GetBinCenter(startBin+k))*(h1->GetBinContent(startBin+k));
468                                                           }
469                                                           mean= mean/nent; //<x>
470                                                           sumw2=sumw2/nent; //<x^2>
471                                                           Double_t rmsmean= 0;
472                                                           rmsmean = TMath::Sqrt((sumw2-mean*mean)/nent);
473                                                           if (ich<fNChannels) {
474                                                                   Float_t delay = mean*AliTOFGeometry::TdcBinWidth()*1.E-3; // delay in ns
475                                                                   fCal->SetDelay(ich,delay);  // delay in ns
476                                                                   AliDebug(2,Form("Setting delay %f (ns) for channel %i",delay,ich));
477                                                           }
478                                                           delete h1;
479                                                           h1=0x0;
480                                                   }
481                                           }
482                                           if (nNotStatistics!=0) Log(Form("Too little statistics for %d channels!",nNotStatistics)); 
483                                   }
484                                   delete h2;
485                                   daqFile->Close();
486                                   delete daqFile;
487                           }
488                           else{
489                                   Log("The Cumulative data file from DAQ does not exist, TOF exiting from Shuttle"); 
490                                   delete listTot;
491                                   delete fCal;
492                                   fCal=0x0;
493                                   return 6;//return error code for problems in retrieving DAQ data 
494                           }
495                   }
496                   delete listTot;
497           }
498   else{
499     Log("Problem: no list for Cumulative data file from DAQ was found, TOF exiting from Shuttle");
500     delete fCal;
501     fCal=0x0;
502     return 6; //return error code for problems in retrieving DAQ data 
503   }
504
505   /* check whether we don't need to update OCDB.
506    * in this case we can return without errors and
507    * the current FEE is stored in the fStatus object. */
508   if (!updateOCDB) {
509     AliInfo("update OCDB flag not set. Do not overwrite stored file.");
510     return 0; /* return ok */
511   }
512   
513   daqFile=0;
514   AliCDBMetaData metaData;
515   metaData.SetBeamPeriod(0);
516   metaData.SetResponsible("Chiara Zampolli");
517   metaData.SetComment("This preprocessor fills an AliTOFChannelOnlineArray object for online calibration - delays.");
518   AliInfo("Storing Calibration Data");
519   resultTOFPP = Store("Calib","ParOnlineDelay",fCal, &metaData,deltaStartingRun,kTRUE);
520   if(!resultTOFPP){
521     Log("Some problems occurred while storing online object resulting from DAQ data processing");
522     delete fCal;
523     fCal=0x0;
524     return 8;//return error code for problems in storing DAQ data 
525   }
526
527   if (fCal){
528     delete fCal;
529     fCal = 0;
530   }
531
532   return 0;
533 }
534 //_____________________________________________________________________________
535
536 UInt_t AliTOFPreprocessor::ProcessPulserData()
537 {
538   // Processing Pulser Run data for TOF channel status
539
540   Log("Processing Pulser");
541
542   if (fStatus==0x0){
543           AliError("No valid fStatus found, some errors must have occurred!!");
544           return 20;
545   }
546
547   TH1::AddDirectory(0);
548   
549   Bool_t resultPulserRef=kFALSE;
550   Bool_t resultPulser=kFALSE;
551   
552   static const Int_t kSize = AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors();
553   TH1S * htofPulser = new TH1S("hTOFpulser","histo with signals on TOF during pulser", kSize,-0.5,kSize-0.5);
554   for (Int_t ibin =1;ibin<=kSize;ibin++){
555           htofPulser->SetBinContent(ibin,-1);
556   }
557   
558   // processing pulser
559   
560   TFile * daqFile=0x0;
561   TH1S *h1=0x0;
562   
563   //retrieving Pulser data 
564   TList* listPulser = GetFileSources(kDAQ, "PULSER");
565   if (listPulser !=0x0 && listPulser->GetEntries()!=0)
566           {
567                   AliInfo("The following sources produced files with the id PULSER");
568                   listPulser->Print();
569                   Int_t nPulser = 0;
570                   for (Int_t jj=0;jj<listPulser->GetEntries();jj++){
571                           Int_t nPulserSource = 0;
572                           TObjString * str = dynamic_cast<TObjString*> (listPulser->At(jj));
573                           AliInfo(Form("found source %s", str->String().Data()));
574                           // file to be stored run per run
575                           TString fileNamePulser = GetFile(kDAQ, "PULSER", str->GetName());
576                           if (fileNamePulser.Length()>0){
577                                   // storing refernce data
578                                   AliInfo(Form("Got the file %s, now we can process pulser data.", fileNamePulser.Data()));
579                                   daqFile = new TFile(fileNamePulser.Data(),"READ");
580                                   h1 = (TH1S*) daqFile->Get("hTOFpulser");
581                                   for (Int_t ibin=0;ibin<kSize;ibin++){
582                                           if ((h1->GetBinContent(ibin+1))!=-1){
583                                                   if ((htofPulser->GetBinContent(ibin+1))==-1){
584                                                           htofPulser->SetBinContent(ibin+1,h1->GetBinContent(ibin+1));
585                                                   }
586                                                   else {
587                                                           Log(Form("Something strange occurred during Pulser run, channel %i already read by another LDC, please check!",ibin));
588                                                   }
589                                           }
590                                   }
591                                   
592                                   // elaborating infos
593                                   Double_t mean =0;
594                                   Int_t nread=0;
595                                   Int_t nreadNotEmpty=0;
596                                   for (Int_t ientry=1;ientry<=h1->GetNbinsX();ientry++){
597                                           
598                                           AliDebug(3,Form(" channel %i pulser status before pulser = %i, with global status = %i",ientry,(Int_t)fStatus->GetPulserStatus(ientry),(Int_t)fStatus->GetStatus(ientry)));
599                                           /* check whether channel has been read out during current run.
600                                            * if the status is bad it means it has not been read out.
601                                            * in this case skip channel in order to not affect the mean */ 
602                                           if (fStatus->GetHWStatus(ientry-1) == AliTOFChannelOnlineStatusArray::kTOFHWBad)
603                                                   continue;
604                                           nPulser++;
605                                           nPulserSource++;
606                                           if (h1->GetBinContent(ientry)==-1) continue;
607                                           else {
608                                                   if (h1->GetBinContent(ientry)>0) {
609                                                           nreadNotEmpty++;
610                                                           AliDebug(2,Form(" channel %i is ok with entry = %f; so far %i channels added ",ientry-1,h1->GetBinContent(ientry),nreadNotEmpty));
611                                                   }
612                                                   mean+=h1->GetBinContent(ientry);
613                                                   nread++;
614                                           }
615                                   }
616                                   if (nread!=0) {
617                                           mean/=nread;
618                                           AliDebug(2,Form(" nread =  %i , nreadNotEmpty = %i, mean = %f",nread,nreadNotEmpty,mean));
619                                           for (Int_t ich =0;ich<fNChannels;ich++){
620                                                   if (h1->GetBinContent(ich+1)==-1) continue;
621                                                   AliDebug(3,Form(" channel %i pulser status before pulser = %i",ich,(Int_t)fStatus->GetPulserStatus(ich)));
622                                                   
623                                                   /* check whether channel has been read out during current run.
624                                                    * if the status is bad it means it has not been read out.
625                                                    * in this case skip channel in order to leave its status 
626                                                    * unchanged */
627                                                   if (fStatus->GetHWStatus(ich) == AliTOFChannelOnlineStatusArray::kTOFHWBad)
628                                                           continue;
629                                                   
630                                                   if (h1->GetBinContent(ich+1)<0.05*mean){
631                                                           fStatus->SetPulserStatus(ich,AliTOFChannelOnlineStatusArray::kTOFPulserBad);  // bad status for pulser
632                                                           AliDebug(2,Form( " channel %i pulser status after pulser = %i (bad, content = %f), with global status = %i",ich,(Int_t)fStatus->GetPulserStatus(ich),h1->GetBinContent(ich+1),(Int_t)fStatus->GetStatus(ich)));
633                                                   }
634                                                   else {
635                                                           fStatus->SetPulserStatus(ich,AliTOFChannelOnlineStatusArray::kTOFPulserOk);  // good status for pulser
636                                                           AliDebug(2,Form( " channel %i pulser status after pulser = %i (good), with global status = %i",ich,(Int_t)fStatus->GetPulserStatus(ich),(Int_t)fStatus->GetStatus(ich)));
637                                                   }
638                                           }
639                                   }
640                                   else {
641                                           Log("No channels read!! No action taken, keeping old status");
642                                   }
643                                   
644                                   daqFile->Close();
645                                   delete daqFile;
646                                   delete h1;
647                           }
648                           
649                           else{
650                                   Log("The input data file from DAQ (pulser) was not found, TOF exiting from Shuttle "); 
651                                   delete listPulser;
652                                   delete htofPulser;
653                                   htofPulser = 0x0;
654                                   if (fStatus){
655                                           delete fStatus;
656                                           fStatus = 0;
657                                   }
658                                   return 10;//return error code for failure in retrieving Ref Data 
659                           }
660                           AliDebug(2,Form(" Number of channels processed during pulser run from source %i = %i",jj, nPulserSource));             
661                   }
662                   AliDebug(2,Form(" Number of channels processed during pulser run = %i",nPulser));
663                   delete listPulser;
664           }
665   
666   else{
667           Log("The input data file list from DAQ (pulser) was not found, TOF exiting from Shuttle "); 
668           delete htofPulser;
669           htofPulser = 0x0;
670           if (fStatus){
671                   delete fStatus;
672                   fStatus = 0;
673           }
674           return 10;//return error code for failure in retrieving Ref Data 
675   }     
676   
677   //storing in OCDB  
678   
679   AliCDBMetaData metaData;
680   metaData.SetBeamPeriod(0);
681   metaData.SetResponsible("Chiara Zampolli");
682   metaData.SetComment("This preprocessor fills an AliTOFChannelOnlineStatusArray object after a Pulser run.");
683   AliInfo("Storing Calibration Data from Pulser Run");
684   resultPulser = Store("Calib","Status",fStatus, &metaData,0,kTRUE);
685   if(!resultPulser){
686     Log("Some problems occurred while storing online object resulting from Pulser data processing");
687     delete htofPulser;
688     htofPulser = 0x0;
689     if (fStatus){
690             delete fStatus;
691             fStatus = 0;
692     }
693     return 11;//return error code for problems in storing Pulser data 
694   }
695
696   if(fStoreRefData){
697     
698     AliCDBMetaData metaDataHisto;
699     metaDataHisto.SetBeamPeriod(0);
700     metaDataHisto.SetResponsible("Chiara Zampolli");
701     char comment[200];
702     sprintf(comment,"This preprocessor stores the Ref data from a pulser run.");
703     metaDataHisto.SetComment(comment);
704     AliInfo("Storing Reference Data");
705     resultPulserRef = StoreReferenceData("Calib","PulserData",htofPulser, &metaDataHisto);
706     if (!resultPulserRef){
707       Log("some problems occurred::No Reference Data for pulser stored, TOF exiting from Shuttle");
708       delete htofPulser;
709       htofPulser = 0x0;
710       if (fStatus){
711               delete fStatus;
712               fStatus = 0;
713       }
714       return 9;//return error code for failure in storing Ref Data 
715     }
716   }
717   
718   daqFile=0;
719
720   delete htofPulser;
721   htofPulser = 0x0;
722
723   if (fStatus){
724     delete fStatus;
725     fStatus = 0;
726   }
727
728   return 0;
729 }
730 //_____________________________________________________________________________
731
732 UInt_t AliTOFPreprocessor::ProcessNoiseData()
733 {
734
735   // Processing Noise Run data for TOF channel status
736
737   Log("Processing Noise");
738
739   if (fStatus==0x0){
740           AliError("No valid fStatus found, some errors must have occurred!!");
741           return 20;
742   }
743
744   Float_t noiseThr = 1;   // setting default threshold for noise to 1 Hz
745   // reading config map
746   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","ConfigNoise");
747   if (!cdbEntry) {
748           Log(Form("No Configuration entry found in CDB, using default values: NoiseThr = %d",noiseThr));
749   }
750   else {
751           TMap *configMap = (TMap*)cdbEntry->GetObject();
752           if (!configMap){
753                   Log(Form("No map found in Config entry in CDB, using default values: NoiseThr = %d", noiseThr));
754           }
755           else{
756                   TObjString *strNoiseThr = (TObjString*)configMap->GetValue("NoiseThr");
757                   if (strNoiseThr) {
758                           TString tmpstr = strNoiseThr->GetString();
759                           noiseThr = tmpstr.Atoi();
760                   }
761                   else {
762                           Log(Form("No NoiseThr value found in Map from ConfigNoise entry in CDB, using default value: NoiseThr = %i",noiseThr));
763                   }
764           }
765   }
766
767   delete cdbEntry;
768   cdbEntry = 0x0;
769
770   TH1::AddDirectory(0);
771
772   Bool_t resultNoiseRef=kFALSE;
773   Bool_t resultNoise=kFALSE;
774
775   static const Int_t kSize = AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors();
776   TH1F * htofNoise = new TH1F("hTOFnoise","histo with signals on TOF during noise", kSize,-0.5,kSize-0.5);
777   for (Int_t ibin =1;ibin<=kSize;ibin++){
778           htofNoise->SetBinContent(ibin,-1);
779   }
780   
781   // processing noise
782   
783   TFile * daqFile=0x0;
784   TH1F * h1=0x0;
785   
786   // useful counters
787   Int_t nNoise = 0;
788   Int_t nNoisyChannels = 0;
789   Int_t nNotNoisyChannels = 0;
790   Int_t nChannelsFromDA = 0;
791   Int_t nMatchingWindowNullNonZero = 0;
792   Int_t nMatchingWindowNullEqualZero = 0;
793
794   // retrieving Noise data 
795   TList* listNoise = GetFileSources(kDAQ, "NOISE");
796   if (listNoise !=0x0 && listNoise->GetEntries()!=0)
797           {
798                   AliInfo("The following sources produced files with the id NOISE");
799                   listNoise->Print();
800                   for (Int_t jj=0;jj<listNoise->GetEntries();jj++){
801                           Int_t nNoiseSource = 0;
802                           TObjString * str = dynamic_cast<TObjString*> (listNoise->At(jj));
803                           AliInfo(Form("found source %s", str->String().Data()));
804                           // file to be stored run per run
805                           TString fileNameNoise = GetFile(kDAQ, "NOISE", str->GetName());
806                           if (fileNameNoise.Length()>0){
807                                   // storing reference data
808                                   AliInfo(Form("Got the file %s, now we can process noise data.", fileNameNoise.Data()));
809                                   daqFile = new TFile(fileNameNoise.Data(),"READ");
810                                   h1 = (TH1F*) daqFile->Get("hTOFnoise");
811                                   for (Int_t ibin=0;ibin<kSize;ibin++){
812                                           if ((h1->GetBinContent(ibin+1))!=-1){
813                                                   nNoiseSource++;
814                                                   // checking the matching window for current channel
815                                                   if (fMatchingWindow[ibin] == 0){
816                                                           Log(Form("Matching window for channel %i null, but the channel was read by the LDC! skipping channel, BUT Please check!",ibin));
817                                                           if ((h1->GetBinContent(ibin+1))!=0) nMatchingWindowNullNonZero++;                                             
818                                                           if ((h1->GetBinContent(ibin+1))==0) nMatchingWindowNullEqualZero++;                                           
819                                                           continue;
820                                                   }
821                                                   if ((htofNoise->GetBinContent(ibin+1))==-1){
822                                                           htofNoise->SetBinContent(ibin+1,h1->GetBinContent(ibin+1)/(fMatchingWindow[ibin]*1.E-9));
823                                                           if ((h1->GetBinContent(ibin+1))!= 0) AliDebug(2,Form("Channel = %i, Matching window = %i, Content = %f", ibin, fMatchingWindow[ibin], htofNoise->GetBinContent(ibin+1)));
824                                                   }
825                                                   else {
826                                                           Log(Form("Something strange occurred during Noise run, channel %i already read by another LDC, please check!",ibin));
827                                                   }
828                                           }
829                                   }
830
831                                   Log(Form(" Number of channels processed during noise run from source %i = %i",jj, nNoiseSource));
832                                   daqFile->Close();
833                                   delete daqFile;
834                                   delete h1;
835                                   daqFile = 0x0;
836                                   h1 = 0x0;
837
838                           }
839                           else{
840                                   Log("The input data file from DAQ (noise) was not found, TOF exiting from Shuttle "); 
841                                   delete listNoise;
842                                   listNoise = 0x0;
843                                   delete htofNoise;
844                                   htofNoise = 0x0;
845                                   if (fStatus){
846                                           delete fStatus;
847                                           fStatus = 0;
848                                   }
849                                   if (fMatchingWindow){
850                                           delete [] fMatchingWindow;
851                                           fMatchingWindow = 0;
852                                   }
853                                   return 13;//return error code for failure in retrieving Ref Data 
854                           }
855                   }               
856           }
857                           
858   else{
859           Log("The input data file list from DAQ (noise) was not found, TOF exiting from Shuttle "); 
860           delete htofNoise;
861           htofNoise = 0x0;
862           if (fStatus){
863                   delete fStatus;
864                   fStatus = 0;
865           }
866           if (fMatchingWindow){
867                   delete [] fMatchingWindow;
868                   fMatchingWindow = 0;
869           }
870           return 13;//return error code for failure in retrieving Ref Data 
871   }     
872   
873   // elaborating infos to set NOISE status
874   for (Int_t ich =0;ich<fNChannels;ich++){
875           if (htofNoise->GetBinContent(ich+1)== -1) continue;
876
877           nChannelsFromDA++;
878
879           AliDebug(3,Form(" channel %i noise status before noise = %i, with global status = %i",ich,(Int_t)fStatus->GetNoiseStatus(ich),(Int_t)fStatus->GetStatus(ich)));
880           //AliDebug(2,Form( " channel %i status before noise = %i",ich,(Int_t)fStatus->GetNoiseStatus(ich)));
881           
882           /* check whether channel has been read out during current run.
883            * if the status is bad it means it has not been read out.
884            * in this case skip channel in order to leave its status 
885            * unchanged */
886
887           if ((fStatus->GetHWStatus(ich)) == AliTOFChannelOnlineStatusArray::kTOFHWBad)
888                   continue;
889           
890           nNoise++;
891           if (htofNoise->GetBinContent(ich+1) >= noiseThr){
892                   fStatus->SetNoiseStatus(ich,AliTOFChannelOnlineStatusArray::kTOFNoiseBad); // bad status for noise
893                   AliDebug(3,Form( " channel %i noise status after noise = %i, with global status = %i",ich,(Int_t)fStatus->GetNoiseStatus(ich),(Int_t)fStatus->GetStatus(ich)));
894                   nNoisyChannels++;
895           }
896           else {
897                   fStatus->SetNoiseStatus(ich,AliTOFChannelOnlineStatusArray::kTOFNoiseOk); // good status for noise
898                   AliDebug(3,Form(" channel %i noise status after noise = %i, with global status = %i",ich,(Int_t)fStatus->GetNoiseStatus(ich),(Int_t)fStatus->GetStatus(ich)));
899                   nNotNoisyChannels++;
900           }
901   }
902   
903   Log(Form(" Number of channels processed by DA during noise run, independetly from TOFFEE = %i",nChannelsFromDA));
904   Log(Form(" Number of channels processed during noise run (that were ON according to TOFFEE) = %i",nNoise));
905   Log(Form(" Number of noisy channels found during noise run = %i",nNoisyChannels));
906   Log(Form(" Number of not noisy channels found during noise run = %i",nNotNoisyChannels));
907   Log(Form(" Number of channels with matching window NULL (so skipped), but Non Zero content = %i",nMatchingWindowNullNonZero));
908   Log(Form(" Number of channels with matching window NULL (so skipped), and Zero content = %i",nMatchingWindowNullEqualZero));
909
910   delete listNoise;
911   
912   //daqFile=0;
913   
914   //storing in OCDB
915   
916   AliCDBMetaData metaData;
917   metaData.SetBeamPeriod(0);
918   metaData.SetResponsible("Chiara Zampolli");
919   metaData.SetComment("This preprocessor fills an AliTOFChannelOnlineStatusArray object after a Noise run.");
920   AliInfo("Storing Calibration Data from Noise Run");
921   resultNoise = Store("Calib","Status",fStatus, &metaData,0,kTRUE);
922   if(!resultNoise){
923     Log("Some problems occurred while storing online object resulting from Noise data processing");
924     delete htofNoise;
925     htofNoise = 0x0;
926     if (fStatus){
927             delete fStatus;
928             fStatus = 0;
929     }
930     if (fMatchingWindow){
931             delete [] fMatchingWindow;
932             fMatchingWindow = 0;
933     }
934     return 14;//return error code for problems in storing Noise data 
935   }
936
937   if(fStoreRefData){
938     
939     AliCDBMetaData metaDataHisto;
940     metaDataHisto.SetBeamPeriod(0);
941     metaDataHisto.SetResponsible("Chiara Zampolli");
942     char comment[200];
943     sprintf(comment,"This preprocessor stores the Ref data from a noise run. ");
944     metaDataHisto.SetComment(comment);
945     AliInfo("Storing Reference Data");
946     resultNoiseRef = StoreReferenceData("Calib","NoiseData",htofNoise, &metaDataHisto);
947     if (!resultNoiseRef){
948       Log("some problems occurred::No Reference Data for noise stored");
949       delete htofNoise;
950       htofNoise = 0x0;
951       if (fStatus){
952               delete fStatus;
953               fStatus = 0;
954       }
955       if (fMatchingWindow){
956               delete [] fMatchingWindow;
957               fMatchingWindow = 0;
958       }
959       return 12;//return error code for failure in storing Ref Data 
960     }
961   }
962
963   delete htofNoise;
964   htofNoise = 0x0;
965
966   if (fStatus){
967     delete fStatus;
968     fStatus = 0;
969   }
970
971   if (fMatchingWindow){
972           delete [] fMatchingWindow;
973           fMatchingWindow = 0;
974   }
975
976   return 0;
977 }
978 //_____________________________________________________________________________
979
980 UInt_t AliTOFPreprocessor::ProcessFEEData()
981 {
982   // Processing Pulser Run data for TOF channel status
983   // dummy for the time being
984
985   Log("Processing FEE");
986
987   Bool_t updateOCDB = kFALSE;
988   AliTOFFEEReader feeReader;
989
990   TH1C hCurrentFEE("hCurrentFEE","histo with current FEE channel status", fNChannels, 0, fNChannels);
991   
992   /* load current TOF FEE config from DCS FXS, parse, 
993    * fill current FEE histogram and set FEE status */
994   
995   const char * nameFile = GetFile(kDCS,"TofFeeLightMap",""); 
996   AliInfo(Form("nameFile = %s",nameFile));
997   if (nameFile == NULL) {
998           return 15;
999   } 
1000   feeReader.LoadFEElightConfig(nameFile);
1001   Int_t parseFee = feeReader.ParseFEElightConfig();
1002   AliDebug(2,Form("%i enabled channels found in FEElight configuration",parseFee));
1003   /* load stored TOF FEE from OCDB and compare it with current FEE.
1004    * if stored FEE is different from current FEE set update flag.
1005    * if there is no stored FEE in OCDB set update flag */
1006   
1007   fMatchingWindow = new Int_t[fNChannels];
1008   fLatencyWindow = new Int_t[fNChannels];
1009   
1010   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","Status");
1011   if (!cdbEntry) {
1012           /* no CDB entry found. set update flag */
1013           Log("     ************ WARNING ************");
1014           Log("No CDB Status entry found, creating a new one!");
1015           Log("     *********************************");
1016           fStatus = new AliTOFChannelOnlineStatusArray(fNChannels);
1017           updateOCDB = kTRUE;
1018   }
1019   else {
1020           if (cdbEntry) cdbEntry->SetOwner(kFALSE);
1021           /* CDB entry OK. loop over channels */
1022           fStatus = (AliTOFChannelOnlineStatusArray*) cdbEntry->GetObject();
1023           delete cdbEntry;
1024           cdbEntry = 0x0;
1025           /* cehck whether status object has latency window data */
1026           if (!fStatus->HasLatencyWindow()) {
1027             /* create new status object and update OCDB */
1028             Log("     ************ WARNING ************");
1029             Log("CDB Status entry found but has no latency window data, creating a new one!");
1030             Log("     *********************************");
1031             delete fStatus;
1032             fStatus = new AliTOFChannelOnlineStatusArray(fNChannels);
1033             updateOCDB = kTRUE;
1034           }
1035   }
1036   for (Int_t iChannel = 0; iChannel < fNChannels; iChannel++){
1037           //AliDebug(2,Form("********** channel %i",iChannel));
1038           /* compare current FEE channel status with stored one 
1039            * if different set update flag and break loop */
1040           //AliDebug(2,Form( " channel %i status before FEE = %i",iChannel,(Int_t)fStatus->GetHWStatus(iChannel)));
1041           fMatchingWindow[iChannel] = feeReader.GetMatchingWindow(iChannel);
1042           fLatencyWindow[iChannel] = feeReader.GetLatencyWindow(iChannel);
1043           if (feeReader.IsChannelEnabled(iChannel)) {
1044                   hCurrentFEE.SetBinContent(iChannel + 1, 1);
1045                   if (fStatus->GetHWStatus(iChannel)!=AliTOFChannelOnlineStatusArray::kTOFHWOk){
1046                           updateOCDB = kTRUE;
1047                           fStatus->SetHWStatus(iChannel,AliTOFChannelOnlineStatusArray::kTOFHWOk);
1048                           AliDebug(3,Form( " changed into enabled: channel %i status after FEE = %i",iChannel,(Int_t)fStatus->GetHWStatus(iChannel)));
1049                   }
1050                   if (fStatus->GetLatencyWindow(iChannel)!=fLatencyWindow[iChannel]){
1051                           updateOCDB = kTRUE;
1052                           fStatus->SetLatencyWindow(iChannel,fLatencyWindow[iChannel]);
1053                           AliDebug(3,Form( " changed latency window: channel %i latency window after FEE = %i",iChannel,fStatus->GetLatencyWindow(iChannel)));
1054                   }
1055           }
1056           else {
1057                   if (fStatus->GetHWStatus(iChannel)!=AliTOFChannelOnlineStatusArray::kTOFHWBad){
1058                           updateOCDB = kTRUE;
1059                           fStatus->SetHWStatus(iChannel,AliTOFChannelOnlineStatusArray::kTOFHWBad);
1060                           AliDebug(3,Form( " changed into disabled: channel %i status after FEE = %i",iChannel,(Int_t)fStatus->GetHWStatus(iChannel)));
1061                   }
1062           }
1063   }
1064
1065
1066   /* check whether we don't have to store reference data.
1067    * in this case we return without errors. */
1068   if (fStoreRefData) {
1069           /* store reference data */
1070           AliCDBMetaData metaDataHisto;
1071           metaDataHisto.SetBeamPeriod(0);
1072           metaDataHisto.SetResponsible("Roberto Preghenella");
1073           metaDataHisto.SetComment("This preprocessor stores the FEE Ref data of the current run.");
1074           AliInfo("Storing FEE reference data");
1075           /* store FEE reference data */
1076           if (!StoreReferenceData("Calib", "FEEData", &hCurrentFEE, &metaDataHisto)) {
1077                   /* failed */
1078                   Log("problems while storing FEE reference data");
1079                   if (fStatus){
1080                           delete fStatus;
1081                           fStatus = 0;
1082                   }
1083                   return 18; /* error return code for problems while storing FEE reference data */
1084           }
1085   }
1086
1087   /* check whether we don't need to update OCDB.
1088    * in this case we can return without errors and
1089    * the current FEE is stored in the fStatus object. */
1090   if (!updateOCDB) {
1091     AliInfo("TOF FEE config has not changed. Do not overwrite stored file.");
1092     return 0; /* return ok */
1093   }
1094
1095   TString runType = GetRunType();
1096   if (runType != "PHYSICS") {
1097           AliInfo(Form("Run Type = %s, waiting to store status map",GetRunType()));
1098     return 0; /* return ok */
1099   }
1100
1101   /* update the OCDB with the current FEE since even 
1102    * a little difference has been detected. */
1103
1104   AliCDBMetaData metaData;
1105   metaData.SetBeamPeriod(0);
1106   metaData.SetResponsible("Roberto Preghenella");
1107   metaData.SetComment("This preprocessor fills an AliTOFChannelOnlineStatusArray object from FEE data.");
1108   AliInfo("Storing Status data from current run after FEE parsing");
1109   /* store FEE data */
1110   if (!Store("Calib", "Status", fStatus, &metaData, 0, kTRUE)) {
1111     /* failed */
1112     Log("problems while storing FEE data object");
1113     if (fStatus){
1114             delete fStatus;
1115             fStatus = 0;
1116     }
1117     return 17; /* return error code for problems  while storing FEE data */
1118   }
1119
1120   /* everything fine. return */
1121
1122   if (fStatus){
1123     delete fStatus;
1124     fStatus = 0;
1125   }
1126
1127   return 0;
1128
1129 }
1130
1131 //_____________________________________________________________________________
1132
1133 UInt_t AliTOFPreprocessor::Process(TMap* dcsAliasMap)
1134 {
1135   //
1136   //
1137   //
1138
1139   TString runType = GetRunType();
1140   Log(Form("RunType %s",runType.Data()));
1141   
1142   // processing 
1143
1144   /* always process FEE data */
1145   Int_t iresultFEE = ProcessFEEData();
1146   if (iresultFEE != 0)
1147     return iresultFEE;
1148
1149   if (runType == "PULSER") {
1150     Int_t iresultPulser = ProcessPulserData();
1151     return iresultPulser; 
1152   }
1153
1154   if (runType == "NOISE") { // for the time being associating noise runs with pedestal runs; proper run type to be defined 
1155     Int_t iresultNoise = ProcessNoiseData();
1156     return iresultNoise; 
1157   }
1158   
1159   if (runType == "PHYSICS") {
1160     Int_t iresultDAQ = ProcessOnlineDelays();
1161     if (iresultDAQ != 0) {
1162       return iresultDAQ;
1163     }
1164     else {
1165       Int_t iresultDCS = ProcessDCSDataPoints(dcsAliasMap);
1166       return iresultDCS;
1167     }
1168   }
1169
1170   // storing
1171   return 0;
1172 }
1173
1174
1175 //_____________________________________________________________________________
1176
1177 void
1178 AliTOFPreprocessor::FillWithCosmicCalibration(AliTOFChannelOnlineArray *cal)
1179 {
1180   /*
1181    * fill with cosmic calibration 
1182    */
1183
1184   Log(" Using cosmic-ray calibration.");
1185   
1186   AliTOFcalibHisto calibHisto;
1187   calibHisto.SetFullCorrectionFlag(AliTOFcalibHisto::kTimeSlewingCorr, kFALSE);
1188   Log(Form(" loading calibration histograms from %s", calibHisto.GetCalibHistoFileName()));
1189   Log(Form(" loading calibration parameters from %s", calibHisto.GetCalibParFileName()));
1190   calibHisto.LoadCalibPar();
1191   
1192   /* loop over channel index */
1193   for (Int_t iIndex = 0; iIndex < fNChannels; iIndex++) {
1194     cal->SetDelay(iIndex, calibHisto.GetFullCorrection(iIndex));
1195   }
1196   
1197 }
1198
1199 //_____________________________________________________________________________
1200
1201 void
1202 AliTOFPreprocessor::FillWithCableLengthMap(AliTOFChannelOnlineArray *cal)
1203 {
1204   /*
1205    * fill with cosmic calibration 
1206    */
1207   
1208   Log(" Using cable-length map.");
1209   AliTOFRawStream tofrs;
1210   Int_t det[5], dummy, index;
1211   Float_t cableTimeShift;
1212   
1213   /* temporarly disable warnings */
1214   AliLog::EType_t logLevel = (AliLog::EType_t)AliLog::GetGlobalLogLevel();
1215   AliLog::SetGlobalLogLevel(AliLog::kError);
1216   
1217   /* loop over EO indeces */
1218   for (Int_t iddl = 0; iddl < 72; iddl++)
1219     for (Int_t islot = 3; islot <= 12; islot++)
1220       for (Int_t ichain = 0; ichain < 2; ichain++)
1221         for (Int_t itdc = 0; itdc < 15; itdc++)
1222           for (Int_t ichannel = 0; ichannel < 8; ichannel++) {
1223             
1224             /* get DO index */
1225             tofrs.EquipmentId2VolumeId(iddl, islot, ichain, itdc, ichannel, det);
1226             
1227             /* swap det[3] and det[4] indeces (needed to obtain correct channel index) */
1228             dummy = det[3];
1229             det[3] = det[4];
1230             det[4] = dummy;
1231             
1232             /* check DO index */
1233             if (det[0] < 0 || det[0] > 17 ||
1234                 det[1] < 0 || det[1] > 4 ||
1235                 det[2] < 0 || det[2] > 18 ||
1236                 det[3] < 0 || det[3] > 1 ||
1237                 det[4] < 0 || det[4] > 47)
1238               continue;
1239             
1240             /* get channel index */
1241             index = AliTOFGeometry::GetIndex(det);
1242             
1243             /* get cable time shift */
1244             cableTimeShift = AliTOFCableLengthMap::GetCableTimeShift(iddl, islot, ichain, itdc);
1245             
1246             /* set delay */
1247             if (index<fNChannels) {
1248               cal->SetDelay(index,cableTimeShift);  // delay in ns
1249               AliDebug(2,Form("Setting delay %f (ns) for channel %i",cableTimeShift,index));
1250             }
1251             
1252           } /* loop over EO indeces */
1253   
1254   /* re-enable warnings */
1255   AliLog::SetGlobalLogLevel(logLevel);
1256   
1257 }
1258
1259