Remove Obsolete PRFwidth (Marian)
[u/mrichter/AliRoot.git] / TPC / AliTPCcalibDB.cxx
CommitLineData
c5bbaa2c 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
17///////////////////////////////////////////////////////////////////////////////
18// //
19// Class providing the calibration parameters by accessing the CDB //
20// //
21// Request an instance with AliTPCcalibDB::Instance() //
22// If a new event is processed set the event number with SetRun //
23// Then request the calibration data //
24// //
25///////////////////////////////////////////////////////////////////////////////
26
418bbcaf 27#include <iostream>
28#include <fstream>
29
c5bbaa2c 30
31#include <AliCDBManager.h>
c5bbaa2c 32#include <AliCDBEntry.h>
33#include <AliLog.h>
34
35#include "AliTPCcalibDB.h"
418bbcaf 36#include "AliTPCExB.h"
c5bbaa2c 37
38#include "AliTPCCalROC.h"
39#include "AliTPCCalPad.h"
54472e4f 40#include "AliTPCSensorTempArray.h"
418bbcaf 41#include "AliTPCTransform.h"
42class AliCDBStorage;
43class AliTPCCalDet;
86df2b3a 44//
45//
46
86df2b3a 47#include "TFile.h"
48#include "TKey.h"
49
50#include "TObjArray.h"
51#include "TObjString.h"
52#include "TString.h"
53#include "AliTPCCalPad.h"
0fe7645c 54#include "AliTPCCalibPulser.h"
86df2b3a 55#include "AliTPCCalibPedestal.h"
56#include "AliTPCCalibCE.h"
57
58
59
60
c5bbaa2c 61
62ClassImp(AliTPCcalibDB)
63
64AliTPCcalibDB* AliTPCcalibDB::fgInstance = 0;
65Bool_t AliTPCcalibDB::fgTerminated = kFALSE;
66
67
68//_ singleton implementation __________________________________________________
69AliTPCcalibDB* AliTPCcalibDB::Instance()
70{
71 //
72 // Singleton implementation
73 // Returns an instance of this class, it is created if neccessary
74 //
75
76 if (fgTerminated != kFALSE)
77 return 0;
78
79 if (fgInstance == 0)
80 fgInstance = new AliTPCcalibDB();
81
82 return fgInstance;
83}
84
85void AliTPCcalibDB::Terminate()
86{
87 //
88 // Singleton implementation
89 // Deletes the instance of this class and sets the terminated flag, instances cannot be requested anymore
90 // This function can be called several times.
91 //
92
93 fgTerminated = kTRUE;
94
95 if (fgInstance != 0)
96 {
97 delete fgInstance;
98 fgInstance = 0;
99 }
100}
101
102//_____________________________________________________________________________
e4dce695 103AliTPCcalibDB::AliTPCcalibDB():
104 fRun(-1),
418bbcaf 105 fTrafo(0),
481f877b 106 fExB(0),
e4dce695 107 fPadGainFactor(0),
108 fPadTime0(0),
e4dce695 109 fPadNoise(0),
110 fPedestals(0),
111 fTemperature(0),
418bbcaf 112 fParam(0)
c5bbaa2c 113{
114 //
115 // constructor
116 //
54472e4f 117 //
c5bbaa2c 118 Update(); // temporary
119}
120
121//_____________________________________________________________________________
122AliTPCcalibDB::~AliTPCcalibDB()
123{
124 //
125 // destructor
126 //
68751c2c 127
128 // don't delete anything, CDB cache is active!
129 //if (fPadGainFactor) delete fPadGainFactor;
130 //if (fPadTime0) delete fPadTime0;
68751c2c 131 //if (fPadNoise) delete fPadNoise;
c5bbaa2c 132}
133
134
135//_____________________________________________________________________________
136AliCDBEntry* AliTPCcalibDB::GetCDBEntry(const char* cdbPath)
137{
138 //
139 // Retrieves an entry with path <cdbPath> from the CDB.
140 //
141 char chinfo[1000];
142
68751c2c 143 AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, fRun);
c5bbaa2c 144 if (!entry)
145 {
146 sprintf(chinfo,"AliTPCcalibDB: Failed to get entry:\t%s ", cdbPath);
147 AliError(chinfo);
148 return 0;
149 }
150 return entry;
151}
152
153
154//_____________________________________________________________________________
155void AliTPCcalibDB::SetRun(Long64_t run)
156{
157 //
158 // Sets current run number. Calibration data is read from the corresponding file.
159 //
160 if (fRun == run)
161 return;
162 fRun = run;
163 Update();
164}
165
166
167
168void AliTPCcalibDB::Update(){
169 //
170 AliCDBEntry * entry=0;
68751c2c 171
172 Bool_t cdbCache = AliCDBManager::Instance()->GetCacheFlag(); // save cache status
173 AliCDBManager::Instance()->SetCacheFlag(kTRUE); // activate CDB cache
174
c5bbaa2c 175 //
176 entry = GetCDBEntry("TPC/Calib/PadGainFactor");
177 if (entry){
68751c2c 178 //if (fPadGainFactor) delete fPadGainFactor;
c5bbaa2c 179 entry->SetOwner(kTRUE);
180 fPadGainFactor = (AliTPCCalPad*)entry->GetObject();
181 }
182 //
183 entry = GetCDBEntry("TPC/Calib/PadTime0");
184 if (entry){
68751c2c 185 //if (fPadTime0) delete fPadTime0;
c5bbaa2c 186 entry->SetOwner(kTRUE);
187 fPadTime0 = (AliTPCCalPad*)entry->GetObject();
188 }
189 //
c5bbaa2c 190 //
191 entry = GetCDBEntry("TPC/Calib/PadNoise");
192 if (entry){
68751c2c 193 //if (fPadNoise) delete fPadNoise;
c5bbaa2c 194 entry->SetOwner(kTRUE);
195 fPadNoise = (AliTPCCalPad*)entry->GetObject();
196 }
8477f500 197
198 entry = GetCDBEntry("TPC/Calib/Pedestals");
199 if (entry){
200 //if (fPedestals) delete fPedestals;
201 entry->SetOwner(kTRUE);
202 fPedestals = (AliTPCCalPad*)entry->GetObject();
203 }
204
54472e4f 205 entry = GetCDBEntry("TPC/Calib/Temperature");
206 if (entry){
207 //if (fTemperature) delete fTemperature;
208 entry->SetOwner(kTRUE);
209 fTemperature = (AliTPCSensorTempArray*)entry->GetObject();
210 }
211
8477f500 212 entry = GetCDBEntry("TPC/Calib/Parameters");
213 if (entry){
54472e4f 214 //if (fPadNoise) delete fPadNoise;
8477f500 215 entry->SetOwner(kTRUE);
a778f7e3 216 fParam = (AliTPCParam*)(entry->GetObject()->Clone());
8477f500 217 }
218
481f877b 219 entry = GetCDBEntry("TPC/Calib/ExB");
220 if (entry) {
221 entry->SetOwner(kTRUE);
222 fExB=dynamic_cast<AliTPCExB*>(entry->GetObject()->Clone());
223 }
224
225 delete fTrafo;
226 fTrafo=new AliTPCTransform();
8477f500 227
c5bbaa2c 228 //
68751c2c 229 AliCDBManager::Instance()->SetCacheFlag(cdbCache); // reset original CDB cache
230
c5bbaa2c 231}
e4dce695 232AliTPCcalibDB::AliTPCcalibDB(const AliTPCcalibDB& org)
233{
234 //
235 // Copy constructor invalid -- singleton implementation
236 //
237 Error("copy constructor","invalid -- singleton implementation");
238}
239
240AliTPCcalibDB& AliTPCcalibDB::operator= (const AliTPCcalibDB& rhs)
241{
242//
243// Singleton implementation - no assignment operator
244//
245 Error("operator =", "assignment operator not implemented");
246 return *this;
247}
248
86df2b3a 249
250
251void AliTPCcalibDB::CreateObjectList(const Char_t *filename, TObjArray *calibObjects)
252{
418bbcaf 253//
254// Create calibration objects and read contents from OCDB
255//
86df2b3a 256 if ( calibObjects == 0x0 ) return;
257 ifstream in;
258 in.open(filename);
259 if ( !in.is_open() ){
260 fprintf(stderr,"Error: cannot open list file '%s'", filename);
261 return;
262 }
263
264 AliTPCCalPad *calPad=0x0;
265
266 TString sFile;
267 sFile.ReadFile(in);
268 in.close();
269
270 TObjArray *arrFileLine = sFile.Tokenize("\n");
271
272 TIter nextLine(arrFileLine);
273
274 TObjString *sObjLine=0x0;
275 while ( sObjLine = (TObjString*)nextLine() ){
276 TString sLine(sObjLine->GetString());
277
278 TObjArray *arrNextCol = sLine.Tokenize("\t");
279
280 TObjString *sObjType = (TObjString*)(arrNextCol->At(0));
281 TObjString *sObjFileName = (TObjString*)(arrNextCol->At(1));
282
283 if ( !sObjType || ! sObjFileName ) continue;
284 TString sType(sObjType->GetString());
285 TString sFileName(sObjFileName->GetString());
286 printf("%s\t%s\n",sType.Data(),sFileName.Data());
287
288 TFile *fIn = TFile::Open(sFileName);
289 if ( !fIn ){
290 fprintf(stderr,"File not found: '%s'", sFileName.Data());
291 continue;
292 }
293
294 if ( sType == "CE" ){
295 AliTPCCalibCE *ce = (AliTPCCalibCE*)fIn->Get("AliTPCCalibCE");
296
297 calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadT0());
298 calPad->SetNameTitle("CETmean","CETmean");
299 calibObjects->Add(calPad);
300
301 calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadQ());
302 calPad->SetNameTitle("CEQmean","CEQmean");
303 calibObjects->Add(calPad);
304
305 calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadRMS());
306 calPad->SetNameTitle("CETrms","CETrms");
307 calibObjects->Add(calPad);
308
309 } else if ( sType == "Pulser") {
0fe7645c 310 AliTPCCalibPulser *sig = (AliTPCCalibPulser*)fIn->Get("AliTPCCalibPulser");
86df2b3a 311
312 calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadT0());
313 calPad->SetNameTitle("PulserTmean","PulserTmean");
314 calibObjects->Add(calPad);
315
316 calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadQ());
317 calPad->SetNameTitle("PulserQmean","PulserQmean");
318 calibObjects->Add(calPad);
319
320 calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadRMS());
321 calPad->SetNameTitle("PulserTrms","PulserTrms");
322 calibObjects->Add(calPad);
323
324 } else if ( sType == "Pedestals") {
325 AliTPCCalibPedestal *ped = (AliTPCCalibPedestal*)fIn->Get("AliTPCCalibPedestal");
326
327 calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadPedestal());
328 calPad->SetNameTitle("Pedestals","Pedestals");
329 calibObjects->Add(calPad);
330
331 calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadRMS());
332 calPad->SetNameTitle("Noise","Noise");
333 calibObjects->Add(calPad);
334
335 } else {
336 fprintf(stderr,"Undefined Type: '%s'",sType.Data());
337
338 }
339 delete fIn;
340 }
341}
342
343
344
345void AliTPCcalibDB::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad* outlierPad, Float_t ltmFraction) {
346 //
347 // Write a tree with all available information
418bbcaf 348 // if mapFileName is specified, the Map information are also written to the tree
86df2b3a 349 // pads specified in outlierPad are not used for calculating statistics
350 // - the same function as AliTPCCalPad::MakeTree -
351 //
352 AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
353
354 TObjArray* mapIROCs = 0;
355 TObjArray* mapOROCs = 0;
356 TVectorF *mapIROCArray = 0;
357 TVectorF *mapOROCArray = 0;
358 Int_t mapEntries = 0;
359 TString* mapNames = 0;
360
361 if (mapFileName) {
362 TFile mapFile(mapFileName, "read");
363
364 TList* listOfROCs = mapFile.GetListOfKeys();
365 mapEntries = listOfROCs->GetEntries()/2;
366 mapIROCs = new TObjArray(mapEntries*2);
367 mapOROCs = new TObjArray(mapEntries*2);
368 mapIROCArray = new TVectorF[mapEntries];
369 mapOROCArray = new TVectorF[mapEntries];
370
371 mapNames = new TString[mapEntries];
372 for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
418bbcaf 373 TString nameROC(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
374 nameROC.Remove(nameROC.Length()-4, 4);
375 mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "IROC").Data()), ivalue);
376 mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "OROC").Data()), ivalue);
377 mapNames[ivalue].Append(nameROC);
86df2b3a 378 }
379
380 for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
381 mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
382 mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
383
384 for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
385 (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
386 for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
387 (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
388 }
389
390 } // if (mapFileName)
391
392 TTreeSRedirector cstream(fileName);
393 Int_t arrayEntries = array->GetEntries();
394
395 TString* names = new TString[arrayEntries];
396 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
397 names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
398
399 for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
400 //
401 // get statistic for given sector
402 //
403 TVectorF median(arrayEntries);
404 TVectorF mean(arrayEntries);
405 TVectorF rms(arrayEntries);
406 TVectorF ltm(arrayEntries);
407 TVectorF ltmrms(arrayEntries);
408 TVectorF medianWithOut(arrayEntries);
409 TVectorF meanWithOut(arrayEntries);
410 TVectorF rmsWithOut(arrayEntries);
411 TVectorF ltmWithOut(arrayEntries);
412 TVectorF ltmrmsWithOut(arrayEntries);
413
414 TVectorF *vectorArray = new TVectorF[arrayEntries];
415 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
416 vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
417
418 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
419 AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
420 AliTPCCalROC* calROC = calPad->GetCalROC(isector);
421 AliTPCCalROC* outlierROC = 0;
422 if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
423 if (calROC) {
424 median[ivalue] = calROC->GetMedian();
425 mean[ivalue] = calROC->GetMean();
426 rms[ivalue] = calROC->GetRMS();
427 Double_t ltmrmsValue = 0;
428 ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
429 ltmrms[ivalue] = ltmrmsValue;
430 if (outlierROC) {
431 medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
432 meanWithOut[ivalue] = calROC->GetMean(outlierROC);
433 rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
434 ltmrmsValue = 0;
435 ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
436 ltmrmsWithOut[ivalue] = ltmrmsValue;
437 }
438 }
439 else {
440 median[ivalue] = 0.;
441 mean[ivalue] = 0.;
442 rms[ivalue] = 0.;
443 ltm[ivalue] = 0.;
444 ltmrms[ivalue] = 0.;
445 medianWithOut[ivalue] = 0.;
446 meanWithOut[ivalue] = 0.;
447 rmsWithOut[ivalue] = 0.;
448 ltmWithOut[ivalue] = 0.;
449 ltmrmsWithOut[ivalue] = 0.;
450 }
451 }
452
453 //
454 // fill vectors of variable per pad
455 //
456 TVectorF *posArray = new TVectorF[8];
457 for (Int_t ivalue = 0; ivalue < 8; ivalue++)
458 posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
459
460 Float_t posG[3] = {0};
461 Float_t posL[3] = {0};
462 Int_t ichannel = 0;
463 for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
464 for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
465 tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
466 tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
467 posArray[0][ichannel] = irow;
468 posArray[1][ichannel] = ipad;
469 posArray[2][ichannel] = posL[0];
470 posArray[3][ichannel] = posL[1];
471 posArray[4][ichannel] = posG[0];
472 posArray[5][ichannel] = posG[1];
473 posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
474 posArray[7][ichannel] = ichannel;
475
476 // loop over array containing AliTPCCalPads
477 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
478 AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
479 AliTPCCalROC* calROC = calPad->GetCalROC(isector);
480 if (calROC)
481 (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
482 else
483 (vectorArray[ivalue])[ichannel] = 0;
484 }
485 ichannel++;
486 }
487 }
488
489 cstream << "calPads" <<
490 "sector=" << isector;
491
492 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
493 cstream << "calPads" <<
494 (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
495 (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
496 (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
497 (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
498 (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
499 if (outlierPad) {
500 cstream << "calPads" <<
501 (Char_t*)((names[ivalue] + "_Median_OutlierCutted=").Data()) << medianWithOut[ivalue] <<
502 (Char_t*)((names[ivalue] + "_Mean_OutlierCutted=").Data()) << meanWithOut[ivalue] <<
503 (Char_t*)((names[ivalue] + "_RMS_OutlierCutted=").Data()) << rmsWithOut[ivalue] <<
504 (Char_t*)((names[ivalue] + "_LTM_OutlierCutted=").Data()) << ltmWithOut[ivalue] <<
505 (Char_t*)((names[ivalue] + "_RMS_LTM_OutlierCutted=").Data()) << ltmrmsWithOut[ivalue];
506 }
507 }
508
509 for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
510 cstream << "calPads" <<
511 (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
512 }
513
514 if (mapFileName) {
515 for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
516 if (isector < 36)
517 cstream << "calPads" <<
518 (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
519 else
520 cstream << "calPads" <<
521 (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
522 }
523 }
524
525 cstream << "calPads" <<
526 "row.=" << &posArray[0] <<
527 "pad.=" << &posArray[1] <<
528 "lx.=" << &posArray[2] <<
529 "ly.=" << &posArray[3] <<
530 "gx.=" << &posArray[4] <<
531 "gy.=" << &posArray[5] <<
532 "rpad.=" << &posArray[6] <<
533 "channel.=" << &posArray[7];
534
535 cstream << "calPads" <<
536 "\n";
537
538 delete[] posArray;
539 delete[] vectorArray;
540 }
541
542
543 delete[] names;
544 if (mapFileName) {
545 delete mapIROCs;
546 delete mapOROCs;
547 delete[] mapIROCArray;
548 delete[] mapOROCArray;
549 delete[] mapNames;
550 }
551}
552