Merge branch 'master' into TPCdev
[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 //
27199d86 21// Run Types treated: //
22// PULSER, INJECTOR -> Store OCDB parameters //
23// ALL RUNS -> Store DCS reference data //
979b5a5f 24// Origin: E.Crescio, Torino, crescio@to.infn.it //
25// F.Prino, Torino, prino@to.infn.it //
26// //
27///////////////////////////////////////////////////////////////////
28
27199d86 29#include "AliITSDDLModuleMapSDD.h"
e56160b8 30#include "AliITSPreprocessorSDD.h"
31#include "AliITSCalibrationSDD.h"
b34fed4e 32#include "AliITSDriftSpeedSDD.h"
18da6e54 33#include "AliITSDriftSpeedArraySDD.h"
cfaccd71 34#include "AliITSDCSAnalyzerSDD.h"
e56160b8 35#include "AliShuttleInterface.h"
979b5a5f 36#include "AliCDBEntry.h"
e56160b8 37#include "AliCDBMetaData.h"
38#include "TObjArray.h"
e56160b8 39#include "AliLog.h"
dfb8947f 40#include <TObjString.h>
41#include <TSystem.h>
42#include <TList.h>
7960eafe 43#include <TF1.h>
44#include <TH1D.h>
e56160b8 45
7537d03c 46const TString AliITSPreprocessorSDD::fgkNameHistoPedestals = "hpedestal";
47const TString AliITSPreprocessorSDD::fgkNameHistoNoise = "hnoise";
e56160b8 48ClassImp(AliITSPreprocessorSDD)
49
15b67cdc 50//______________________________________________________________________
51AliITSPreprocessorSDD::AliITSPreprocessorSDD( AliShuttleInterface* shuttle):
52 AliPreprocessor("SDD", shuttle)
53{
54 // constructor
55 AddRunType("PULSER");
56 AddRunType("INJECTOR");
57 AddRunType("PHYSICS");
58}
e56160b8 59
317e638f 60//______________________________________________________________________
cfaccd71 61UInt_t AliITSPreprocessorSDD::Process(TMap* dcsAliasMap){
e56160b8 62
7d21c0a9 63
979b5a5f 64 // Get DDL map from OCDB
65 AliCDBEntry* entry = GetFromOCDB("Calib", "DDLMapSDD");
66 if(!entry){
67 Log("DDL map file not found in OCDB.");
68 return 2;
69 }
70 AliITSDDLModuleMapSDD* ddlmap = (AliITSDDLModuleMapSDD*)entry->GetObject();
71 if(!ddlmap){
72 Log("AliITSDDLModuleMapSDD object not in file.");
73 return 2;
74 }
b478db1a 75 ddlmap->PrintDDLMap();
e56160b8 76
979b5a5f 77 //preprocessing
18da6e54 78 TString runType = GetRunType();
461c9263 79 Int_t retcode=0;
c2bd28b6 80
ad2585ed 81 if (runType == "PULSER"){
b478db1a 82 Log("Process FXS files from PULSER RUN");
317e638f 83 retcode=ProcessPulser(ddlmap);
84 }else if(runType== "INJECTOR"){
b478db1a 85 Log("Process FXS files from INJECTOR RUN");
317e638f 86 retcode=ProcessInjector(ddlmap);
317e638f 87 }
461c9263 88 if(retcode!=0) return retcode;
89
b478db1a 90 Log("Process DCS data");
461c9263 91 Bool_t retcodedcs =ProcessDCSDataPoints(dcsAliasMap);
92 if(retcodedcs) return 0;
93 else return 1;
94
317e638f 95}
96//______________________________________________________________________
461c9263 97UInt_t AliITSPreprocessorSDD::ProcessPulser(AliITSDDLModuleMapSDD* ddlmap){
317e638f 98 // Process FXS files from PULSER run (baseline, noise, gain)
461c9263 99 // returns 0 in case of success,
100 // 1 in case of storage error,
101 // 2 in case of error with input files
317e638f 102 TObjArray calSDD(kNumberOfSDD);
103 calSDD.SetOwner(kFALSE);
7d21c0a9 104
317e638f 105 Float_t baseline,rawnoise,cmn,corn,gain;
106 Int_t isgoodan,i,im,is,isgoodmod,basmin,basoff;
e7610d19 107 Int_t th,tl;
317e638f 108 Int_t numOfBadChannels[kNumberOfSDD];
18da6e54 109
317e638f 110 TList* sourceList = GetFileSources(kDAQ, "SDD_Calib");
111 if (!sourceList){
112 Log("Error: no sources found for SDD_Calib");
113 return 2;
114 }
dfb8947f 115
317e638f 116 Int_t ind = 0;
117 while (sourceList->At(ind)!=NULL) {
118 TObjString* tarId = (TObjString*) sourceList->At(ind);
119 TString tarName = GetFile(kDAQ, "SDD_Calib", tarId->GetString().Data());
120 if(tarName.Length()==0){
121 Log(Form("Baseline tar file from source %d not found.",ind));
122 return 2;
18da6e54 123 }
7d21c0a9 124 TString command;
125 command.Form("tar -xf %s",tarName.Data());
317e638f 126 gSystem->Exec(command);
127 ind++;
128 }
129 delete sourceList;
27416b0d 130 // Read ADC sampling frequency from fee.conf
131 Int_t amSamplFreq=40;
296ee458 132 Int_t retfscf;
27416b0d 133 FILE* feefil=fopen("fee.conf","r");
134 if(feefil){
296ee458 135 retfscf=fscanf(feefil,"%d \n",&amSamplFreq);
27416b0d 136 fclose(feefil);
14e709e6 137 if(retfscf<0){
138 Log("Error reading from fee.conf. AM sampling set at 40 MHz by default");
139 }else{
140 Log(Form("AM sampling frequency = %d MHz",amSamplFreq));
141 }
27416b0d 142 }else{
143 Log("File fee.conf not found. AM sampling set at 40 MHz by default");
144 }
317e638f 145
146 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
147 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
148 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
149 if(modID==-1) continue;
150 modID-=240; // to have SDD modules numbering from 0 to 260
151 AliITSCalibrationSDD *cal = new AliITSCalibrationSDD("simulated");
27416b0d 152 if(amSamplFreq!=40) cal->SetAMAt20MHz();
317e638f 153 numOfBadChannels[modID]=0;
154 Int_t badch[kNumberOfChannels];
296ee458 155 Bool_t sid0ok=kTRUE;
156 Bool_t sid1ok=kTRUE;
317e638f 157 for(Int_t isid=0;isid<=1;isid++){
7d21c0a9 158 TString inpFileName;
159 inpFileName.Form("./SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
160
317e638f 161 FILE* basFil = fopen(inpFileName,"read");
162 if (basFil == 0) {
7d21c0a9 163 Log(Form("File %s not found.",inpFileName.Data()));
296ee458 164 if(isid==0){
165 sid0ok=kFALSE;
166 for(Int_t iChip=0; iChip<4; iChip++) cal->SetChipBad(iChip);
167 cal->SetDeadChannels(cal->GetDeadChannels()+256);
168 for(Int_t iAnode=0; iAnode<256; iAnode++) cal->SetBadChannel(iAnode,iAnode);
169 }else{
170 sid1ok=kFALSE;
171 for(Int_t iChip=4; iChip<8; iChip++) cal->SetChipBad(iChip);
172 cal->SetDeadChannels(cal->GetDeadChannels()+256);
173 for(Int_t iAnode=0; iAnode<256; iAnode++) cal->SetBadChannel(iAnode,iAnode+256);
174 }
317e638f 175 continue;
176 }
296ee458 177
178 retfscf=fscanf(basFil,"%d %d %d\n",&im,&is,&isgoodmod);
14e709e6 179 if(retfscf<0) isgoodmod=kFALSE;
296ee458 180 if(!isgoodmod){
181 if(isid==0){
182 sid0ok=kFALSE;
183 for(Int_t iChip=0; iChip<4; iChip++) cal->SetChipBad(iChip);
184 }else{
185 sid1ok=kFALSE;
186 for(Int_t iChip=4; iChip<8; iChip++) cal->SetChipBad(iChip);
187 }
188 }
189 retfscf=fscanf(basFil,"%d\n",&th);
190 retfscf=fscanf(basFil,"%d\n",&tl);
20f3f947 191 cal->SetZSLowThreshold(isid,tl);
192 cal->SetZSHighThreshold(isid,th);
317e638f 193 for(Int_t ian=0;ian<(kNumberOfChannels/2);ian++){
296ee458 194 retfscf=fscanf(basFil,"%d %d %f %d %d %f %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn,&gain);
317e638f 195 Int_t ich=ian;
196 if(isid==1) ich+=256;
197 if(!isgoodan){
198 Int_t ibad=numOfBadChannels[modID];
199 badch[ibad]=ich;
200 numOfBadChannels[modID]++;
979b5a5f 201 }
20f3f947 202 cal->SetBaseline(ich,baseline-basoff);
317e638f 203 cal->SetNoiseAfterElectronics(ich,rawnoise);
204 cal->SetGain(ich,gain);
979b5a5f 205 }
317e638f 206 cal->SetDeadChannels(numOfBadChannels[modID]);
207 for(Int_t ibad=0;ibad<numOfBadChannels[modID];ibad++){
208 cal->SetBadChannel(ibad,badch[ibad]);
209 }
210 fclose(basFil);
c2bd28b6 211 }
296ee458 212 if(!sid0ok && !sid1ok) cal->SetBad();
b478db1a 213 Log(Form("Put calib obj for module %d (DDL %d Carlos %d)",modID,iddl,imod));
317e638f 214 calSDD.AddAt(cal,modID);
e56160b8 215 }
317e638f 216 }
217 AliCDBMetaData *md= new AliCDBMetaData();
218 md->SetResponsible("Francesco Prino");
219 md->SetBeamPeriod(0);
220 md->SetComment("AliITSCalibrationSDD from PEDESTAL+PULSER runs");
221 Bool_t retCode = Store("Calib","CalibSDD",&calSDD,md, 0, kTRUE);
461c9263 222 if(retCode) return 0;
223 else return 1;
317e638f 224}
225//______________________________________________________________________
461c9263 226UInt_t AliITSPreprocessorSDD::ProcessInjector(AliITSDDLModuleMapSDD* ddlmap){
317e638f 227 // Process FXS files from injector events (INJECTOR or PHYSICS runs)
461c9263 228 // returns 0 in case of success,
229 // 1 in case of storage error,
230 // 2 in case of error with input files
317e638f 231 TObjArray vdrift(2*kNumberOfSDD);
232 vdrift.SetOwner(kFALSE);
317e638f 233 Int_t evNumb,polDeg;
50d25e98 234 UInt_t timeStamp,statusInj;
9f963593 235 Bool_t modSet[2*kNumberOfSDD]; // flag modules with good inj.
5eb2b05e 236 for(Int_t ihyb=0; ihyb<2*kNumberOfSDD; ihyb++) modSet[ihyb]=0;
50d25e98 237 Double_t nPtLay3 = 0;
238 Double_t nPtLay4 = 0;
9f963593 239
240 Double_t param[4]; // parameters of poly fit
915c0469 241 Double_t minValP0=5.; // min value for param[0]
242 Double_t maxValP0=8.; // max value for param[0]
9f963593 243 Double_t minValP1=0.; // min value for param[1]
50d25e98 244 Double_t aveCoefLay3[4]={0.,0.,0.,0.}; // average param for good mod.
245 Double_t aveCoefLay4[4]={0.,0.,0.,0.}; // average param for good mod.
9f963593 246 Double_t defCoef[4]={6.53227,0.00128941,-5.14493e-06,0}; // default values for param
247 Float_t auxP;
e56160b8 248
317e638f 249 TList* sourceList = GetFileSources(kDAQ, "SDD_Injec");
250 if (!sourceList){
251 Log("Error: no sources found for SDD_Injec");
252 return 2;
253 }
254 Int_t ind = 0;
255 while (sourceList->At(ind)!=NULL) {
256 TObjString* tarId = (TObjString*) sourceList->At(ind);
257 TString tarName = GetFile(kDAQ, "SDD_Injec", tarId->GetString().Data());
258 if(tarName.Length()==0){
259 Log(Form("Injector tar file from source %d not found.",ind));
18da6e54 260 return 2;
261 }
7d21c0a9 262 TString command;
263 command.Form("tar -xf %s",tarName.Data());
317e638f 264 gSystem->Exec(command);
265 ind++;
266 }
267 delete sourceList;
296ee458 268 Int_t retfscf;
7960eafe 269
9f963593 270
317e638f 271 for(Int_t iddl=0;iddl<kNumberOfDDL;iddl++){
272 for(Int_t imod=0;imod<kModulesPerDDL;imod++){
273 Int_t modID=ddlmap->GetModuleNumber(iddl,imod);
274 if(modID==-1) continue;
275 modID-=240; // to have SDD modules numbering from 0 to 260
276 for(Int_t isid=0;isid<=1;isid++){
277 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
7d21c0a9 278 TString inpFileName;
279 inpFileName.Form("./SDDinj_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
317e638f 280 FILE* injFil = fopen(inpFileName,"read");
281 if (injFil == 0) {
7d21c0a9 282 Log(Form("File %s not found.",inpFileName.Data()));
317e638f 283 continue;
284 }
296ee458 285 retfscf=fscanf(injFil,"%d",&polDeg);
14e709e6 286 if(retfscf<0){
287 Log(Form("File %s has bad format.",inpFileName.Data()));
288 continue;
289 }
317e638f 290 while (!feof(injFil)){
296ee458 291 retfscf=fscanf(injFil,"%d %u ",&evNumb,&timeStamp);
50d25e98 292 if(evNumb==-99){
293 statusInj=timeStamp;
294 arr->SetInjectorStatus(statusInj);
295 }else{
296 if(feof(injFil)) break;
297 for(Int_t ic=0;ic<4;ic++){
298 retfscf=fscanf(injFil,"%f ",&auxP);
299 param[ic]=auxP;
300 }
9f963593 301
7a8c5708 302 if(polDeg>=0 && polDeg<=AliITSDriftSpeedSDD::GetMaxPolDeg() &&
303 param[0]>minValP0 && param[0]<maxValP0 && param[1]>minValP1){
915c0469 304 if(polDeg==3){
305 if(modID<kNumberOfSDDLay3){
306 for(Int_t ic=0;ic<4;ic++) aveCoefLay3[ic]+=param[ic];
307 nPtLay3++;
308 }else{
309 for(Int_t ic=0;ic<4;ic++) aveCoefLay4[ic]+=param[ic];
310 nPtLay4++;
311 }
50d25e98 312 }
313 AliITSDriftSpeedSDD *dsp=new AliITSDriftSpeedSDD(evNumb,timeStamp,polDeg,param);
314 arr->AddDriftSpeed(dsp);
315 modSet[2*modID+isid]=1;
915c0469 316 }else{
317 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 318 }
9f963593 319 }
18da6e54 320 }
9ebc7917 321 fclose(injFil);
b478db1a 322 Log(Form("Put calib obj for hybrid %d (DDL %d Carlos %d)",2*modID+isid,iddl,imod));
9f963593 323 if(modSet[2*modID+isid]) vdrift.AddAt(arr,2*modID+isid);
18da6e54 324 }
325 }
cfaccd71 326 }
9f963593 327
328 // set drift speed for modules with bad injectors
329 for(Int_t ic=0;ic<4;ic++){
50d25e98 330 if(nPtLay3>0) aveCoefLay3[ic]/=nPtLay3; // mean parameters
331 else aveCoefLay3[ic]=defCoef[ic]; // default parameters
332 if(nPtLay4>0) aveCoefLay4[ic]/=nPtLay4; // mean parameters
333 else aveCoefLay4[ic]=defCoef[ic]; // default parameters
9f963593 334 }
2c4e6a6a 335 AliITSDriftSpeedSDD *avdsp3=new AliITSDriftSpeedSDD(evNumb,timeStamp,3,aveCoefLay3);
336 AliITSDriftSpeedSDD *avdsp4=new AliITSDriftSpeedSDD(evNumb,timeStamp,3,aveCoefLay4);
9f963593 337
7960eafe 338 // Check status of golden modules
339 Int_t idGoldenMod=-1, idGoldenSide=-1;
340 Int_t idGoldenModList[5]={319,319,321,243,243};
341 Int_t idGoldenSideList[5]={0,1,0,0,1};
342 AliITSDriftSpeedSDD* refSpeed=0x0;
343 for(Int_t iGold=0; iGold<5; iGold++){
344 Int_t indexG=2*(idGoldenModList[iGold]-240)+idGoldenSideList[iGold];
345 if(modSet[indexG]){
346 idGoldenMod=idGoldenModList[iGold];
347 idGoldenSide=idGoldenSideList[iGold];
348 AliITSDriftSpeedArraySDD* arrRef=(AliITSDriftSpeedArraySDD*)vdrift.At(indexG);
349 refSpeed=arrRef->GetDriftSpeedObject(0);
350 break;
351 }
352 }
353 TList* correctionList=0x0;
354 if(idGoldenMod>=240 && idGoldenSide>=0){
355 // Get rescaling corrections from OCDB
356 AliCDBEntry* entry = GetFromOCDB("Calib", "RescaleDriftSpeedSDD");
357 if(!entry){
358 Log("RescaleDriftSpeedSDD file not found in OCDB.");
359 }
360 TList* fullList=(TList*)entry->GetObject();
361 if(!fullList){
362 Log("TList object not found in file.");
363 }
364 TString listName=Form("RefMod%d_Side%d",idGoldenMod,idGoldenSide);
365 correctionList=(TList*)fullList->FindObject(listName.Data());
366 if(!correctionList){
367 Log(Form("TList for requested module %d side %d not found",idGoldenMod,idGoldenSide));
368 }else{
369 Log(Form("Use module %d side %d as reference module",idGoldenMod,idGoldenSide));
370 if(refSpeed){
371 Log(Form("Drift speed params for golden module = %g %g %g %g",refSpeed->GetDriftSpeedParameter(0),refSpeed->GetDriftSpeedParameter(1),refSpeed->GetDriftSpeedParameter(2),refSpeed->GetDriftSpeedParameter(3)));
372 }else{
373 AliError("No drift speed object for golden module");
374 }
375 }
376 }
377
50d25e98 378 for(Int_t ihyb=0; ihyb<2*kNumberOfSDDLay3; ihyb++){
7960eafe 379 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
9f963593 380 if(modSet[ihyb]==0){
7960eafe 381 Int_t iBadMod=ihyb/2+240;
382 Int_t iBadSide=ihyb%2;
383 Bool_t goldenUsed=kFALSE;
384 if(correctionList && refSpeed){
385 Double_t *params=RescaleDriftSpeedModule(correctionList,iBadMod,iBadSide,refSpeed);
386 if(params){
387 AliWarning(Form("No good injector events for mod. %d side %d --> use rescaled values from golden module",iBadMod,iBadSide));
388 AliITSDriftSpeedSDD* dspres=new AliITSDriftSpeedSDD(0,refSpeed->GetEventTimestamp(),3,params);
389 arr->AddDriftSpeed(dspres);
390 arr->SetInjectorStatus(1);
391 goldenUsed=kTRUE;
392 }
393 }
394 if(!goldenUsed){
395 AliWarning(Form("No good injector events for mod. %d side %d --> use average values for layer 3",iBadMod,iBadSide));
396 arr->AddDriftSpeed(avdsp3);
397 arr->SetInjectorStatus(0);
398 }
9f963593 399 vdrift.AddAt(arr,ihyb);
400 }
401 }
402
50d25e98 403 for(Int_t ihyb=2*kNumberOfSDDLay3; ihyb<2*kNumberOfSDD; ihyb++){
404 if(modSet[ihyb]==0){
405 AliWarning(Form("No good injector events for mod. %d side %d --> use average values for layer 4",ihyb/2,ihyb%2));
406 AliITSDriftSpeedArraySDD *arr=new AliITSDriftSpeedArraySDD();
407 arr->AddDriftSpeed(avdsp4);
408 arr->SetInjectorStatus(0);
409 vdrift.AddAt(arr,ihyb);
410 }
411 }
9f963593 412
317e638f 413 AliCDBMetaData *md= new AliCDBMetaData();
414 md->SetResponsible("Francesco Prino");
415 md->SetBeamPeriod(0);
416 md->SetComment("AliITSDriftSpeedSDD from injector events");
417 Bool_t retCode = Store("Calib","DriftSpeedSDD",&vdrift,md,0, kTRUE);
461c9263 418 if(retCode) return 0;
419 else return 1;
317e638f 420}
7960eafe 421
422//______________________________________________________________________
27199d86 423Double_t* AliITSPreprocessorSDD::RescaleDriftSpeedModule(const TList* theList,
7960eafe 424 Int_t iBadMod,
425 Int_t iBadSide,
b34fed4e 426 const AliITSDriftSpeedSDD* refSpeed)
7960eafe 427 const
428{
429 // Rescale driftSpeed for a drift region starting from values of golden module
430
431 if(!refSpeed) return 0x0;
432 TString hisName=Form("hRatioMod%d_Side%d",iBadMod,iBadSide);
433 TH1D* h=(TH1D*)theList->FindObject(hisName.Data());
434 if(!h) return 0x0;
435
436 TF1* fpoly=new TF1("fpoly","pol3",0.,255.);
437 for(Int_t iAnode=0; iAnode<256; iAnode++){
438 Double_t vref=refSpeed->GetDriftSpeedAtAnode((Double_t)iAnode);
439 Double_t vcorr=h->GetBinContent(iAnode+1)*vref;
440 h->SetBinContent(iAnode+1,vcorr);
441 }
442 h->Fit(fpoly,"RNQ");
443 Double_t *params=new Double_t[4];
444 for(Int_t iPar=0; iPar<4; iPar++) params[iPar]=fpoly->GetParameter(iPar);
445 delete fpoly;
446 return params;
447}
448
317e638f 449//______________________________________________________________________
450Bool_t AliITSPreprocessorSDD::ProcessDCSDataPoints(TMap* dcsAliasMap){
451 // Process DCS data
452 AliITSDCSAnalyzerSDD *dcs=new AliITSDCSAnalyzerSDD();
453 dcs->AnalyzeData(dcsAliasMap);
454 TObjArray refDCS(kNumberOfSDD);
455 refDCS.SetOwner(kFALSE);
456 for(Int_t imod=0;imod<kNumberOfSDD;imod++){
457 AliITSDCSDataSDD *dcsdata=dcs->GetDCSData(imod);
458 refDCS.Add(dcsdata);
459 }
460 AliCDBMetaData *md= new AliCDBMetaData();
461 md->SetResponsible("Francesco Prino");
462 md->SetBeamPeriod(0);
463 md->SetComment("AliITSDCSDataSDD objects from DCS DB");
464 Bool_t retCode = StoreReferenceData("DCS","DataSDD",&refDCS,md);
465 return retCode;
e56160b8 466}
5eb2b05e 467