Holes for PHOS
[u/mrichter/AliRoot.git] / TOF / AliTOFPreprocessor.cxx
CommitLineData
c9fe8530 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
9edefa04 16/* $Id$ */
9d883ed9 17
9edefa04 18#include <Riostream.h>
19#include <stdio.h>
20#include <stdlib.h>
c9fe8530 21
9edefa04 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>
c9fe8530 31
32#include "AliCDBMetaData.h"
33#include "AliLog.h"
c9fe8530 34#include "AliTOFChannelOnline.h"
9edefa04 35#include "AliTOFDataDCS.h"
10056aa6 36#include "AliTOFGeometry.h"
9edefa04 37#include "AliTOFPreprocessor.h"
c9fe8530 38
39class TF1;
40class AliDCSValue;
41class AliTOFGeometry;
42
43// TOF preprocessor class.
44// It takes data from DCS and passes them to the class AliTOFDataDCS, which
45// processes them. The result is then written to the CDB.
46// analogously, it takes data form DAQ (both at Run level and inclusive -
47// of all the runs - level, processes them, and stores both Reference Data
48// and Online Calibration files in the CDB.
49
50
51ClassImp(AliTOFPreprocessor)
52
53const Int_t AliTOFPreprocessor::fgkBinRangeAve = 13; // number of bins where to calculate the mean
9d883ed9 54const Double_t AliTOFPreprocessor::fgkThrPar = 0.013; // parameter used to trigger the calculation of the delay
c9fe8530 55
56//_____________________________________________________________________________
57
708db10b 58AliTOFPreprocessor::AliTOFPreprocessor(AliShuttleInterface* shuttle) :
59 AliPreprocessor("TOF", shuttle),
c9fe8530 60 fData(0),
708db10b 61 fh2(0),
c9fe8530 62 fCal(0),
10056aa6 63 fNChannels(0),
5936ab02 64 fStoreRefData(kTRUE)
c9fe8530 65{
66 // constructor
10056aa6 67
c9fe8530 68}
69
70//_____________________________________________________________________________
71
72AliTOFPreprocessor::~AliTOFPreprocessor()
73{
74 // destructor
7981612d 75 if (fData){
76 delete fData;
77 fData = 0;
78 }
79 if (fh2){
80 delete fh2;
81 fh2 = 0;
82 }
83 if (fCal){
84 fCal->Clear();
85 delete fCal;
86 fCal = 0;
87 }
c9fe8530 88}
89
90//______________________________________________________________________________
91void AliTOFPreprocessor::Initialize(Int_t run, UInt_t startTime,
92 UInt_t endTime)
93{
94 // Creates AliTOFDataDCS object
95
96 AliPreprocessor::Initialize(run, startTime, endTime);
97
98 AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
99 TTimeStamp(startTime).AsString(),
100 TTimeStamp(endTime).AsString()));
101
102 fData = new AliTOFDataDCS(fRun, fStartTime, fEndTime);
708db10b 103 fh2 = 0x0;
10056aa6 104 fNChannels = AliTOFGeometry::NSectors()*(2*(AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB())+AliTOFGeometry::NStripA())*AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX();
105 fCal = new TObjArray(fNChannels);
106 fCal->SetOwner();
107 for (Int_t ich = 0; ich<fNChannels; ich ++){
108 AliTOFChannelOnline * calChOnline = new AliTOFChannelOnline();
109 fCal->AddAt(calChOnline,ich);
110 }
c9fe8530 111}
112
113//_____________________________________________________________________________
114
115UInt_t AliTOFPreprocessor::Process(TMap* dcsAliasMap)
116{
117 // Fills data into a AliTOFDataDCS object
5936ab02 118 // return codes:
119 // return=0 : all ok
7885c291 120 // return=1 : no DCS input data Map
121 // return=2 : no DCS input data processing
b0d8f3fc 122 // return=3 : no DCS processed data was stored in Ref Data
7885c291 123 // return=4 : no DAQ input for Ref Data
b0d8f3fc 124 // return=5 : failed to store Ref Data
7885c291 125 // return=6 : failed to retrieve DAQ data for calibration
126 // return=7 : problems in histos in the input DAQ file
127 // return=8 : failed to store Online Delays
c9fe8530 128
708db10b 129 TH1::AddDirectory(0);
5936ab02 130
7885c291 131 Bool_t resultDCSMap=kFALSE;
132 Bool_t resultDCSStore=kFALSE;
133 Bool_t resultDAQ=kFALSE;
134 Bool_t resultDAQRef=kFALSE;
c9fe8530 135
136 // processing DCS
137
138 if (!dcsAliasMap){
5936ab02 139 Log("No DCS map found: TOF exiting from Shuttle");
140 return 1;// return error Code for DCS input data not found
c9fe8530 141 }
142 else {
143 // The processing of the DCS input data is forwarded to AliTOFDataDCS
7885c291 144 resultDCSMap=fData->ProcessData(*dcsAliasMap);
145 if(!resultDCSMap){
146 Log("Some problems occurred while processing DCS data, TOF exiting from Shuttle");
5936ab02 147 return 2;// return error Code for processed DCS data not stored
c9fe8530 148 }
7885c291 149 else{
150 AliCDBMetaData metaDataDCS;
151 metaDataDCS.SetBeamPeriod(0);
152 metaDataDCS.SetResponsible("Chiara Zampolli");
153 metaDataDCS.SetComment("This preprocessor fills an AliTOFDataDCS object.");
154 AliInfo("Storing DCS Data");
b0d8f3fc 155 resultDCSStore = StoreReferenceData("Calib","DCSData",fData, &metaDataDCS);
7885c291 156 if (!resultDCSStore){
b0d8f3fc 157 Log("Some problems occurred while storing DCS data results in Reference Data, TOF exiting from Shuttle");
7885c291 158 return 3;// return error Code for processed DCS data not stored
b0d8f3fc 159 // in reference data
7885c291 160
161 }
162 }
c9fe8530 163 }
7885c291 164
c9fe8530 165 // processing DAQ
7885c291 166
c9fe8530 167 TFile * daqFile=0x0;
7885c291 168
5936ab02 169 if(fStoreRefData){
170 //retrieving data at Run level
171 TList* list = GetFileSources(kDAQ, "RUNLevel");
172 if (list)
173 {
174 AliInfo("The following sources produced files with the id RUNLevel");
175 list->Print();
176 for (Int_t jj=0;jj<list->GetEntries();jj++){
177 TObjString * str = dynamic_cast<TObjString*> (list->At(jj));
178 AliInfo(Form("found source %s", str->String().Data()));
179 // file to be stored run per run
180 TString fileNameRun = GetFile(kDAQ, "RUNLevel", str->GetName());
181 if (fileNameRun.Length()>0){
182 AliInfo(Form("Got the file %s, now we can store the Reference Data for the current Run.", fileNameRun.Data()));
183 daqFile = new TFile(fileNameRun.Data(),"READ");
184 fh2 = (TH2S*) daqFile->Get("htof");
185 AliCDBMetaData metaDataHisto;
186 metaDataHisto.SetBeamPeriod(0);
187 metaDataHisto.SetResponsible("Chiara Zampolli");
188 metaDataHisto.SetComment("This preprocessor stores the array of histos object as Reference Data.");
189 AliInfo("Storing Reference Data");
190 resultDAQRef = StoreReferenceData("Calib","DAQData",fh2, &metaDataHisto);
191 if (!resultDAQRef){
192 Log("some problems occurred::No Reference Data stored, TOF exiting from Shuttle");
7885c291 193 return 5;//return error code for failure in storing Ref Data
5936ab02 194 }
195 daqFile->Close();
196 delete daqFile;
197 }
198
199 else{
200 Log("The input data file from DAQ (run-level) was not found, TOF exiting from Shuttle ");
7885c291 201 return 4;//return error code for failure in retrieving Ref Data
c9fe8530 202 }
c9fe8530 203 }
9bc469d1 204 }
5936ab02 205 else{
206 Log("The input data file list from DAQ (run-level) was not found, TOF exiting from Shuttle ");
7885c291 207 return 4;//return error code for failure in retrieving Ref Data
5936ab02 208 }
9bc469d1 209 }
210
9bc469d1 211
5936ab02 212//Total files, with cumulative histos
213
9bc469d1 214 TList* listTot = GetFileSources(kDAQ, "DELAYS");
215 if (listTot)
216 {
217 AliInfo("The following sources produced files with the id DELAYS");
218 listTot->Print();
219 for (Int_t jj=0;jj<listTot->GetEntries();jj++){
220 TObjString * str = dynamic_cast<TObjString*> (listTot->At(jj));
221 AliInfo(Form("found source %s", str->String().Data()));
222
c9fe8530 223 // file with summed histos, to extract calib params
9bc469d1 224 TString fileName = GetFile(kDAQ, "DELAYS", str->GetName());
225 if (fileName.Length()>0){
226 AliInfo(Form("Got the file %s, now we can extract some values.", fileName.Data()));
c9fe8530 227
9bc469d1 228 daqFile = new TFile(fileName.Data(),"READ");
708db10b 229 fh2 = (TH2S*) daqFile->Get("htoftot");
230 if (!fh2){
9d883ed9 231 Log("some problems occurred:: No histo retrieved, TOF exiting from Shuttle");
232 delete daqFile;
7885c291 233 return 7; //return error code for histograms not existing/junky
c9fe8530 234 }
c9fe8530 235 else {
3a3ece53 236 static const Int_t kSize=fh2->GetNbinsX();
237 static const Int_t kNBins=fh2->GetNbinsY();
238 static const Double_t kXBinmin=fh2->GetYaxis()->GetBinLowEdge(1);
10056aa6 239 if (kSize != fNChannels){
9d883ed9 240 Log(" number of bins along x different from number of pads, found only a subset of the histograms, TOF exiting from Shuttle");
241 delete daqFile;
7885c291 242 return 7; //return error code for histograms not existing/junky
9bc469d1 243 }
3a3ece53 244 for (Int_t ich=0;ich<kSize;ich++){
245 TH1S *h1 = new TH1S("h1","h1",kNBins,kXBinmin-0.5,kNBins*1.+kXBinmin-0.5);
246 for (Int_t ibin=0;ibin<kNBins;ibin++){
708db10b 247 h1->SetBinContent(ibin+1,fh2->GetBinContent(ich+1,ibin+1));
248 }
c9fe8530 249 Bool_t found=kFALSE;
9bc469d1 250 Float_t minContent=h1->Integral()*fgkThrPar;
c9fe8530 251 Int_t nbinsX = h1->GetNbinsX();
252 Int_t startBin=1;
253 for (Int_t j=1; j<=nbinsX; j++){
254 if ((
255 h1->GetBinContent(j) +
256 h1->GetBinContent(j+1)+
257 h1->GetBinContent(j+2)+
258 h1->GetBinContent(j+3))>minContent){
259 found=kTRUE;
260 startBin=j;
261 break;
262 }
263 }
708db10b 264 if(!found) AliInfo(Form("WARNING!!! no start of fit found for histo # %i",ich));
c9fe8530 265 // Now calculate the mean over the interval.
266 Double_t mean = 0;
267 Double_t sumw2 = 0;
268 Double_t nent = 0;
269 for(Int_t k=0;k<fgkBinRangeAve;k++){
270 mean=mean+h1->GetBinCenter(startBin+k)*h1->GetBinContent(startBin+k);
271 nent=nent+h1->GetBinContent(startBin+k);
272 sumw2=sumw2+(h1->GetBinCenter(startBin+k))*(h1->GetBinCenter(startBin+k))*(h1->GetBinContent(startBin+k));
273 }
c9fe8530 274 mean= mean/nent; //<x>
275 sumw2=sumw2/nent; //<x^2>
276 Double_t rmsmean= 0;
277 rmsmean = TMath::Sqrt((sumw2-mean*mean)/nent);
10056aa6 278 if (ich<fNChannels) {
279 AliTOFChannelOnline * ch = (AliTOFChannelOnline *)fCal->At(ich);
280 ch->SetDelay((Double_t)mean*AliTOFGeometry::TdcBinWidth()*1.E-3); // delay in ns
c9fe8530 281 }
708db10b 282 delete h1;
283 h1=0x0;
c9fe8530 284 }
c9fe8530 285 }
286 daqFile->Close();
287 delete daqFile;
288 AliCDBMetaData metaData;
289 metaData.SetBeamPeriod(0);
290 metaData.SetResponsible("Chiara Zampolli");
291 metaData.SetComment("This preprocessor fills an AliTOFCal object.");
5936ab02 292 AliInfo("Storing Calibration Data");
a7e9db2a 293 resultDAQ = Store("Calib","ParOnline",fCal, &metaData);
9d883ed9 294 if(!resultDAQ){
295 Log("Some problems occurred while storing DAQ data processing results");
7885c291 296 return 8;//return error code for problems in storing DAQ data
9d883ed9 297 }
c9fe8530 298 }
299 else{
9d883ed9 300 Log("The Cumulative data file from DAQ does not exist, TOF exiting from Shuttle");
7885c291 301 return 6;//return error code for problems in retrieving DAQ data
c9fe8530 302 }
303 }
304 }
305 else{
9d883ed9 306 Log("Problem: no list for Cumulative data file from DAQ was found, TOF exiting from Shuttle");
7885c291 307 return 6; //return error code for problems in retrieving DAQ data
c9fe8530 308 }
309
c9fe8530 310 daqFile=0;
9d883ed9 311
5936ab02 312 return 0;
c9fe8530 313}
314
315