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