Correct formula for phi, in case of current local X <0, in GetLocalXat and GetPhiZat
[u/mrichter/AliRoot.git] / ITS / AliITSPreprocessorSDD.cxx
CommitLineData
979b5a5f 1/**************************************************************************
2 * Copyright(c) 2007-2009, 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 **************************************************************************/
e56160b8 15
20f3f947 16/* $Id$ */
5a9fd7dd 17
979b5a5f 18///////////////////////////////////////////////////////////////////
19// //
20// Implementation of the class for SDD preprocessing //
21// Origin: E.Crescio, Torino, crescio@to.infn.it //
22// F.Prino, Torino, prino@to.infn.it //
23// //
24///////////////////////////////////////////////////////////////////
25
e56160b8 26#include "AliITSPreprocessorSDD.h"
27#include "AliITSCalibrationSDD.h"
18da6e54 28#include "AliITSDriftSpeedSDD.h"
29#include "AliITSDriftSpeedArraySDD.h"
cfaccd71 30#include "AliITSDCSAnalyzerSDD.h"
5e6e7c0c 31#include "AliITSHLTforSDD.h"
e56160b8 32#include "AliShuttleInterface.h"
979b5a5f 33#include "AliCDBEntry.h"
e56160b8 34#include "AliCDBMetaData.h"
35#include "TObjArray.h"
e56160b8 36#include "AliLog.h"
dfb8947f 37#include <TObjString.h>
38#include <TSystem.h>
39#include <TList.h>
e56160b8 40
7537d03c 41const TString AliITSPreprocessorSDD::fgkNameHistoPedestals = "hpedestal";
42const TString AliITSPreprocessorSDD::fgkNameHistoNoise = "hnoise";
e56160b8 43ClassImp(AliITSPreprocessorSDD)
44
15b67cdc 45//______________________________________________________________________
46AliITSPreprocessorSDD::AliITSPreprocessorSDD( AliShuttleInterface* shuttle):
47 AliPreprocessor("SDD", shuttle)
48{
49 // constructor
50 AddRunType("PULSER");
51 AddRunType("INJECTOR");
52 AddRunType("PHYSICS");
53}
e56160b8 54
317e638f 55//______________________________________________________________________
cfaccd71 56UInt_t AliITSPreprocessorSDD::Process(TMap* dcsAliasMap){
e56160b8 57
7d21c0a9 58
979b5a5f 59 // Get DDL map from OCDB
60 AliCDBEntry* entry = GetFromOCDB("Calib", "DDLMapSDD");
61 if(!entry){
62 Log("DDL map file not found in OCDB.");
63 return 2;
64 }
65 AliITSDDLModuleMapSDD* ddlmap = (AliITSDDLModuleMapSDD*)entry->GetObject();
66 if(!ddlmap){
67 Log("AliITSDDLModuleMapSDD object not in file.");
68 return 2;
69 }
b478db1a 70 ddlmap->PrintDDLMap();
e56160b8 71
979b5a5f 72 //preprocessing
18da6e54 73 TString runType = GetRunType();
461c9263 74 Int_t retcode=0;
c2bd28b6 75
ad2585ed 76 if (runType == "PULSER"){
b478db1a 77 Log("Process FXS files from PULSER RUN");
317e638f 78 retcode=ProcessPulser(ddlmap);
79 }else if(runType== "INJECTOR"){
b478db1a 80 Log("Process FXS files from INJECTOR RUN");
317e638f 81 retcode=ProcessInjector(ddlmap);
5e6e7c0c 82 }else if(runType== "PHYSICS"){
83 retcode=ProcessPhysics();
317e638f 84 }
461c9263 85 if(retcode!=0) return retcode;
86
b478db1a 87 Log("Process DCS data");
461c9263 88 Bool_t retcodedcs =ProcessDCSDataPoints(dcsAliasMap);
89 if(retcodedcs) return 0;
90 else return 1;
91
317e638f 92}
93//______________________________________________________________________
5e6e7c0c 94UInt_t AliITSPreprocessorSDD::ProcessPhysics(){
95 // Get the HLT status for the PHYSICS run
96 // needeed to define the raw data format
97
98 AliITSHLTforSDD *hltSDD=new AliITSHLTforSDD();
99 TString hltMode = GetRunParameter("HLTmode");
100 TSubString firstChar = hltMode(0,1);
101 if (firstChar == "C") hltSDD->SetHLTmodeC(kTRUE);
102 else hltSDD->SetHLTmodeC(kFALSE);
103
104 AliCDBMetaData *md= new AliCDBMetaData();
105 md->SetResponsible("Francesco Prino");
106 md->SetBeamPeriod(0);
107 md->SetComment("HLT mode C flag for PHYSICS run");
108 Bool_t retCode = Store("Calib","HLTforSDD",hltSDD,md);
109 if(retCode) return 0;
110 else return 1;
111}
112//______________________________________________________________________
461c9263 113UInt_t AliITSPreprocessorSDD::ProcessPulser(AliITSDDLModuleMapSDD* ddlmap){
317e638f 114 // Process FXS files from PULSER run (baseline, noise, gain)
461c9263 115 // returns 0 in case of success,
116 // 1 in case of storage error,
117 // 2 in case of error with input files
317e638f 118 TObjArray calSDD(kNumberOfSDD);
119 calSDD.SetOwner(kFALSE);
7d21c0a9 120
317e638f 121 Float_t baseline,rawnoise,cmn,corn,gain;
122 Int_t isgoodan,i,im,is,isgoodmod,basmin,basoff;
e7610d19 123 Int_t th,tl;
317e638f 124 Int_t numOfBadChannels[kNumberOfSDD];
18da6e54 125
317e638f 126 TList* sourceList = GetFileSources(kDAQ, "SDD_Calib");
127 if (!sourceList){
128 Log("Error: no sources found for SDD_Calib");
129 return 2;
130 }
dfb8947f 131
317e638f 132 Int_t ind = 0;
133 while (sourceList->At(ind)!=NULL) {
134 TObjString* tarId = (TObjString*) sourceList->At(ind);
135 TString tarName = GetFile(kDAQ, "SDD_Calib", tarId->GetString().Data());
136 if(tarName.Length()==0){
137 Log(Form("Baseline tar file from source %d not found.",ind));
138 return 2;
18da6e54 139 }
7d21c0a9 140 TString command;
141 command.Form("tar -xf %s",tarName.Data());
317e638f 142 gSystem->Exec(command);
143 ind++;
144 }
145 delete sourceList;
146
147 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
148 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
149 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
150 if(modID==-1) continue;
151 modID-=240; // to have SDD modules numbering from 0 to 260
152 AliITSCalibrationSDD *cal = new AliITSCalibrationSDD("simulated");
2140af73 153 cal->SetAMAt20MHz(); // for runs > 51275 with clock at 20 MHz
317e638f 154 numOfBadChannels[modID]=0;
155 Int_t badch[kNumberOfChannels];
156 for(Int_t isid=0;isid<=1;isid++){
7d21c0a9 157 TString inpFileName;
158 inpFileName.Form("./SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
159
317e638f 160 FILE* basFil = fopen(inpFileName,"read");
161 if (basFil == 0) {
7d21c0a9 162 Log(Form("File %s not found.",inpFileName.Data()));
317e638f 163 cal->SetBad();
164 continue;
165 }
166 fscanf(basFil,"%d %d %d\n",&im,&is,&isgoodmod);
20f3f947 167 if(!isgoodmod) cal->SetBad();
e7610d19 168 fscanf(basFil,"%d\n",&th);
169 fscanf(basFil,"%d\n",&tl);
20f3f947 170 cal->SetZSLowThreshold(isid,tl);
171 cal->SetZSHighThreshold(isid,th);
317e638f 172 for(Int_t ian=0;ian<(kNumberOfChannels/2);ian++){
173 fscanf(basFil,"%d %d %f %d %d %f %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn,&gain);
174 Int_t ich=ian;
175 if(isid==1) ich+=256;
176 if(!isgoodan){
177 Int_t ibad=numOfBadChannels[modID];
178 badch[ibad]=ich;
179 numOfBadChannels[modID]++;
979b5a5f 180 }
20f3f947 181 cal->SetBaseline(ich,baseline-basoff);
317e638f 182 cal->SetNoiseAfterElectronics(ich,rawnoise);
183 cal->SetGain(ich,gain);
979b5a5f 184 }
317e638f 185 cal->SetDeadChannels(numOfBadChannels[modID]);
186 for(Int_t ibad=0;ibad<numOfBadChannels[modID];ibad++){
187 cal->SetBadChannel(ibad,badch[ibad]);
188 }
189 fclose(basFil);
c2bd28b6 190 }
b478db1a 191 Log(Form("Put calib obj for module %d (DDL %d Carlos %d)",modID,iddl,imod));
317e638f 192 calSDD.AddAt(cal,modID);
e56160b8 193 }
317e638f 194 }
195 AliCDBMetaData *md= new AliCDBMetaData();
196 md->SetResponsible("Francesco Prino");
197 md->SetBeamPeriod(0);
198 md->SetComment("AliITSCalibrationSDD from PEDESTAL+PULSER runs");
199 Bool_t retCode = Store("Calib","CalibSDD",&calSDD,md, 0, kTRUE);
461c9263 200 if(retCode) return 0;
201 else return 1;
317e638f 202}
203//______________________________________________________________________
461c9263 204UInt_t AliITSPreprocessorSDD::ProcessInjector(AliITSDDLModuleMapSDD* ddlmap){
317e638f 205 // Process FXS files from injector events (INJECTOR or PHYSICS runs)
461c9263 206 // returns 0 in case of success,
207 // 1 in case of storage error,
208 // 2 in case of error with input files
317e638f 209 TObjArray vdrift(2*kNumberOfSDD);
210 vdrift.SetOwner(kFALSE);
317e638f 211 Int_t evNumb,polDeg;
212 UInt_t timeStamp;
9f963593 213 Bool_t modSet[2*kNumberOfSDD]; // flag modules with good inj.
5eb2b05e 214 for(Int_t ihyb=0; ihyb<2*kNumberOfSDD; ihyb++) modSet[ihyb]=0;
9f963593 215 Double_t nPt = 0;
216
217 Double_t param[4]; // parameters of poly fit
218 Double_t minValP0=4.; // min value for param[0]
219 Double_t maxValP0=9.; // max value for param[0]
220 Double_t minValP1=0.; // min value for param[1]
221 Double_t aveCoef[4]={0.,0.,0.,0.}; // average param for good mod.
222 Double_t defCoef[4]={6.53227,0.00128941,-5.14493e-06,0}; // default values for param
223 Float_t auxP;
e56160b8 224
317e638f 225 TList* sourceList = GetFileSources(kDAQ, "SDD_Injec");
226 if (!sourceList){
227 Log("Error: no sources found for SDD_Injec");
228 return 2;
229 }
230 Int_t ind = 0;
231 while (sourceList->At(ind)!=NULL) {
232 TObjString* tarId = (TObjString*) sourceList->At(ind);
233 TString tarName = GetFile(kDAQ, "SDD_Injec", tarId->GetString().Data());
234 if(tarName.Length()==0){
235 Log(Form("Injector tar file from source %d not found.",ind));
18da6e54 236 return 2;
237 }
7d21c0a9 238 TString command;
239 command.Form("tar -xf %s",tarName.Data());
317e638f 240 gSystem->Exec(command);
241 ind++;
242 }
243 delete sourceList;
9f963593 244
245
317e638f 246 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
247 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
248 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
249 if(modID==-1) continue;
250 modID-=240; // to have SDD modules numbering from 0 to 260
251 for(Int_t isid=0;isid<=1;isid++){
252 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
7d21c0a9 253 TString inpFileName;
254 inpFileName.Form("./SDDinj_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
317e638f 255 FILE* injFil = fopen(inpFileName,"read");
256 if (injFil == 0) {
7d21c0a9 257 Log(Form("File %s not found.",inpFileName.Data()));
317e638f 258 AliITSDriftSpeedSDD *dsp=new AliITSDriftSpeedSDD();
259 arr->AddDriftSpeed(dsp);
979b5a5f 260 vdrift.AddAt(arr,2*modID+isid);
317e638f 261 continue;
262 }
263 fscanf(injFil,"%d",&polDeg);
264 while (!feof(injFil)){
938494b4 265 fscanf(injFil,"%d %u ",&evNumb,&timeStamp);
317e638f 266 if(feof(injFil)) break;
9f963593 267 for(Int_t ic=0;ic<4;ic++){
268 fscanf(injFil,"%f ",&auxP);
269 param[ic]=auxP;
270 }
271
272 if(param[0]>minValP0 && param[0]<maxValP0 && param[1]>minValP1){
273 for(Int_t ic=0;ic<4;ic++) aveCoef[ic]+=param[ic];
274 nPt++;
275 AliITSDriftSpeedSDD *dsp=new AliITSDriftSpeedSDD(evNumb,timeStamp,polDeg,param);
276 arr->AddDriftSpeed(dsp);
277 modSet[2*modID+isid]=1;
278 }
18da6e54 279 }
b478db1a 280 Log(Form("Put calib obj for hybrid %d (DDL %d Carlos %d)",2*modID+isid,iddl,imod));
9f963593 281 if(modSet[2*modID+isid]) vdrift.AddAt(arr,2*modID+isid);
18da6e54 282 }
283 }
cfaccd71 284 }
9f963593 285
286 // set drift speed for modules with bad injectors
287 for(Int_t ic=0;ic<4;ic++){
288 if(nPt>0) aveCoef[ic]/=nPt; // mean parameters
289 else aveCoef[ic]=defCoef[ic]; // default parameters
290 }
291 AliITSDriftSpeedSDD *avdsp=new AliITSDriftSpeedSDD(evNumb,timeStamp,polDeg,aveCoef);
292
293 for(Int_t ihyb=0; ihyb<2*kNumberOfSDD; ihyb++){
294 if(modSet[ihyb]==0){
295 AliWarning(Form("No good injector events for mod. %d --> use average values",ihyb/2));
296 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
297 arr->AddDriftSpeed(avdsp);
298 vdrift.AddAt(arr,ihyb);
299 }
300 }
301
302
317e638f 303 AliCDBMetaData *md= new AliCDBMetaData();
304 md->SetResponsible("Francesco Prino");
305 md->SetBeamPeriod(0);
306 md->SetComment("AliITSDriftSpeedSDD from injector events");
307 Bool_t retCode = Store("Calib","DriftSpeedSDD",&vdrift,md,0, kTRUE);
461c9263 308 if(retCode) return 0;
309 else return 1;
317e638f 310}
311//______________________________________________________________________
312Bool_t AliITSPreprocessorSDD::ProcessDCSDataPoints(TMap* dcsAliasMap){
313 // Process DCS data
314 AliITSDCSAnalyzerSDD *dcs=new AliITSDCSAnalyzerSDD();
315 dcs->AnalyzeData(dcsAliasMap);
316 TObjArray refDCS(kNumberOfSDD);
317 refDCS.SetOwner(kFALSE);
318 for(Int_t imod=0;imod<kNumberOfSDD;imod++){
319 AliITSDCSDataSDD *dcsdata=dcs->GetDCSData(imod);
320 refDCS.Add(dcsdata);
321 }
322 AliCDBMetaData *md= new AliCDBMetaData();
323 md->SetResponsible("Francesco Prino");
324 md->SetBeamPeriod(0);
325 md->SetComment("AliITSDCSDataSDD objects from DCS DB");
326 Bool_t retCode = StoreReferenceData("DCS","DataSDD",&refDCS,md);
327 return retCode;
e56160b8 328}
5eb2b05e 329