New object for SDD drift speed calibration in modules with injectors dameged by the...
[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"
e56160b8 31#include "AliShuttleInterface.h"
979b5a5f 32#include "AliCDBEntry.h"
e56160b8 33#include "AliCDBMetaData.h"
34#include "TObjArray.h"
e56160b8 35#include "AliLog.h"
dfb8947f 36#include <TObjString.h>
37#include <TSystem.h>
38#include <TList.h>
e56160b8 39
7537d03c 40const TString AliITSPreprocessorSDD::fgkNameHistoPedestals = "hpedestal";
41const TString AliITSPreprocessorSDD::fgkNameHistoNoise = "hnoise";
e56160b8 42ClassImp(AliITSPreprocessorSDD)
43
15b67cdc 44//______________________________________________________________________
45AliITSPreprocessorSDD::AliITSPreprocessorSDD( AliShuttleInterface* shuttle):
46 AliPreprocessor("SDD", shuttle)
47{
48 // constructor
49 AddRunType("PULSER");
50 AddRunType("INJECTOR");
51 AddRunType("PHYSICS");
52}
e56160b8 53
317e638f 54//______________________________________________________________________
cfaccd71 55UInt_t AliITSPreprocessorSDD::Process(TMap* dcsAliasMap){
e56160b8 56
7d21c0a9 57
979b5a5f 58 // Get DDL map from OCDB
59 AliCDBEntry* entry = GetFromOCDB("Calib", "DDLMapSDD");
60 if(!entry){
61 Log("DDL map file not found in OCDB.");
62 return 2;
63 }
64 AliITSDDLModuleMapSDD* ddlmap = (AliITSDDLModuleMapSDD*)entry->GetObject();
65 if(!ddlmap){
66 Log("AliITSDDLModuleMapSDD object not in file.");
67 return 2;
68 }
b478db1a 69 ddlmap->PrintDDLMap();
e56160b8 70
979b5a5f 71 //preprocessing
18da6e54 72 TString runType = GetRunType();
461c9263 73 Int_t retcode=0;
c2bd28b6 74
ad2585ed 75 if (runType == "PULSER"){
b478db1a 76 Log("Process FXS files from PULSER RUN");
317e638f 77 retcode=ProcessPulser(ddlmap);
78 }else if(runType== "INJECTOR"){
b478db1a 79 Log("Process FXS files from INJECTOR RUN");
317e638f 80 retcode=ProcessInjector(ddlmap);
317e638f 81 }
461c9263 82 if(retcode!=0) return retcode;
83
b478db1a 84 Log("Process DCS data");
461c9263 85 Bool_t retcodedcs =ProcessDCSDataPoints(dcsAliasMap);
86 if(retcodedcs) return 0;
87 else return 1;
88
317e638f 89}
90//______________________________________________________________________
461c9263 91UInt_t AliITSPreprocessorSDD::ProcessPulser(AliITSDDLModuleMapSDD* ddlmap){
317e638f 92 // Process FXS files from PULSER run (baseline, noise, gain)
461c9263 93 // returns 0 in case of success,
94 // 1 in case of storage error,
95 // 2 in case of error with input files
317e638f 96 TObjArray calSDD(kNumberOfSDD);
97 calSDD.SetOwner(kFALSE);
7d21c0a9 98
317e638f 99 Float_t baseline,rawnoise,cmn,corn,gain;
100 Int_t isgoodan,i,im,is,isgoodmod,basmin,basoff;
e7610d19 101 Int_t th,tl;
317e638f 102 Int_t numOfBadChannels[kNumberOfSDD];
18da6e54 103
317e638f 104 TList* sourceList = GetFileSources(kDAQ, "SDD_Calib");
105 if (!sourceList){
106 Log("Error: no sources found for SDD_Calib");
107 return 2;
108 }
dfb8947f 109
317e638f 110 Int_t ind = 0;
111 while (sourceList->At(ind)!=NULL) {
112 TObjString* tarId = (TObjString*) sourceList->At(ind);
113 TString tarName = GetFile(kDAQ, "SDD_Calib", tarId->GetString().Data());
114 if(tarName.Length()==0){
115 Log(Form("Baseline tar file from source %d not found.",ind));
116 return 2;
18da6e54 117 }
7d21c0a9 118 TString command;
119 command.Form("tar -xf %s",tarName.Data());
317e638f 120 gSystem->Exec(command);
121 ind++;
122 }
123 delete sourceList;
27416b0d 124 // Read ADC sampling frequency from fee.conf
125 Int_t amSamplFreq=40;
296ee458 126 Int_t retfscf;
27416b0d 127 FILE* feefil=fopen("fee.conf","r");
128 if(feefil){
296ee458 129 retfscf=fscanf(feefil,"%d \n",&amSamplFreq);
27416b0d 130 fclose(feefil);
131 Log(Form("AM sampling frequency = %d MHz",amSamplFreq));
132 }else{
133 Log("File fee.conf not found. AM sampling set at 40 MHz by default");
134 }
317e638f 135
136 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
137 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
138 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
139 if(modID==-1) continue;
140 modID-=240; // to have SDD modules numbering from 0 to 260
141 AliITSCalibrationSDD *cal = new AliITSCalibrationSDD("simulated");
27416b0d 142 if(amSamplFreq!=40) cal->SetAMAt20MHz();
317e638f 143 numOfBadChannels[modID]=0;
144 Int_t badch[kNumberOfChannels];
296ee458 145 Bool_t sid0ok=kTRUE;
146 Bool_t sid1ok=kTRUE;
317e638f 147 for(Int_t isid=0;isid<=1;isid++){
7d21c0a9 148 TString inpFileName;
149 inpFileName.Form("./SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
150
317e638f 151 FILE* basFil = fopen(inpFileName,"read");
152 if (basFil == 0) {
7d21c0a9 153 Log(Form("File %s not found.",inpFileName.Data()));
296ee458 154 if(isid==0){
155 sid0ok=kFALSE;
156 for(Int_t iChip=0; iChip<4; iChip++) cal->SetChipBad(iChip);
157 cal->SetDeadChannels(cal->GetDeadChannels()+256);
158 for(Int_t iAnode=0; iAnode<256; iAnode++) cal->SetBadChannel(iAnode,iAnode);
159 }else{
160 sid1ok=kFALSE;
161 for(Int_t iChip=4; iChip<8; iChip++) cal->SetChipBad(iChip);
162 cal->SetDeadChannels(cal->GetDeadChannels()+256);
163 for(Int_t iAnode=0; iAnode<256; iAnode++) cal->SetBadChannel(iAnode,iAnode+256);
164 }
317e638f 165 continue;
166 }
296ee458 167
168 retfscf=fscanf(basFil,"%d %d %d\n",&im,&is,&isgoodmod);
169 if(!isgoodmod){
170 if(isid==0){
171 sid0ok=kFALSE;
172 for(Int_t iChip=0; iChip<4; iChip++) cal->SetChipBad(iChip);
173 }else{
174 sid1ok=kFALSE;
175 for(Int_t iChip=4; iChip<8; iChip++) cal->SetChipBad(iChip);
176 }
177 }
178 retfscf=fscanf(basFil,"%d\n",&th);
179 retfscf=fscanf(basFil,"%d\n",&tl);
20f3f947 180 cal->SetZSLowThreshold(isid,tl);
181 cal->SetZSHighThreshold(isid,th);
317e638f 182 for(Int_t ian=0;ian<(kNumberOfChannels/2);ian++){
296ee458 183 retfscf=fscanf(basFil,"%d %d %f %d %d %f %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn,&gain);
317e638f 184 Int_t ich=ian;
185 if(isid==1) ich+=256;
186 if(!isgoodan){
187 Int_t ibad=numOfBadChannels[modID];
188 badch[ibad]=ich;
189 numOfBadChannels[modID]++;
979b5a5f 190 }
20f3f947 191 cal->SetBaseline(ich,baseline-basoff);
317e638f 192 cal->SetNoiseAfterElectronics(ich,rawnoise);
193 cal->SetGain(ich,gain);
979b5a5f 194 }
317e638f 195 cal->SetDeadChannels(numOfBadChannels[modID]);
196 for(Int_t ibad=0;ibad<numOfBadChannels[modID];ibad++){
197 cal->SetBadChannel(ibad,badch[ibad]);
198 }
199 fclose(basFil);
c2bd28b6 200 }
296ee458 201 if(!sid0ok && !sid1ok) cal->SetBad();
b478db1a 202 Log(Form("Put calib obj for module %d (DDL %d Carlos %d)",modID,iddl,imod));
317e638f 203 calSDD.AddAt(cal,modID);
e56160b8 204 }
317e638f 205 }
206 AliCDBMetaData *md= new AliCDBMetaData();
207 md->SetResponsible("Francesco Prino");
208 md->SetBeamPeriod(0);
209 md->SetComment("AliITSCalibrationSDD from PEDESTAL+PULSER runs");
210 Bool_t retCode = Store("Calib","CalibSDD",&calSDD,md, 0, kTRUE);
461c9263 211 if(retCode) return 0;
212 else return 1;
317e638f 213}
214//______________________________________________________________________
461c9263 215UInt_t AliITSPreprocessorSDD::ProcessInjector(AliITSDDLModuleMapSDD* ddlmap){
317e638f 216 // Process FXS files from injector events (INJECTOR or PHYSICS runs)
461c9263 217 // returns 0 in case of success,
218 // 1 in case of storage error,
219 // 2 in case of error with input files
317e638f 220 TObjArray vdrift(2*kNumberOfSDD);
221 vdrift.SetOwner(kFALSE);
317e638f 222 Int_t evNumb,polDeg;
50d25e98 223 UInt_t timeStamp,statusInj;
9f963593 224 Bool_t modSet[2*kNumberOfSDD]; // flag modules with good inj.
5eb2b05e 225 for(Int_t ihyb=0; ihyb<2*kNumberOfSDD; ihyb++) modSet[ihyb]=0;
50d25e98 226 Double_t nPtLay3 = 0;
227 Double_t nPtLay4 = 0;
9f963593 228
229 Double_t param[4]; // parameters of poly fit
915c0469 230 Double_t minValP0=5.; // min value for param[0]
231 Double_t maxValP0=8.; // max value for param[0]
9f963593 232 Double_t minValP1=0.; // min value for param[1]
50d25e98 233 Double_t aveCoefLay3[4]={0.,0.,0.,0.}; // average param for good mod.
234 Double_t aveCoefLay4[4]={0.,0.,0.,0.}; // average param for good mod.
9f963593 235 Double_t defCoef[4]={6.53227,0.00128941,-5.14493e-06,0}; // default values for param
236 Float_t auxP;
e56160b8 237
317e638f 238 TList* sourceList = GetFileSources(kDAQ, "SDD_Injec");
239 if (!sourceList){
240 Log("Error: no sources found for SDD_Injec");
241 return 2;
242 }
243 Int_t ind = 0;
244 while (sourceList->At(ind)!=NULL) {
245 TObjString* tarId = (TObjString*) sourceList->At(ind);
246 TString tarName = GetFile(kDAQ, "SDD_Injec", tarId->GetString().Data());
247 if(tarName.Length()==0){
248 Log(Form("Injector tar file from source %d not found.",ind));
18da6e54 249 return 2;
250 }
7d21c0a9 251 TString command;
252 command.Form("tar -xf %s",tarName.Data());
317e638f 253 gSystem->Exec(command);
254 ind++;
255 }
256 delete sourceList;
296ee458 257 Int_t retfscf;
9f963593 258
317e638f 259 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
260 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
261 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
262 if(modID==-1) continue;
263 modID-=240; // to have SDD modules numbering from 0 to 260
264 for(Int_t isid=0;isid<=1;isid++){
265 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
7d21c0a9 266 TString inpFileName;
267 inpFileName.Form("./SDDinj_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
317e638f 268 FILE* injFil = fopen(inpFileName,"read");
269 if (injFil == 0) {
7d21c0a9 270 Log(Form("File %s not found.",inpFileName.Data()));
317e638f 271 continue;
272 }
296ee458 273 retfscf=fscanf(injFil,"%d",&polDeg);
317e638f 274 while (!feof(injFil)){
296ee458 275 retfscf=fscanf(injFil,"%d %u ",&evNumb,&timeStamp);
50d25e98 276 if(evNumb==-99){
277 statusInj=timeStamp;
278 arr->SetInjectorStatus(statusInj);
279 }else{
280 if(feof(injFil)) break;
281 for(Int_t ic=0;ic<4;ic++){
282 retfscf=fscanf(injFil,"%f ",&auxP);
283 param[ic]=auxP;
284 }
9f963593 285
7a8c5708 286 if(polDeg>=0 && polDeg<=AliITSDriftSpeedSDD::GetMaxPolDeg() &&
287 param[0]>minValP0 && param[0]<maxValP0 && param[1]>minValP1){
915c0469 288 if(polDeg==3){
289 if(modID<kNumberOfSDDLay3){
290 for(Int_t ic=0;ic<4;ic++) aveCoefLay3[ic]+=param[ic];
291 nPtLay3++;
292 }else{
293 for(Int_t ic=0;ic<4;ic++) aveCoefLay4[ic]+=param[ic];
294 nPtLay4++;
295 }
50d25e98 296 }
297 AliITSDriftSpeedSDD *dsp=new AliITSDriftSpeedSDD(evNumb,timeStamp,polDeg,param);
298 arr->AddDriftSpeed(dsp);
299 modSet[2*modID+isid]=1;
915c0469 300 }else{
301 Log(Form("Module %d side %d not accepted, degree=%d, params=%g %g %g %g",modID+240,isid,polDeg,param[0],param[1],param[2],param[3]));
50d25e98 302 }
9f963593 303 }
18da6e54 304 }
9ebc7917 305 fclose(injFil);
b478db1a 306 Log(Form("Put calib obj for hybrid %d (DDL %d Carlos %d)",2*modID+isid,iddl,imod));
9f963593 307 if(modSet[2*modID+isid]) vdrift.AddAt(arr,2*modID+isid);
18da6e54 308 }
309 }
cfaccd71 310 }
9f963593 311
312 // set drift speed for modules with bad injectors
313 for(Int_t ic=0;ic<4;ic++){
50d25e98 314 if(nPtLay3>0) aveCoefLay3[ic]/=nPtLay3; // mean parameters
315 else aveCoefLay3[ic]=defCoef[ic]; // default parameters
316 if(nPtLay4>0) aveCoefLay4[ic]/=nPtLay4; // mean parameters
317 else aveCoefLay4[ic]=defCoef[ic]; // default parameters
9f963593 318 }
2c4e6a6a 319 AliITSDriftSpeedSDD *avdsp3=new AliITSDriftSpeedSDD(evNumb,timeStamp,3,aveCoefLay3);
320 AliITSDriftSpeedSDD *avdsp4=new AliITSDriftSpeedSDD(evNumb,timeStamp,3,aveCoefLay4);
9f963593 321
50d25e98 322 for(Int_t ihyb=0; ihyb<2*kNumberOfSDDLay3; ihyb++){
9f963593 323 if(modSet[ihyb]==0){
50d25e98 324 AliWarning(Form("No good injector events for mod. %d side %d --> use average values for layer 3",ihyb/2,ihyb%2));
9f963593 325 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
50d25e98 326 arr->AddDriftSpeed(avdsp3);
327 arr->SetInjectorStatus(0);
9f963593 328 vdrift.AddAt(arr,ihyb);
329 }
330 }
331
50d25e98 332 for(Int_t ihyb=2*kNumberOfSDDLay3; ihyb<2*kNumberOfSDD; ihyb++){
333 if(modSet[ihyb]==0){
334 AliWarning(Form("No good injector events for mod. %d side %d --> use average values for layer 4",ihyb/2,ihyb%2));
335 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
336 arr->AddDriftSpeed(avdsp4);
337 arr->SetInjectorStatus(0);
338 vdrift.AddAt(arr,ihyb);
339 }
340 }
9f963593 341
317e638f 342 AliCDBMetaData *md= new AliCDBMetaData();
343 md->SetResponsible("Francesco Prino");
344 md->SetBeamPeriod(0);
345 md->SetComment("AliITSDriftSpeedSDD from injector events");
346 Bool_t retCode = Store("Calib","DriftSpeedSDD",&vdrift,md,0, kTRUE);
461c9263 347 if(retCode) return 0;
348 else return 1;
317e638f 349}
350//______________________________________________________________________
351Bool_t AliITSPreprocessorSDD::ProcessDCSDataPoints(TMap* dcsAliasMap){
352 // Process DCS data
353 AliITSDCSAnalyzerSDD *dcs=new AliITSDCSAnalyzerSDD();
354 dcs->AnalyzeData(dcsAliasMap);
355 TObjArray refDCS(kNumberOfSDD);
356 refDCS.SetOwner(kFALSE);
357 for(Int_t imod=0;imod<kNumberOfSDD;imod++){
358 AliITSDCSDataSDD *dcsdata=dcs->GetDCSData(imod);
359 refDCS.Add(dcsdata);
360 }
361 AliCDBMetaData *md= new AliCDBMetaData();
362 md->SetResponsible("Francesco Prino");
363 md->SetBeamPeriod(0);
364 md->SetComment("AliITSDCSDataSDD objects from DCS DB");
365 Bool_t retCode = StoreReferenceData("DCS","DataSDD",&refDCS,md);
366 return retCode;
e56160b8 367}
5eb2b05e 368