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