]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFPreprocessor.cxx
Suppressing unnecessary warning message, because appropriate error messages are being...
[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 <TH1.h>
24 //#include <TH1F.h>
25 //#include <TH1S.h>
26 #include <TH2S.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 "AliTOFChannelOnline.h"
36 #include "AliTOFChannelOnlineStatus.h"
37 #include "AliTOFDataDCS.h"
38 #include "AliTOFGeometry.h"
39 #include "AliTOFPreprocessor.h"
40 #include "AliTOFFEEReader.h"
41
42 //class TF1;
43 //class AliDCSValue;
44 //class AliTOFGeometry;
45
46 // TOF preprocessor class.
47 // It takes data from DCS and passes them to the class AliTOFDataDCS, which
48 // processes them. The result is then written to the CDB.
49 // analogously, it takes data form DAQ (both at Run level and inclusive - 
50 // of all the runs - level, processes them, and stores both Reference Data
51 // and Online Calibration files in the CDB. 
52 // Processing of Pulser/Noise Run data and of TOF FEE DCS map
53
54 // return codes:
55 // return=0 : all ok
56 // return=1 : no DCS input data Map
57 // return=2 : no DCS input data processing
58 // return=3 : no DCS processed data was stored in Ref Data
59 // return=4 : no DAQ input for Ref Data
60 // return=5 : failed to store DAQ Ref Data
61 // return=6 : failed to retrieve DAQ data for calibration 
62 // return=7 : problems in processing histos in the input DAQ file 
63 // return=8 : failed to store Online Delays
64 // return=9 : failed to store Reference Data for Pulser
65 // return=10: failed to retrieve Pulser data 
66 // return=11: failed to store Pulser map in OCDB
67 // return=12: failed to store Reference Data for Noise
68 // return=13: failed to retrieve Noise data 
69 // return=14: failed to store Noise map in OCDB
70 // return=15: failed to retrieve FEE data from FXS
71 // return=16: failed to retrieve FEE data from OCDB
72 // return=17: failed to store FEE data in OCDB
73 // return=18: failed to store FEE reference data in OCDB
74
75 ClassImp(AliTOFPreprocessor)
76
77 const Int_t    AliTOFPreprocessor::fgkBinRangeAve = 13;    // number of bins where to calculate the mean 
78 const Double_t AliTOFPreprocessor::fgkIntegralThr = 100;   // min number of entries to perform computation of delay per channel 
79 const Double_t AliTOFPreprocessor::fgkThrPar      = 0.013; // parameter used to trigger the calculation of the delay
80
81 //_____________________________________________________________________________
82
83 AliTOFPreprocessor::AliTOFPreprocessor(AliShuttleInterface* shuttle) :
84   AliPreprocessor("TOF", shuttle),
85   fData(0),
86   fh2(0),
87   fCal(0),
88   fCalStatus(0),
89   fFEEStatus(0),
90   fNChannels(0),
91   fStoreRefData(kTRUE),
92   fFDRFlag(kTRUE)
93 {
94   // constructor
95   AddRunType("PHYSICS");
96   AddRunType("PULSER");
97   AddRunType("NOISE");
98
99 }
100
101 //_____________________________________________________________________________
102
103 AliTOFPreprocessor::~AliTOFPreprocessor()
104 {
105   // destructor
106   if (fData){
107     delete fData;
108     fData = 0;
109   }
110   if (fh2){
111     delete fh2;
112     fh2 = 0;
113   }
114   if (fCal){
115     //    fCal->Clear();
116     delete fCal;
117     fCal = 0;
118   }
119   if (fCalStatus){
120     delete fCalStatus;
121     fCalStatus = 0;
122   }
123   if (fFEEStatus){
124     delete fFEEStatus;
125     fFEEStatus = 0;
126   }
127 }
128
129 //______________________________________________________________________________
130 void AliTOFPreprocessor::Initialize(Int_t run, UInt_t startTime,
131         UInt_t endTime)
132 {
133   // Creates AliTOFDataDCS object
134
135   AliPreprocessor::Initialize(run, startTime, endTime);
136
137         AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
138                 TTimeStamp(startTime).AsString(),
139                 TTimeStamp(endTime).AsString()));
140
141         fData = new AliTOFDataDCS(fRun, fStartTime, fEndTime);
142         fData->SetFDRFlag(fFDRFlag);
143         fh2 = 0x0;
144         fNChannels = AliTOFGeometry::NSectors()*(2*(AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB())+AliTOFGeometry::NStripA())*AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX();
145         fCal = new TObjArray(fNChannels);
146         fCal->SetOwner();
147         for (Int_t ich = 0; ich<fNChannels; ich ++){
148           AliTOFChannelOnline * calChOnline = new AliTOFChannelOnline();
149           fCal->AddAt(calChOnline,ich);
150         }
151         fCalStatus = new TObjArray(fNChannels);
152         fCalStatus->SetOwner();
153         for (Int_t ich = 0; ich<fNChannels; ich ++){
154           AliTOFChannelOnlineStatus * calChOnlineStatus = new AliTOFChannelOnlineStatus();
155           fCalStatus->AddAt(calChOnlineStatus,ich);
156         }
157         fFEEStatus = new TObjArray(fNChannels);
158         fFEEStatus->SetOwner();
159         for (Int_t ich = 0; ich<fNChannels; ich ++){
160           AliTOFChannelOnlineStatus * calChOnlineStatus = new AliTOFChannelOnlineStatus();
161           fFEEStatus->AddAt(calChOnlineStatus,ich);
162         }
163 }
164 //_____________________________________________________________________________
165 Bool_t AliTOFPreprocessor::ProcessDCS(){
166
167   // check whether DCS should be processed or not...
168
169   TString runType = GetRunType();
170   Log(Form("RunType %s",runType.Data()));
171
172   if (runType != "PHYSICS"){
173     return kFALSE;
174   }
175
176   return kTRUE;
177 }
178 //_____________________________________________________________________________
179
180 UInt_t AliTOFPreprocessor::ProcessDCSDataPoints(TMap* dcsAliasMap)
181 {
182   // Fills data into a AliTOFDataDCS object
183
184
185   Log("Processing DCS DP");
186   TH1::AddDirectory(0);
187
188   Bool_t resultDCSMap=kFALSE;
189   Bool_t resultDCSStore=kFALSE;
190
191   // processing DCS
192
193   if (!dcsAliasMap){
194     Log("No DCS map found: TOF exiting from Shuttle");
195     return 1;// return error Code for DCS input data not found 
196   }
197   else {
198   // The processing of the DCS input data is forwarded to AliTOFDataDCS
199     resultDCSMap=fData->ProcessData(*dcsAliasMap);
200     if(!resultDCSMap){
201       Log("Some problems occurred while processing DCS data, TOF exiting from Shuttle");
202       return 2;// return error Code for processed DCS data not stored 
203     }
204     else{
205       AliCDBMetaData metaDataDCS;
206       metaDataDCS.SetBeamPeriod(0);
207       metaDataDCS.SetResponsible("Chiara Zampolli");
208       metaDataDCS.SetComment("This preprocessor fills an AliTOFDataDCS object.");
209       AliInfo("Storing DCS Data");
210       resultDCSStore = StoreReferenceData("Calib","DCSData",fData, &metaDataDCS);
211       if (!resultDCSStore){
212         Log("Some problems occurred while storing DCS data results in Reference Data, TOF exiting from Shuttle");
213         return 3;// return error Code for processed DCS data not stored 
214                  // in reference data
215         
216       }
217     }
218   }
219   return 0;
220 }
221 //_____________________________________________________________________________
222
223 UInt_t AliTOFPreprocessor::ProcessOnlineDelays()
224 {
225   // Processing data from DAQ for online calibration 
226
227   Log("Processing DAQ delays");
228
229   TH1::AddDirectory(0);
230
231   Bool_t resultDAQRef=kFALSE;
232   Bool_t resultTOFPP=kFALSE;
233   // processing DAQ
234   
235   TFile * daqFile=0x0;
236   
237   if(fStoreRefData){
238     //retrieving data at Run level
239     TList* list = GetFileSources(kDAQ, "RUNLevel");
240     if (list !=0x0 && list->GetEntries()!=0)
241       {
242         AliInfo("The following sources produced files with the id RUNLevel");
243         list->Print();
244         for (Int_t jj=0;jj<list->GetEntries();jj++){
245           TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
246           AliInfo(Form("found source %s", str->String().Data()));
247           // file to be stored run per run
248           TString fileNameRun = GetFile(kDAQ, "RUNLevel", str->GetName());
249           if (fileNameRun.Length()>0){
250             AliInfo(Form("Got the file %s, now we can store the Reference Data for the current Run.", fileNameRun.Data()));
251             daqFile = new TFile(fileNameRun.Data(),"READ");
252             fh2 = (TH2S*) daqFile->Get("htof");
253             AliCDBMetaData metaDataHisto;
254             metaDataHisto.SetBeamPeriod(0);
255             metaDataHisto.SetResponsible("Chiara Zampolli");
256             metaDataHisto.SetComment("This preprocessor stores the array of histos object as Reference Data.");
257             AliInfo("Storing Reference Data");
258             resultDAQRef = StoreReferenceData("Calib","DAQData",fh2, &metaDataHisto);
259             if (!resultDAQRef){
260               Log("some problems occurred::No Reference Data stored, TOF exiting from Shuttle");
261               return 5;//return error code for failure in storing Ref Data 
262             }
263             daqFile->Close();
264             delete daqFile;
265           }
266           
267           else{
268             Log("The input data file from DAQ (run-level) was not found, TOF exiting from Shuttle "); 
269             return 4;//return error code for failure in retrieving Ref Data 
270           }
271         }
272         delete list;
273       }
274     else{
275       Log("The input data file list from DAQ (run-level) was not found, TOF exiting from Shuttle "); 
276       return 4;//return error code for failure in retrieving Ref Data 
277     }   
278   }
279
280
281 //Total files, with cumulative histos
282   
283   TList* listTot = GetFileSources(kDAQ, "DELAYS");
284   if (listTot !=0x0 && listTot->GetEntries()!=0)
285     {
286       AliInfo("The following sources produced files with the id DELAYS");
287       listTot->Print();
288       for (Int_t jj=0;jj<listTot->GetEntries();jj++){
289         TObjString * str = dynamic_cast<TObjString*> (listTot->At(jj));
290         AliInfo(Form("found source %s", str->String().Data()));
291
292         // file with summed histos, to extract calib params
293         TString fileName = GetFile(kDAQ, "DELAYS", str->GetName());
294         if (fileName.Length()>0){
295           AliInfo(Form("Got the file %s, now we can extract some values.", fileName.Data()));
296
297           daqFile = new TFile(fileName.Data(),"READ");
298           if (fh2) delete fh2;
299           fh2 = (TH2S*) daqFile->Get("htoftot");
300           if (!fh2){
301             Log("some problems occurred:: No histo retrieved, TOF exiting from Shuttle");
302             delete daqFile;
303             return 7; //return error code for histograms not existing/junky
304           }
305           else {
306             static const Int_t kSize=fh2->GetNbinsX();
307             static const Int_t kNBins=fh2->GetNbinsY();
308             static const Double_t kXBinmin=fh2->GetYaxis()->GetBinLowEdge(1);
309             if (kSize != fNChannels){
310               Log(" number of bins along x different from number of pads, found only a subset of the histograms, TOF exiting from Shuttle");
311               delete daqFile;
312               return 7; //return error code for histograms not existing/junky
313             }
314             Int_t nNotStatistics = 0; // number of channel with not enough statistics
315             for (Int_t ich=0;ich<kSize;ich++){
316               /* check whether channel has been read out during current run.
317                   * if the status is bad it means it has not been read out.
318                   * in this case skip channel in order to not affect the mean */ 
319               if (((AliTOFChannelOnlineStatus *)fFEEStatus->At(ich))->GetStatus() == AliTOFChannelOnlineStatus::kTOFHWBad){
320                 AliDebug(2,Form(" Channel %i found bad according to FEEmap, skipping from delay computing",ich));
321                 continue;
322               }
323               AliDebug(2,Form(" Channel %i found ok according to FEEmap, starting delay computing",ich));
324               TH1S *h1 = new TH1S("h1","h1",kNBins,kXBinmin-0.5,kNBins*1.+kXBinmin-0.5);
325               for (Int_t ibin=0;ibin<kNBins;ibin++){
326                 h1->SetBinContent(ibin+1,fh2->GetBinContent(ich+1,ibin+1));
327               }
328               if(h1->Integral()<fgkIntegralThr) {
329                 nNotStatistics++;
330                 if (!fFDRFlag)  Log(Form(" Not enough statistics for bin %i, skipping this channel",ich));  // printing message only if not in FDR runs
331                 delete h1;
332                 h1=0x0;
333                 continue;
334               }
335               if (!fFDRFlag) {  // not computing delays if in FDR runs
336                 Bool_t found=kFALSE; 
337                 Float_t minContent=h1->Integral()*fgkThrPar; 
338                 Int_t nbinsX = h1->GetNbinsX();
339                 Int_t startBin=1;
340                 for (Int_t j=1; j<=nbinsX; j++){
341                   if ((
342                        h1->GetBinContent(j) +     
343                        h1->GetBinContent(j+1)+
344                        h1->GetBinContent(j+2)+ 
345                        h1->GetBinContent(j+3))>minContent){
346                     found=kTRUE;
347                     startBin=j;
348                     break;
349                   }
350                 }
351                 if(!found) AliInfo(Form("WARNING!!! no start of fit found for histo # %i",ich));
352                 // Now calculate the mean over the interval. 
353                 Double_t mean = 0;
354                 Double_t sumw2 = 0;
355                 Double_t nent = 0;
356                 for(Int_t k=0;k<fgkBinRangeAve;k++){
357                   mean=mean+h1->GetBinCenter(startBin+k)*h1->GetBinContent(startBin+k);                 
358                   nent=nent+h1->GetBinContent(startBin+k);                 
359                   sumw2=sumw2+(h1->GetBinCenter(startBin+k))*(h1->GetBinCenter(startBin+k))*(h1->GetBinContent(startBin+k));
360                 }
361                 mean= mean/nent; //<x>
362                 sumw2=sumw2/nent; //<x^2>
363                 Double_t rmsmean= 0;
364                 rmsmean = TMath::Sqrt((sumw2-mean*mean)/nent);
365                 if (ich<fNChannels) {
366                   AliTOFChannelOnline * ch = (AliTOFChannelOnline *)fCal->At(ich);
367                   ch->SetDelay((Double_t)mean*AliTOFGeometry::TdcBinWidth()*1.E-3);  // delay in ns
368                   // ch->SetStatus(1);  // calibrated channel, removed for the time being from AliTOFChannelOnline
369                 }
370               }
371             delete h1;
372             h1=0x0;
373             }
374             if (nNotStatistics!=0) Log(Form("Too little statistics for %d channels!",nNotStatistics)); 
375           }
376           daqFile->Close();
377           delete daqFile;
378         }
379         else{
380           Log("The Cumulative data file from DAQ does not exist, TOF exiting from Shuttle"); 
381           return 6;//return error code for problems in retrieving DAQ data 
382         }
383       }
384       delete listTot;
385     }
386   else{
387     Log("Problem: no list for Cumulative data file from DAQ was found, TOF exiting from Shuttle");
388     return 6; //return error code for problems in retrieving DAQ data 
389   }
390
391   daqFile=0;
392   AliCDBMetaData metaData;
393   metaData.SetBeamPeriod(0);
394   metaData.SetResponsible("Chiara Zampolli");
395   metaData.SetComment("This preprocessor fills a TObjArray object.");
396   AliInfo("Storing Calibration Data");
397   resultTOFPP = Store("Calib","ParOnline",fCal, &metaData,0,kTRUE);
398   if(!resultTOFPP){
399     Log("Some problems occurred while storing online object resulting from DAQ data processing");
400     return 8;//return error code for problems in storing DAQ data 
401   }
402
403   return 0;
404 }
405 //_____________________________________________________________________________
406
407 UInt_t AliTOFPreprocessor::ProcessPulserData()
408 {
409   // Processing Pulser Run data for TOF channel status
410
411   Log("Processing Pulser");
412
413   TH1::AddDirectory(0);
414
415   Bool_t resultPulserRef=kFALSE;
416   Bool_t resultPulser=kFALSE;
417
418   static const Int_t kSize = AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors();
419   TH1S * htofPulser = new TH1S("hTOFpulser","histo with signals on TOF during pulser", kSize,-0.5,kSize-0.5);
420   for (Int_t ibin =1;ibin<=kSize;ibin++){
421     htofPulser->SetBinContent(ibin,-1);
422   }
423
424   // retrieving last stored pulser object, and copying
425
426   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","Pulser");
427   if (cdbEntry!=0x0){
428     TObjArray *currentCalStatus = (TObjArray*)cdbEntry->GetObject();
429     for (Int_t ich = 0; ich<fNChannels; ich ++){
430       AliTOFChannelOnlineStatus * calChOnlineSt = (AliTOFChannelOnlineStatus*)fCalStatus->At(ich);
431       calChOnlineSt->SetStatus(((AliTOFChannelOnlineStatus*)currentCalStatus->At(ich))->GetStatus());
432     }
433   }
434
435   // processing pulser
436   
437   TFile * daqFile=0x0;
438   TH1S *h1=0x0;
439   
440   //retrieving Pulser data 
441   TList* listPulser = GetFileSources(kDAQ, "PULSER");
442   if (listPulser !=0x0 && listPulser->GetEntries()!=0)
443     {
444       AliInfo("The following sources produced files with the id PULSER");
445       listPulser->Print();
446       for (Int_t jj=0;jj<listPulser->GetEntries();jj++){
447         TObjString * str = dynamic_cast<TObjString*> (listPulser->At(jj));
448         AliInfo(Form("found source %s", str->String().Data()));
449         // file to be stored run per run
450         TString fileNamePulser = GetFile(kDAQ, "PULSER", str->GetName());
451         if (fileNamePulser.Length()>0){
452           // storing refernce data
453           AliInfo(Form("Got the file %s, now we can process pulser data.", fileNamePulser.Data()));
454           daqFile = new TFile(fileNamePulser.Data(),"READ");
455           h1 = (TH1S*) daqFile->Get("hTOFpulser");
456           for (Int_t ibin=0;ibin<kSize;ibin++){
457             if ((h1->GetBinContent(ibin+1))!=-1){
458               if ((htofPulser->GetBinContent(ibin+1))==-1){
459                 htofPulser->SetBinContent(ibin+1,h1->GetBinContent(ibin+1));
460               }
461               else {
462                 Log(Form("Something strange occurred during Pulser run, channel %i already read by another LDC, please check!",ibin));
463               }
464             }
465           }
466           
467           // elaborating infos
468           Double_t mean =0;
469           Int_t nread=0;
470           Int_t nreadNotEmpty=0;
471           for (Int_t ientry=1;ientry<=h1->GetNbinsX();ientry++){
472
473             /* check whether channel has been read out during current run.
474              * if the status is bad it means it has not been read out.
475              * in this case skip channel in order to not affect the mean */ 
476             if (((AliTOFChannelOnlineStatus *)fFEEStatus->At(ientry-1))->GetStatus() == AliTOFChannelOnlineStatus::kTOFHWBad)
477                     continue;
478
479             if (h1->GetBinContent(ientry)==-1) continue;
480             else {
481               if (h1->GetBinContent(ientry)>0) {
482                 nreadNotEmpty++;
483                 AliDebug(1,Form(" channel %i is ok with entry = %f; so far %i channels added ",ientry-1,h1->GetBinContent(ientry),nreadNotEmpty));
484               }
485               mean+=h1->GetBinContent(ientry);
486               nread++;
487             }
488           }
489           if (nread!=0) {
490                   mean/=nread;
491                   AliDebug(1,Form(" nread =  %i , mean = %f",nread,mean));
492                   for (Int_t ich =0;ich<fNChannels;ich++){
493                           AliTOFChannelOnlineStatus * chSt = (AliTOFChannelOnlineStatus *)fCalStatus->At(ich);
494                           if (h1->GetBinContent(ich+1)==-1) continue;
495                           AliDebug(1,Form(" channel %i ",ich));
496                           AliDebug(1,Form(" channel status before pulser = %i",(Int_t)chSt->GetStatus()));
497                           
498                           /* check whether channel has been read out during current run.
499                            * if the status is bad it means it has not been read out.
500                            * in this case skip channel in order to leave its status 
501                            * unchanged */
502                           if (((AliTOFChannelOnlineStatus *)fFEEStatus->At(ich))->GetStatus() == AliTOFChannelOnlineStatus::kTOFHWBad)
503                           continue;
504                           
505                           if (h1->GetBinContent(ich+1)<0.05*mean){
506                                   chSt->SetStatus(chSt->GetStatus()|AliTOFChannelOnlineStatus::kTOFPulserBad);  // bad status for pulser
507                                   AliDebug(1,Form(" channel status after pulser = %i",(Int_t)chSt->GetStatus()));
508                           }
509                           else {
510                                   chSt->SetStatus(chSt->GetStatus()|AliTOFChannelOnlineStatus::kTOFPulserOk);  // bad status for pulser
511                                   AliDebug(1,Form(" channel status after pulser = %i",(Int_t)chSt->GetStatus()));
512                           }
513                   }
514           }
515           else {
516                   Log("No channels read!! No action taken, keeping old status");
517           }
518
519           daqFile->Close();
520           delete daqFile;
521           delete h1;
522         }
523         
524         else{
525           Log("The input data file from DAQ (pulser) was not found, TOF exiting from Shuttle "); 
526           return 10;//return error code for failure in retrieving Ref Data 
527         }
528         
529       }
530       delete listPulser;
531     }
532   
533   else{
534     Log("The input data file list from DAQ (pulser) was not found, TOF exiting from Shuttle "); 
535     return 10;//return error code for failure in retrieving Ref Data 
536   }     
537
538   //storing in OCDB  
539
540   AliCDBMetaData metaData;
541   metaData.SetBeamPeriod(0);
542   metaData.SetResponsible("Chiara Zampolli");
543   metaData.SetComment("This preprocessor fills a TObjArray object for Pulser data.");
544   AliInfo("Storing Calibration Data from Pulser Run");
545   resultPulser = Store("Calib","Pulser",fCalStatus, &metaData,0,kTRUE);
546   if(!resultPulser){
547     Log("Some problems occurred while storing online object resulting from Pulser data processing");
548     return 11;//return error code for problems in storing Pulser data 
549   }
550
551   if(fStoreRefData){
552     
553     AliCDBMetaData metaDataHisto;
554     metaDataHisto.SetBeamPeriod(0);
555     metaDataHisto.SetResponsible("Chiara Zampolli");
556     char comment[200];
557     sprintf(comment,"This preprocessor stores the result of the pulser run");
558     metaDataHisto.SetComment(comment);
559     AliInfo("Storing Reference Data");
560     resultPulserRef = StoreReferenceData("Calib","PulserData",htofPulser, &metaDataHisto);
561     if (!resultPulserRef){
562       Log("some problems occurred::No Reference Data for pulser stored, TOF exiting from Shuttle");
563       return 9;//return error code for failure in storing Ref Data 
564     }
565   }
566   
567   daqFile=0;
568
569   return 0;
570 }
571 //_____________________________________________________________________________
572
573 UInt_t AliTOFPreprocessor::ProcessNoiseData()
574 {
575
576   // Processing Noise Run data for TOF channel status
577
578   Log("Processing Noise");
579
580   TH1::AddDirectory(0);
581
582   Bool_t resultNoiseRef=kFALSE;
583   Bool_t resultNoise=kFALSE;
584
585   static const Int_t kSize = AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors();
586   TH1F * htofNoise = new TH1F("hTOFnoise","histo with signals on TOF during pulser", kSize,-0.5,kSize-0.5);
587   for (Int_t ibin =1;ibin<=kSize;ibin++){
588     htofNoise->SetBinContent(ibin,-1);
589   }
590
591   // retrieving last stored noise object, and copying
592
593   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","Noise");
594   if (cdbEntry!=0x0){
595     TObjArray *currentCalStatus = (TObjArray*)cdbEntry->GetObject();
596     for (Int_t ich = 0; ich<fNChannels; ich ++){
597       AliTOFChannelOnlineStatus * calChOnlineSt = (AliTOFChannelOnlineStatus*)fCalStatus->At(ich);
598       calChOnlineSt->SetStatus(((AliTOFChannelOnlineStatus*)currentCalStatus->At(ich))->GetStatus());
599     }
600   }
601
602   // processing noise
603   
604   TFile * daqFile=0x0;
605   TH1F * h1=0x0;
606   
607   //retrieving Noise data 
608   TList* listNoise = GetFileSources(kDAQ, "NOISE");
609   if (listNoise !=0x0 && listNoise->GetEntries()!=0)
610     {
611       AliInfo("The following sources produced files with the id NOISE");
612       listNoise->Print();
613       for (Int_t jj=0;jj<listNoise->GetEntries();jj++){
614         TObjString * str = dynamic_cast<TObjString*> (listNoise->At(jj));
615         AliInfo(Form("found source %s", str->String().Data()));
616         // file to be stored run per run
617         TString fileNameNoise = GetFile(kDAQ, "NOISE", str->GetName());
618         if (fileNameNoise.Length()>0){
619           // storing refernce data
620           AliInfo(Form("Got the file %s, now we can process noise data.", fileNameNoise.Data()));
621           daqFile = new TFile(fileNameNoise.Data(),"READ");
622           h1 = (TH1F*) daqFile->Get("hTOFnoise");
623           for (Int_t ibin=0;ibin<kSize;ibin++){
624             if ((h1->GetBinContent(ibin+1))!=-1){
625               if ((htofNoise->GetBinContent(ibin+1))==-1){
626                 htofNoise->SetBinContent(ibin+1,h1->GetBinContent(ibin+1));
627               }
628               else {
629                 Log(Form("Something strange occurred during Noise run, channel %i already read by another LDC, please check!",ibin));
630               }
631             }
632           }
633           // elaborating infos
634           for (Int_t ich =0;ich<fNChannels;ich++){
635             AliTOFChannelOnlineStatus * chSt = (AliTOFChannelOnlineStatus *)fCalStatus->At(ich);
636             if (h1->GetBinContent(ich+1)==-1) continue;
637             AliDebug(1,Form( " channel %i",ich));
638             AliDebug(1,Form( " channel status before noise = %i",(Int_t)chSt->GetStatus()));
639
640             /* check whether channel has been read out during current run.
641              * if the status is bad it means it has not been read out.
642              * in this case skip channel in order to leave its status 
643              * unchanged */
644             if (((AliTOFChannelOnlineStatus *)fFEEStatus->At(ich))->GetStatus() == AliTOFChannelOnlineStatus::kTOFHWBad)
645               continue;
646
647             if (h1->GetBinContent(ich+1)>=1){  // setting limit for noise to 1 kHz
648               chSt->SetStatus(chSt->GetStatus()|AliTOFChannelOnlineStatus::kTOFNoiseBad);  // bad status for noise
649               AliDebug(1,Form( " channel status after noise = %i",(Int_t)chSt->GetStatus()));
650             }
651             else {
652               chSt->SetStatus(chSt->GetStatus()|AliTOFChannelOnlineStatus::kTOFNoiseOk);  // bad status for noise
653               AliDebug(1,Form(" channel status after noise = %i",(Int_t)chSt->GetStatus()));
654             }
655           }
656  
657           daqFile->Close();
658           delete daqFile;
659           delete h1;
660
661         }
662         
663         else{
664           Log("The input data file from DAQ (noise) was not found, TOF exiting from Shuttle "); 
665           return 13;//return error code for failure in retrieving Ref Data 
666         }
667         
668       }
669       delete listNoise;
670     }
671   else{
672     Log("The input data file list from DAQ (noise) was not found, TOF exiting from Shuttle "); 
673     return 13;//return error code for failure in retrieving Ref Data 
674   }     
675   
676   daqFile=0;
677
678   //storing in OCDB
679
680   AliCDBMetaData metaData;
681   metaData.SetBeamPeriod(0);
682   metaData.SetResponsible("Chiara Zampolli");
683   metaData.SetComment("This preprocessor fills a TObjArray object for Noise data.");
684   AliInfo("Storing Calibration Data from Noise Run");
685   resultNoise = Store("Calib","Noise",fCalStatus, &metaData,0,kTRUE);
686   if(!resultNoise){
687     Log("Some problems occurred while storing online object resulting from Noise data processing");
688     return 14;//return error code for problems in storing Noise data 
689   }
690
691   if(fStoreRefData){
692     
693     AliCDBMetaData metaDataHisto;
694     metaDataHisto.SetBeamPeriod(0);
695     metaDataHisto.SetResponsible("Chiara Zampolli");
696     char comment[200];
697     sprintf(comment,"This preprocessor stores the result of the noise run, TOF exiting from Shuttle ");
698     metaDataHisto.SetComment(comment);
699     AliInfo("Storing Reference Data");
700     resultNoiseRef = StoreReferenceData("Calib","NoiseData",htofNoise, &metaDataHisto);
701     if (!resultNoiseRef){
702       Log("some problems occurred::No Reference Data for noise stored");
703       return 12;//return error code for failure in storing Ref Data 
704     }
705   }
706
707   return 0;
708 }
709 //_____________________________________________________________________________
710
711 UInt_t AliTOFPreprocessor::ProcessFEEData()
712 {
713   // Processing Pulser Run data for TOF channel status
714   // dummy for the time being
715
716   Log("Processing FEE");
717
718   Bool_t updateOCDB = kFALSE;
719   AliTOFFEEReader feeReader;
720   AliTOFChannelOnlineStatus *currentChannel = NULL, *storedChannel = NULL;
721   TObjArray *currentFEE = fFEEStatus;
722   
723   TH1C hCurrentFEE("hCurrentFEE","histo with current FEE channel status", fNChannels, 0, fNChannels);
724   
725   /* load current TOF FEE config from DCS FXS, parse, 
726    * fill current FEE histogram and set FEE status */
727   
728   const char * nameFile = GetFile(kDCS,"TofFeeMap",""); 
729   AliInfo(Form("nameFile = %s",nameFile));
730   if (nameFile == NULL) {
731           return 15;
732   } 
733   feeReader.LoadFEEConfig(nameFile);
734   feeReader.ParseFEEConfig();
735   /* loop over channels */
736   for (Int_t iChannel = 0; iChannel < fNChannels; iChannel++) {
737     currentChannel = (AliTOFChannelOnlineStatus *)currentFEE->At(iChannel);
738     if (!currentChannel)
739       continue;
740     /* channel enabled. set FEE channel status ok */
741     if (feeReader.IsChannelEnabled(iChannel)) {
742       currentChannel->SetStatus(AliTOFChannelOnlineStatus::kTOFHWOk);
743       hCurrentFEE.SetBinContent(iChannel + 1, 1);
744     }
745     /* channel disabled. set FEE channel status bad */
746     else {
747       currentChannel->SetStatus(AliTOFChannelOnlineStatus::kTOFHWBad);
748     }
749   }
750   
751   /* load stored TOF FEE from OCDB and compare it with current FEE.
752    * if stored FEE is different from current FEE set update flag.
753    * if there is no stored FEE in OCDB set update flag */
754   
755   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","FEE");
756   /* no CDB entry found. set update flag */
757   if (cdbEntry == NULL) {
758     updateOCDB = kTRUE;
759   }
760   /* CDB entry OK. loop over channels */
761   else {
762     TObjArray *storedFEE = (TObjArray *)cdbEntry->GetObject();
763     for (Int_t iChannel = 0; iChannel < fNChannels; iChannel++){
764       currentChannel = (AliTOFChannelOnlineStatus *)currentFEE->At(iChannel);
765       storedChannel = (AliTOFChannelOnlineStatus *)storedFEE->At(iChannel);
766       /* compare current FEE channel status with stored one 
767        * if different set update flag and break loop */
768       if (currentChannel->GetStatus() != storedChannel->GetStatus()) {
769         updateOCDB = kTRUE;
770         break;
771       }
772     }
773   }
774
775   /* check whether we don't have to store reference data.
776    * in this case we return without errors. */
777   if (fStoreRefData) {
778           /* store reference data */
779           AliCDBMetaData metaDataHisto;
780           metaDataHisto.SetBeamPeriod(0);
781           metaDataHisto.SetResponsible("Roberto Preghenella");
782           metaDataHisto.SetComment("This preprocessor stores the FEE referece data of the current run.");
783           AliInfo("Storing FEE reference data");
784           /* store FEE reference data */
785           if (!StoreReferenceData("Calib", "FEEData", &hCurrentFEE, &metaDataHisto)) {
786                   /* failed */
787                   Log("problems while storing FEE reference data");
788                   return 18; /* error return code for problems while storing FEE reference data */
789           }
790   }
791
792   /* check whether we don't need to update OCDB.
793    * in this case we can return without errors and
794    * the current FEE is stored in the fFEEStatus TObjArray. */
795   if (!updateOCDB) {
796     AliInfo("TOF FEE config has not changed. Do not overwrite stored file.");
797     return 0; /* return ok */
798   }
799
800   /* update the OCDB with the current FEE since even 
801    * a little difference has been detected. */
802
803   AliCDBMetaData metaData;
804   metaData.SetBeamPeriod(0);
805   metaData.SetResponsible("Roberto Preghenella");
806   metaData.SetComment("This preprocessor fills a TObjArray object for FEE data.");
807   AliInfo("Storing FEE data from current run");
808   /* store FEE data */
809   if (!Store("Calib", "FEE", fFEEStatus, &metaData, 0, kTRUE)) {
810     /* failed */
811     Log("problems while storing FEE data object");
812     return 17; /* return error code for problems  while storing FEE data */
813   }
814
815   /* everything fine. return */
816
817   return 0;
818
819 }
820
821 //_____________________________________________________________________________
822
823 UInt_t AliTOFPreprocessor::Process(TMap* dcsAliasMap)
824 {
825   //
826   //
827   //
828
829   TString runType = GetRunType();
830   Log(Form("RunType %s",runType.Data()));
831   
832   // processing 
833
834   /* always process FEE data */
835   Int_t iresultFEE = ProcessFEEData();
836   if (iresultFEE != 0)
837     return iresultFEE;
838
839   if (runType == "PULSER") {
840     Int_t iresultPulser = ProcessPulserData();
841     return iresultPulser; 
842   }
843   
844   if (runType == "NOISE") { // for the time being associating noise runs with pedestal runs; proper run type to be defined 
845     Int_t iresultNoise = ProcessNoiseData();
846     return iresultNoise; 
847   }
848   
849   if (runType == "PHYSICS") {
850     Int_t iresultDCS = ProcessDCSDataPoints(dcsAliasMap);
851     if (iresultDCS != 0) {
852       return iresultDCS;
853     }
854     else { 
855       Int_t iresultDAQ = ProcessOnlineDelays();
856       return iresultDAQ;
857     }
858   }
859
860   // storing
861   return 0;
862 }
863
864