]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCcalibDB.cxx
The present commit corresponds to an important change in the way the
[u/mrichter/AliRoot.git] / TPC / AliTPCcalibDB.cxx
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 // Calibration data:
27 // 0.)  Altro mapping
28 //          Simulation      - not yet 
29 //          Reconstruction  - AliTPCclustererMI::Digits2Clusters(AliRawReader* rawReader)
30 //
31 // 1.)  pad by pad calibration -  AliTPCCalPad
32 //      
33 //      a.) fPadGainFactor
34 //          Simulation: AliTPCDigitizer::ExecFast - Multiply by gain
35 //          Reconstruction : AliTPCclustererMI::Digits2Clusters - Divide by gain  
36 //
37 //      b.) fPadNoise -
38 //          Simulation:        AliTPCDigitizer::ExecFast
39 //          Reconstruction:    AliTPCclustererMI::FindClusters(AliTPCCalROC * noiseROC)
40 //                             Noise depending cut on clusters charge (n sigma)
41 //      c.) fPedestal:
42 //          Simulation:     Not used yet - To be impleneted - Rounding to the nearest integer
43 //          Reconstruction: Used in AliTPCclustererMI::Digits2Clusters(AliRawReader* rawReader) 
44 //                          if data taken without zero suppression  
45 //                          Currently switch in  fRecoParam->GetCalcPedestal();
46 //      
47 //      d.) fPadTime0
48 //          Simulation:      applied in the AliTPC::MakeSector - adding offset
49 //          Reconstruction:  AliTPCTransform::Transform() - remove offset
50 //                           AliTPCTransform::Transform() - to be called
51 //                           in AliTPCtracker::Transform()      
52 //
53 // 
54 // 2.)  Space points transformation:
55 //
56 //      a.) General coordinate tranformation - AliTPCtransform (see $ALICE_ROOT/TPC/AliTPCtransform.cxx)
57 //          Created on fly - use the other calibration components
58 //                 Unisochronity  - (substract time0 - pad by pad)
59 //                 Drift velocity - Currently common drift velocity - functionality of AliTPCParam
60 //                 ExB effect    
61 //          Simulation     - Not used directly (the effects are applied one by one (see AliTPC::MakeSector)
62 //          Reconstruction - 
63 //                           AliTPCclustererMI::AddCluster
64 //                           AliTPCtrackerMI::Transform
65 //      b.) ExB effect calibration - 
66 //             classes (base class AliTPCExB, implementation- AliTPCExBExact.h  AliTPCExBFirst.h)
67 //             a.a) Simulation:   applied in the AliTPC::MakeSector - 
68 //                                calib->GetExB()->CorrectInverse(dxyz0,dxyz1);
69 //             a.b) Reconstruction -  
70 //                  
71 //                  in AliTPCtransform::Correct() - called calib->GetExB()->Correct(dxyz0,dxyz1)
72 //
73 //  3.)   cluster error, shape and Q parameterization
74 //
75 //
76 //
77 ///////////////////////////////////////////////////////////////////////////////
78
79 #include <iostream>
80 #include <fstream>
81
82
83 #include <AliCDBManager.h>
84 #include <AliCDBEntry.h>
85 #include <AliLog.h>
86 #include <AliMagF.h>
87
88 #include "AliTPCcalibDB.h"
89 #include "AliTPCAltroMapping.h"
90 #include "AliTPCExB.h"
91
92 #include "AliTPCCalROC.h"
93 #include "AliTPCCalPad.h"
94 #include "AliTPCSensorTempArray.h"
95 #include "AliGRPObject.h"
96 #include "AliTPCTransform.h"
97
98 class AliCDBStorage;
99 class AliTPCCalDet;
100 //
101 //
102
103 #include "TFile.h"
104 #include "TKey.h"
105
106 #include "TObjArray.h"
107 #include "TObjString.h"
108 #include "TString.h"
109 #include "AliTPCCalPad.h"
110 #include "AliTPCCalibPulser.h"
111 #include "AliTPCCalibPedestal.h"
112 #include "AliTPCCalibCE.h"
113 #include "AliTPCExBFirst.h"
114 #include "AliTPCTempMap.h"
115 #include "AliTPCCalibVdrift.h"
116
117
118
119
120 ClassImp(AliTPCcalibDB)
121
122 AliTPCcalibDB* AliTPCcalibDB::fgInstance = 0;
123 Bool_t AliTPCcalibDB::fgTerminated = kFALSE;
124 TObjArray    AliTPCcalibDB::fgExBArray;    // array of ExB corrections
125
126
127 //_ singleton implementation __________________________________________________
128 AliTPCcalibDB* AliTPCcalibDB::Instance()
129 {
130   //
131   // Singleton implementation
132   // Returns an instance of this class, it is created if neccessary
133   //
134   
135   if (fgTerminated != kFALSE)
136     return 0;
137
138   if (fgInstance == 0)
139     fgInstance = new AliTPCcalibDB();
140   
141   return fgInstance;
142 }
143
144 void AliTPCcalibDB::Terminate()
145 {
146   //
147   // Singleton implementation
148   // Deletes the instance of this class and sets the terminated flag, instances cannot be requested anymore
149   // This function can be called several times.
150   //
151   
152   fgTerminated = kTRUE;
153   
154   if (fgInstance != 0)
155   {
156     delete fgInstance;
157     fgInstance = 0;
158   }
159 }
160
161 //_____________________________________________________________________________
162 AliTPCcalibDB::AliTPCcalibDB():
163   TObject(),
164   fRun(-1),
165   fTransform(0),
166   fExB(0),
167   fPadGainFactor(0),
168   fDedxGainFactor(0),
169   fPadTime0(0),
170   fPadNoise(0),
171   fPedestals(0),
172   fTemperature(0),
173   fMapping(0),
174   fParam(0),
175   fClusterParam(0),  
176   fGRPArray(100000),            //! array of GRPs  -  per run  - JUST for calibration studies
177   fGRPMaps(100000),            //! array of GRPs  -  per run  - JUST for calibration studies
178   fGoofieArray(100000),         //! array of GOOFIE values -per run - Just for calibration studies
179   fVoltageArray(100000),
180   fTemperatureArray(100000),    //! array of temperature sensors - per run - Just for calibration studies
181   fVdriftArray(100000),                 //! array of v drift interfaces
182   fRunList(100000)              //! run list - indicates try to get the run param 
183
184 {
185   //
186   // constructor
187   //  
188   //
189   Update();    // temporary
190 }
191
192 AliTPCcalibDB::AliTPCcalibDB(const AliTPCcalibDB& ):
193   TObject(),
194   fRun(-1),
195   fTransform(0),
196   fExB(0),
197   fPadGainFactor(0),
198   fDedxGainFactor(0),
199   fPadTime0(0),
200   fPadNoise(0),
201   fPedestals(0),
202   fTemperature(0),
203   fMapping(0),
204   fParam(0),
205   fClusterParam(0),
206   fGRPArray(0),          //! array of GRPs  -  per run  - JUST for calibration studies
207   fGRPMaps(0),          //! array of GRPs  -  per run  - JUST for calibration studies
208   fGoofieArray(0),        //! array of GOOFIE values -per run - Just for calibration studies
209   fVoltageArray(0),
210   fTemperatureArray(0),   //! array of temperature sensors - per run - Just for calibration studies
211   fVdriftArray(0),         //! array of v drift interfaces
212   fRunList(0)              //! run list - indicates try to get the run param 
213 {
214   //
215   // Copy constructor invalid -- singleton implementation
216   //
217    Error("copy constructor","invalid -- singleton implementation");
218 }
219
220 AliTPCcalibDB& AliTPCcalibDB::operator= (const AliTPCcalibDB& )
221 {
222 //
223 // Singleton implementation - no assignment operator
224 //
225   Error("operator =", "assignment operator not implemented");
226   return *this;
227 }
228
229
230
231 //_____________________________________________________________________________
232 AliTPCcalibDB::~AliTPCcalibDB() 
233 {
234   //
235   // destructor
236   //
237   
238   // don't delete anything, CDB cache is active!
239   //if (fPadGainFactor) delete fPadGainFactor;
240   //if (fPadTime0) delete fPadTime0;
241   //if (fPadNoise) delete fPadNoise;
242 }
243
244
245 //_____________________________________________________________________________
246 AliCDBEntry* AliTPCcalibDB::GetCDBEntry(const char* cdbPath)
247 {
248   // 
249   // Retrieves an entry with path <cdbPath> from the CDB.
250   //
251   char chinfo[1000];
252     
253   AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, fRun); 
254   if (!entry) 
255   { 
256     sprintf(chinfo,"AliTPCcalibDB: Failed to get entry:\t%s ", cdbPath);
257     AliError(chinfo); 
258     return 0; 
259   }
260   return entry;
261 }
262
263
264 //_____________________________________________________________________________
265 void AliTPCcalibDB::SetRun(Long64_t run)
266 {
267   //
268   // Sets current run number. Calibration data is read from the corresponding file. 
269   //  
270   if (fRun == run)
271     return;  
272   fRun = run;
273   Update();
274 }
275   
276
277
278 void AliTPCcalibDB::Update(){
279   //
280   AliCDBEntry * entry=0;
281   
282   Bool_t cdbCache = AliCDBManager::Instance()->GetCacheFlag(); // save cache status
283   AliCDBManager::Instance()->SetCacheFlag(kTRUE); // activate CDB cache
284   
285   //
286   entry          = GetCDBEntry("TPC/Calib/PadGainFactor");
287   if (entry){
288     //if (fPadGainFactor) delete fPadGainFactor;
289     entry->SetOwner(kTRUE);
290     fPadGainFactor = (AliTPCCalPad*)entry->GetObject();
291   }
292   //
293   entry          = GetCDBEntry("TPC/Calib/GainFactorDedx");
294   if (entry){
295     entry->SetOwner(kTRUE);
296     fDedxGainFactor = (AliTPCCalPad*)entry->GetObject();
297   }
298   //
299   entry          = GetCDBEntry("TPC/Calib/PadTime0");
300   if (entry){
301     //if (fPadTime0) delete fPadTime0;
302     entry->SetOwner(kTRUE);
303     fPadTime0 = (AliTPCCalPad*)entry->GetObject();
304   }
305   //
306   //
307   entry          = GetCDBEntry("TPC/Calib/PadNoise");
308   if (entry){
309     //if (fPadNoise) delete fPadNoise;
310     entry->SetOwner(kTRUE);
311     fPadNoise = (AliTPCCalPad*)entry->GetObject();
312   }
313
314   entry          = GetCDBEntry("TPC/Calib/Pedestals");
315   if (entry){
316     //if (fPedestals) delete fPedestals;
317     entry->SetOwner(kTRUE);
318     fPedestals = (AliTPCCalPad*)entry->GetObject();
319   }
320
321   entry          = GetCDBEntry("TPC/Calib/Temperature");
322   if (entry){
323     //if (fTemperature) delete fTemperature;
324     entry->SetOwner(kTRUE);
325     fTemperature = (AliTPCSensorTempArray*)entry->GetObject();
326   }
327
328   entry          = GetCDBEntry("TPC/Calib/Parameters");
329   if (entry){
330     //if (fPadNoise) delete fPadNoise;
331     entry->SetOwner(kTRUE);
332     fParam = (AliTPCParam*)(entry->GetObject()->Clone());
333   }
334
335   entry          = GetCDBEntry("TPC/Calib/ClusterParam");
336   if (entry){
337     //if (fPadNoise) delete fPadNoise;
338     entry->SetOwner(kTRUE);
339     fClusterParam = (AliTPCClusterParam*)(entry->GetObject()->Clone());
340   }
341
342   entry          = GetCDBEntry("TPC/Calib/Mapping");
343   if (entry){
344     //if (fPadNoise) delete fPadNoise;
345     entry->SetOwner(kTRUE);
346     TObjArray * array = dynamic_cast<TObjArray*>(entry->GetObject());
347     if (array && array->GetEntriesFast()==6){
348       fMapping = new AliTPCAltroMapping*[6];
349       for (Int_t i=0; i<6; i++){
350         fMapping[i] =  dynamic_cast<AliTPCAltroMapping*>(array->At(i));
351       }
352     }
353   }
354
355
356
357   //entry          = GetCDBEntry("TPC/Calib/ExB");
358   //if (entry) {
359   //  entry->SetOwner(kTRUE);
360   //  fExB=dynamic_cast<AliTPCExB*>(entry->GetObject()->Clone());
361   //}
362   //
363   // ExB  - calculate during initialization 
364   //      - 
365   fExB =  GetExB(-5,kTRUE);
366      //
367   if (!fTransform) {
368     fTransform=new AliTPCTransform(); 
369   }
370
371   //
372   AliCDBManager::Instance()->SetCacheFlag(cdbCache); // reset original CDB cache
373   
374 }
375
376
377
378 void AliTPCcalibDB::CreateObjectList(const Char_t *filename, TObjArray *calibObjects)
379 {
380 //
381 // Create calibration objects and read contents from OCDB
382 //
383    if ( calibObjects == 0x0 ) return;
384    ifstream in;
385    in.open(filename);
386    if ( !in.is_open() ){
387       fprintf(stderr,"Error: cannot open list file '%s'", filename);
388       return;
389    }
390    
391    AliTPCCalPad *calPad=0x0;
392    
393    TString sFile;
394    sFile.ReadFile(in);
395    in.close();
396    
397    TObjArray *arrFileLine = sFile.Tokenize("\n");
398    
399    TIter nextLine(arrFileLine);
400    
401    TObjString *sObjLine=0x0;
402    while ( (sObjLine = (TObjString*)nextLine()) ){
403       TString sLine(sObjLine->GetString());
404       
405       TObjArray *arrNextCol = sLine.Tokenize("\t");
406       
407       TObjString *sObjType     = (TObjString*)(arrNextCol->At(0));
408       TObjString *sObjFileName = (TObjString*)(arrNextCol->At(1));
409       
410       if ( !sObjType || ! sObjFileName ) continue;
411       TString sType(sObjType->GetString());
412       TString sFileName(sObjFileName->GetString());
413       printf("%s\t%s\n",sType.Data(),sFileName.Data());
414       
415       TFile *fIn = TFile::Open(sFileName);
416       if ( !fIn ){
417          fprintf(stderr,"File not found: '%s'", sFileName.Data());
418          continue;
419       }
420       
421       if ( sType == "CE" ){
422          AliTPCCalibCE *ce = (AliTPCCalibCE*)fIn->Get("AliTPCCalibCE");
423          
424          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadT0());         
425          calPad->SetNameTitle("CETmean","CETmean");
426          calibObjects->Add(calPad);
427          
428          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadQ());         
429          calPad->SetNameTitle("CEQmean","CEQmean");
430          calibObjects->Add(calPad);        
431          
432          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadRMS());
433          calPad->SetNameTitle("CETrms","CETrms");
434          calibObjects->Add(calPad);         
435                   
436       } else if ( sType == "Pulser") {
437          AliTPCCalibPulser *sig = (AliTPCCalibPulser*)fIn->Get("AliTPCCalibPulser");
438          
439          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadT0());         
440          calPad->SetNameTitle("PulserTmean","PulserTmean");
441          calibObjects->Add(calPad);
442          
443          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadQ());         
444          calPad->SetNameTitle("PulserQmean","PulserQmean");
445          calibObjects->Add(calPad);        
446          
447          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadRMS());
448          calPad->SetNameTitle("PulserTrms","PulserTrms");
449          calibObjects->Add(calPad);         
450       
451       } else if ( sType == "Pedestals") {
452          AliTPCCalibPedestal *ped = (AliTPCCalibPedestal*)fIn->Get("AliTPCCalibPedestal");
453          
454          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadPedestal());         
455          calPad->SetNameTitle("Pedestals","Pedestals");
456          calibObjects->Add(calPad);
457          
458          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadRMS());         
459          calPad->SetNameTitle("Noise","Noise");
460          calibObjects->Add(calPad);        
461      
462       } else {
463          fprintf(stderr,"Undefined Type: '%s'",sType.Data());
464          
465       }
466       delete fIn;
467    }
468 }
469
470
471
472 void AliTPCcalibDB::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad* outlierPad, Float_t ltmFraction) {
473   //
474   // Write a tree with all available information
475   // if mapFileName is specified, the Map information are also written to the tree
476   // pads specified in outlierPad are not used for calculating statistics
477   //  - the same function as AliTPCCalPad::MakeTree - 
478   //
479    AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
480
481    TObjArray* mapIROCs = 0;
482    TObjArray* mapOROCs = 0;
483    TVectorF *mapIROCArray = 0;
484    TVectorF *mapOROCArray = 0;
485    Int_t mapEntries = 0;
486    TString* mapNames = 0;
487    
488    if (mapFileName) {
489       TFile mapFile(mapFileName, "read");
490       
491       TList* listOfROCs = mapFile.GetListOfKeys();
492       mapEntries = listOfROCs->GetEntries()/2;
493       mapIROCs = new TObjArray(mapEntries*2);
494       mapOROCs = new TObjArray(mapEntries*2);
495       mapIROCArray = new TVectorF[mapEntries];
496       mapOROCArray = new TVectorF[mapEntries];
497       
498       mapNames = new TString[mapEntries];
499       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
500         TString nameROC(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
501          nameROC.Remove(nameROC.Length()-4, 4);
502          mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "IROC").Data()), ivalue);
503          mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "OROC").Data()), ivalue);
504          mapNames[ivalue].Append(nameROC);
505       }
506       
507       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
508          mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
509          mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
510       
511          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
512             (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
513          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
514             (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
515       }
516
517    } //  if (mapFileName)
518   
519    TTreeSRedirector cstream(fileName);
520    Int_t arrayEntries = array->GetEntries();
521    
522    TString* names = new TString[arrayEntries];
523    for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
524       names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
525
526    for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
527       //
528       // get statistic for given sector
529       //
530       TVectorF median(arrayEntries);
531       TVectorF mean(arrayEntries);
532       TVectorF rms(arrayEntries);
533       TVectorF ltm(arrayEntries);
534       TVectorF ltmrms(arrayEntries);
535       TVectorF medianWithOut(arrayEntries);
536       TVectorF meanWithOut(arrayEntries);
537       TVectorF rmsWithOut(arrayEntries);
538       TVectorF ltmWithOut(arrayEntries);
539       TVectorF ltmrmsWithOut(arrayEntries);
540       
541       TVectorF *vectorArray = new TVectorF[arrayEntries];
542       for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
543          vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
544       
545       for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
546          AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
547          AliTPCCalROC* calROC = calPad->GetCalROC(isector);
548          AliTPCCalROC* outlierROC = 0;
549          if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
550          if (calROC) {
551             median[ivalue] = calROC->GetMedian();
552             mean[ivalue] = calROC->GetMean();
553             rms[ivalue] = calROC->GetRMS();
554             Double_t ltmrmsValue = 0;
555             ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
556             ltmrms[ivalue] = ltmrmsValue;
557             if (outlierROC) {
558                medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
559                meanWithOut[ivalue] = calROC->GetMean(outlierROC);
560                rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
561                ltmrmsValue = 0;
562                ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
563                ltmrmsWithOut[ivalue] = ltmrmsValue;
564             }
565          }
566          else {
567             median[ivalue] = 0.;
568             mean[ivalue] = 0.;
569             rms[ivalue] = 0.;
570             ltm[ivalue] = 0.;
571             ltmrms[ivalue] = 0.;
572             medianWithOut[ivalue] = 0.;
573             meanWithOut[ivalue] = 0.;
574             rmsWithOut[ivalue] = 0.;
575             ltmWithOut[ivalue] = 0.;
576             ltmrmsWithOut[ivalue] = 0.;
577          }
578       }
579       
580       //
581       // fill vectors of variable per pad
582       //
583       TVectorF *posArray = new TVectorF[8];
584       for (Int_t ivalue = 0; ivalue < 8; ivalue++)
585          posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
586
587       Float_t posG[3] = {0};
588       Float_t posL[3] = {0};
589       Int_t ichannel = 0;
590       for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
591          for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
592             tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
593             tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
594             posArray[0][ichannel] = irow;
595             posArray[1][ichannel] = ipad;
596             posArray[2][ichannel] = posL[0];
597             posArray[3][ichannel] = posL[1];
598             posArray[4][ichannel] = posG[0];
599             posArray[5][ichannel] = posG[1];
600             posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
601             posArray[7][ichannel] = ichannel;
602             
603             // loop over array containing AliTPCCalPads
604             for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
605                AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
606                AliTPCCalROC* calROC = calPad->GetCalROC(isector);
607                if (calROC)
608                   (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
609                else
610                   (vectorArray[ivalue])[ichannel] = 0;
611             }
612             ichannel++;
613          }
614       }
615       
616       cstream << "calPads" <<
617          "sector=" << isector;
618       
619       for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
620          cstream << "calPads" <<
621             (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
622             (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
623             (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
624             (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
625             (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
626          if (outlierPad) {
627             cstream << "calPads" <<
628                (Char_t*)((names[ivalue] + "_Median_OutlierCutted=").Data()) << medianWithOut[ivalue] <<
629                (Char_t*)((names[ivalue] + "_Mean_OutlierCutted=").Data()) << meanWithOut[ivalue] <<
630                (Char_t*)((names[ivalue] + "_RMS_OutlierCutted=").Data()) << rmsWithOut[ivalue] <<
631                (Char_t*)((names[ivalue] + "_LTM_OutlierCutted=").Data()) << ltmWithOut[ivalue] <<
632                (Char_t*)((names[ivalue] + "_RMS_LTM_OutlierCutted=").Data()) << ltmrmsWithOut[ivalue];
633          }
634       }
635
636       for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
637          cstream << "calPads" <<
638             (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
639       }
640
641       if (mapFileName) {
642          for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
643             if (isector < 36)
644                cstream << "calPads" <<
645                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
646             else
647                cstream << "calPads" <<
648                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
649          }
650       }
651
652       cstream << "calPads" <<
653          "row.=" << &posArray[0] <<
654          "pad.=" << &posArray[1] <<
655          "lx.=" << &posArray[2] <<
656          "ly.=" << &posArray[3] <<
657          "gx.=" << &posArray[4] <<
658          "gy.=" << &posArray[5] <<
659          "rpad.=" << &posArray[6] <<
660          "channel.=" << &posArray[7];
661          
662       cstream << "calPads" <<
663          "\n";
664
665       delete[] posArray;
666       delete[] vectorArray;
667    }
668    
669
670    delete[] names;
671    if (mapFileName) {
672       delete mapIROCs;
673       delete mapOROCs;
674       delete[] mapIROCArray;
675       delete[] mapOROCArray;
676       delete[] mapNames;
677    }
678 }
679
680
681
682 void AliTPCcalibDB::RegisterExB(Int_t index, Float_t bz, Bool_t bdelete){
683   //
684   // Register static ExB correction map
685   // index - registration index - used for visualization
686   // bz    - bz field in kGaus
687
688   Float_t factor =  bz/(-5.);  // default b filed in Cheb with minus sign
689   
690   AliMagF*   bmap = new AliMagF("MapsExB","MapsExB", 2,factor,1., 10.,AliMagF::k5kG,"$(ALICE_ROOT)/data/maps/mfchebKGI_sym.root");
691   
692   AliTPCExBFirst *exb  = new  AliTPCExBFirst(bmap,0.88*2.6400e+04,50,50,50);
693   AliTPCExB::SetInstance(exb);
694   
695   if (bdelete){
696     delete bmap;
697   }else{
698     AliTPCExB::RegisterField(index,bmap);
699   }
700   if (index>=fgExBArray.GetEntries()) fgExBArray.Expand((index+1)*2+11);
701   fgExBArray.AddAt(exb,index);
702 }
703
704
705 AliTPCExB*    AliTPCcalibDB::GetExB(Float_t bz, Bool_t deleteB) {
706   //
707   // bz filed in KGaus not in tesla
708   // Get ExB correction map
709   // if doesn't exist - create it
710   //
711   Int_t index = TMath::Nint(5+bz);
712   if (index>fgExBArray.GetEntries()) fgExBArray.Expand((index+1)*2+11);
713   if (!fgExBArray.At(index)) AliTPCcalibDB::RegisterExB(index,bz,deleteB);
714   return (AliTPCExB*)fgExBArray.At(index);
715 }
716
717
718 void  AliTPCcalibDB::SetExBField(Float_t bz){
719   //
720   // Set magnetic filed for ExB correction
721   //
722   fExB = GetExB(bz,kFALSE);
723 }
724
725
726
727 void AliTPCcalibDB::GetRunInformations( Int_t run){
728   //
729   // - > Don't use it for reconstruction - Only for Calibration studies
730   //
731   AliCDBEntry * entry = 0;
732   if (run>= fRunList.GetSize()){
733     fRunList.Set(run*2+1);
734     fGRPArray.Expand(run*2+1);
735     fGRPMaps.Expand(run*2+1);
736     fGoofieArray.Expand(run*2+1);
737     fVoltageArray.Expand(run*2+1); 
738     fTemperatureArray.Expand(run*2+1);
739     fVdriftArray.Expand(run*2+1);
740   }
741   if (fRunList[run]>0) return;
742   entry = AliCDBManager::Instance()->Get("GRP/GRP/Data",run);
743   if (entry)  {
744     AliGRPObject * grpRun = dynamic_cast<AliGRPObject*>(entry->GetObject());
745     if (!grpRun){
746       TMap* map = dynamic_cast<TMap*>(entry->GetObject());
747       if (map){
748         //grpRun = new AliGRPObject; 
749         //grpRun->ReadValuesFromMap(map);
750         grpRun =  MakeGRPObjectFromMap(map);
751
752         fGRPMaps.AddAt(map,run);
753       }
754     }
755     fGRPArray.AddAt(grpRun,run);
756   }
757   entry = AliCDBManager::Instance()->Get("TPC/Calib/Goofie",run);
758   if (entry)  fGoofieArray.AddAt(entry->GetObject(),run);
759   //
760   entry = AliCDBManager::Instance()->Get("TPC/Calib/HighVoltage",run);
761   if (entry)  fVoltageArray.AddAt(entry->GetObject(),run);
762   //
763   entry = AliCDBManager::Instance()->Get("TPC/Calib/Temperature",run);
764   if (entry)  fTemperatureArray.AddAt(entry->GetObject(),run);
765   fRunList[run]=1;  // sign as used
766
767   AliDCSSensor * press = GetPressureSensor(run);
768   AliTPCSensorTempArray * temp = GetTemperatureSensor(run);
769   if (press && temp){
770     AliTPCCalibVdrift * vdrift = new AliTPCCalibVdrift(temp, press,0);
771     fVdriftArray.AddAt(vdrift,run);
772   }
773 }
774
775
776 Float_t AliTPCcalibDB::GetGain(Int_t sector, Int_t row, Int_t pad){
777   //
778   //
779   AliTPCCalPad *calPad = Instance()->fDedxGainFactor;;
780   if (!calPad) return 0;
781   return calPad->GetCalROC(sector)->GetValue(row,pad);
782 }
783
784
785 AliGRPObject *AliTPCcalibDB::GetGRP(Int_t run){
786   //
787   // Get GRP object for given run 
788   //
789   AliGRPObject * grpRun = dynamic_cast<AliGRPObject *>((Instance()->fGRPArray).At(run));
790   if (!grpRun) {
791     Instance()->GetRunInformations(run);
792     grpRun = dynamic_cast<AliGRPObject *>(Instance()->fGRPArray.At(run));
793     if (!grpRun) return 0; 
794   }
795   return grpRun;
796 }
797
798 TMap *  AliTPCcalibDB::GetGRPMap(Int_t run){
799   //
800   //
801   //
802   TMap * grpRun = dynamic_cast<TMap *>((Instance()->fGRPMaps).At(run));
803   if (!grpRun) {
804     Instance()->GetRunInformations(run);
805     grpRun = dynamic_cast<TMap *>(Instance()->fGRPMaps.At(run));
806     if (!grpRun) return 0; 
807   }
808   return grpRun;
809 }
810
811
812 AliDCSSensor * AliTPCcalibDB::GetPressureSensor(Int_t run, Int_t type){
813   //
814   // Get Pressure sensor
815   //
816   //
817   // First try to get if trom map - if existing  (Old format of data storing)
818   //
819   TMap *map = GetGRPMap(run);  
820   if (map){
821     AliDCSSensor * sensor = 0;
822     TObject *osensor=0;
823     if (type==0) osensor = ((*map)("fCavernPressure"));
824     if (type==1) osensor = ((*map)("fP2Pressure"));
825     sensor =dynamic_cast<AliDCSSensor *>(osensor); 
826     if (sensor) return sensor;
827   }
828   //
829   // If not map try to get it from the GRPObject
830   //
831   AliGRPObject * grpRun = dynamic_cast<AliGRPObject *>(fGRPArray.At(run)); 
832   if (!grpRun) {
833     GetRunInformations(run);
834     grpRun = dynamic_cast<AliGRPObject *>(fGRPArray.At(run));
835     if (!grpRun) return 0; 
836   }
837   AliDCSSensor * sensor = grpRun->GetCavernAtmosPressure();
838   if (type==1) sensor = grpRun->GetSurfaceAtmosPressure();
839   return sensor; 
840 }
841
842 AliTPCSensorTempArray * AliTPCcalibDB::GetTemperatureSensor(Int_t run){
843   //
844   // Get temperature sensor array
845   //
846   AliTPCSensorTempArray * tempArray = (AliTPCSensorTempArray *)fTemperatureArray.At(run);
847   if (!tempArray) {
848     GetRunInformations(run);
849     tempArray = (AliTPCSensorTempArray *)fTemperatureArray.At(run);
850   }
851   return tempArray;
852 }
853
854 AliDCSSensorArray * AliTPCcalibDB::GetGoofieSensors(Int_t run){
855   //
856   // Get temperature sensor array
857   //
858   AliDCSSensorArray * goofieArray = (AliDCSSensorArray *)fGoofieArray.At(run);
859   if (!goofieArray) {
860     GetRunInformations(run);
861     goofieArray = (AliDCSSensorArray *)fGoofieArray.At(run);
862   }
863   return goofieArray;
864 }
865
866 AliDCSSensorArray * AliTPCcalibDB::GetVoltageSensors(Int_t run){
867   //
868   // Get temperature sensor array
869   //
870   AliDCSSensorArray * voltageArray = (AliDCSSensorArray *)fVoltageArray.At(run);
871   if (!voltageArray) {
872     GetRunInformations(run);
873     voltageArray = (AliDCSSensorArray *)fVoltageArray.At(run);
874   }
875   return voltageArray;
876 }
877
878 AliTPCCalibVdrift *     AliTPCcalibDB::GetVdrift(Int_t run){
879   //
880   // Get the interface to the the vdrift 
881   //
882   AliTPCCalibVdrift  * vdrift = (AliTPCCalibVdrift*)fVdriftArray.At(run);
883   if (!vdrift) {
884     GetRunInformations(run);
885     vdrift= (AliTPCCalibVdrift*)fVdriftArray.At(run);
886   }
887   return vdrift;
888 }
889
890
891 Float_t AliTPCcalibDB::GetChamberHighVoltage(Int_t timeStamp, Int_t run, Int_t sector) {
892   //
893   // return the chamber HV for given run and time: 0-35 IROC, 36-72 OROC
894   //
895   TTimeStamp stamp(timeStamp);
896   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
897   if (!voltageArray) return 0;
898   AliDCSSensor *sensor = voltageArray->GetSensor((sector+1)*3);
899   if (!sensor) return 0;
900   return sensor->GetValue(stamp);
901 }
902
903 Float_t AliTPCcalibDB::GetPressure(Int_t timeStamp, Int_t run, Int_t type){
904   //
905   // GetPressure for given time stamp and runt
906   //
907   TTimeStamp stamp(timeStamp);
908   AliDCSSensor * sensor = Instance()->GetPressureSensor(run,type);
909   if (!sensor) return 0;
910   return sensor->GetValue(stamp);
911 }
912
913 Float_t AliTPCcalibDB::GetValueGoofie(Int_t timeStamp, Int_t run, Int_t type){
914   //
915   // GetPressure for given time stamp and runt
916   //
917   TTimeStamp stamp(timeStamp);
918   AliDCSSensorArray* goofieArray = AliTPCcalibDB::Instance()->GetGoofieSensors(run);
919   if (!goofieArray) return 0;
920   AliDCSSensor *sensor = goofieArray->GetSensor(type);
921   return sensor->GetValue(stamp);
922 }
923
924
925
926
927
928
929 Bool_t  AliTPCcalibDB::GetTemperatureFit(Int_t timeStamp, Int_t run, Int_t side,TVectorD& fit){
930   //
931   //
932   //
933   TTimeStamp tstamp(timeStamp);
934   AliTPCSensorTempArray* tempArray  = Instance()->GetTemperatureSensor(run);
935   if (! tempArray) return kFALSE;
936   AliTPCTempMap * tempMap = new AliTPCTempMap(tempArray);
937   TLinearFitter * fitter = tempMap->GetLinearFitter(3,side,tstamp);
938   if (fitter){
939     fitter->Eval(); 
940     fitter->GetParameters(fit);
941   }
942   delete fitter;
943   delete tempMap;
944   if (!fitter) return kFALSE;
945   return kTRUE;
946 }
947
948 Float_t AliTPCcalibDB::GetTemperature(Int_t timeStamp, Int_t run, Int_t side){
949   //
950   //
951   //
952   TVectorD vec(5);
953   if (side==0) {
954     GetTemperatureFit(timeStamp,run,0,vec);
955     return vec[0];
956   }
957   if (side==1){
958     GetTemperatureFit(timeStamp,run,0,vec);
959     return vec[0];
960   }
961   return 0;
962 }
963
964
965 Double_t AliTPCcalibDB::GetPTRelative(UInt_t timeSec, Int_t run, Int_t side){
966   //
967   // Get relative P/T 
968   // time - absolute time
969   // run  - run number
970   // side - 0 - A side   1-C side
971   AliTPCCalibVdrift * vdrift =  Instance()->GetVdrift(run);
972   if (!vdrift) return 0;
973   return vdrift->GetPTRelative(timeSec,side);
974 }
975
976
977 void AliTPCcalibDB::ProcessEnv(const char * runList){
978   //
979   // Example test function  - how to use the environment variables
980   // runList  -  ascii file with run numbers
981   // output   -  dcsTime.root file with tree 
982
983   ifstream in;
984   in.open(runList);
985   Int_t irun=0;
986   TTreeSRedirector *pcstream = new TTreeSRedirector("dcsTime.root");
987   while(in.good()) {
988     in >> irun;
989     if (irun==0) continue;
990     printf("Processing run %d\n",irun);
991     AliDCSSensor * sensorPressure = AliTPCcalibDB::Instance()->GetPressureSensor(irun);
992     if (!sensorPressure) continue;
993     AliTPCSensorTempArray * tempArray = AliTPCcalibDB::Instance()->GetTemperatureSensor(irun);
994     AliTPCTempMap * tempMap = new AliTPCTempMap(tempArray);
995     AliDCSSensorArray* goofieArray = AliTPCcalibDB::Instance()->GetGoofieSensors(irun);
996     //
997     Int_t startTime = sensorPressure->GetStartTime();
998     Int_t endTime = sensorPressure->GetEndTime();
999     Int_t dtime = TMath::Max((endTime-startTime)/20,10*60);
1000     for (Int_t itime=startTime; itime<endTime; itime+=dtime){
1001       //
1002       TTimeStamp tstamp(itime);
1003       Float_t valuePressure = sensorPressure->GetValue(tstamp);
1004
1005       TLinearFitter * fitter = 0;
1006       TVectorD vecTemp[10];
1007       if (itime<tempArray->GetStartTime().GetSec() || itime>tempArray->GetEndTime().GetSec()){  
1008       }else{
1009         for (Int_t itype=0; itype<5; itype++)
1010           for (Int_t iside=0; iside<2; iside++){
1011             fitter= tempMap->GetLinearFitter(itype,iside,tstamp);
1012             if (!fitter) continue;
1013             fitter->Eval(); fitter->GetParameters(vecTemp[itype+iside*5]);
1014             delete fitter;
1015           }
1016       }
1017       
1018       TVectorD vecGoofie, vecEntries, vecMean, vecMedian,vecRMS;
1019       if (goofieArray){ 
1020         vecGoofie.ResizeTo(goofieArray->NumSensors());
1021         ProcessGoofie(goofieArray, vecEntries ,vecMedian, vecMean, vecRMS);
1022   //
1023         for (Int_t isensor=0; isensor<goofieArray->NumSensors();isensor++){
1024           AliDCSSensor *gsensor = goofieArray->GetSensor(isensor);
1025           if (gsensor){
1026             vecGoofie[isensor] = gsensor->GetValue(tstamp);
1027           }
1028         }
1029       }
1030
1031
1032       //tempMap->GetLinearFitter(0,0,itime);
1033       (*pcstream)<<"dcs"<<
1034         "run="<<irun<<
1035         "time="<<itime<<
1036         "goofie.="<<&vecGoofie<<
1037         "goofieE.="<<&vecEntries<<
1038         "goofieMean.="<<&vecMean<<
1039         "goofieMedian.="<<&vecMedian<<
1040         "goofieRMS.="<<&vecRMS<<
1041         "press="<<valuePressure<<
1042         "temp00.="<<&vecTemp[0]<<
1043         "temp10.="<<&vecTemp[1]<<
1044         "temp20.="<<&vecTemp[2]<<
1045         "temp30.="<<&vecTemp[3]<<
1046         "temp40.="<<&vecTemp[4]<<
1047         "temp01.="<<&vecTemp[5]<<
1048         "temp11.="<<&vecTemp[6]<<
1049         "temp21.="<<&vecTemp[7]<<
1050         "temp31.="<<&vecTemp[8]<<
1051         "temp41.="<<&vecTemp[9]<<
1052         "\n";
1053     }
1054   }
1055   delete pcstream;
1056 }
1057
1058
1059 void AliTPCcalibDB::ProcessGoofie( AliDCSSensorArray* goofieArray, TVectorD & vecEntries, TVectorD & vecMedian, TVectorD &vecMean, TVectorD &vecRMS){
1060   /*
1061     
1062   1       TPC_ANODE_I_A00_STAT
1063   2       TPC_DVM_CO2
1064   3       TPC_DVM_DriftVelocity
1065   4       TPC_DVM_FCageHV
1066   5       TPC_DVM_GainFar
1067   6       TPC_DVM_GainNear
1068   7       TPC_DVM_N2
1069   8       TPC_DVM_NumberOfSparks
1070   9       TPC_DVM_PeakAreaFar
1071   10      TPC_DVM_PeakAreaNear
1072   11      TPC_DVM_PeakPosFar
1073   12      TPC_DVM_PeakPosNear
1074   13      TPC_DVM_PickupHV
1075   14      TPC_DVM_Pressure
1076   15      TPC_DVM_T1_Over_P
1077   16      TPC_DVM_T2_Over_P
1078   17      TPC_DVM_T_Over_P
1079   18      TPC_DVM_TemperatureS1
1080    */
1081   //
1082   //
1083   //  TVectorD  vecMedian; TVectorD  vecEntries; TVectorD  vecMean; TVectorD  vecRMS;
1084   Double_t kEpsilon=0.0000000001;
1085   Double_t kBig=100000000000.;
1086   Int_t nsensors = goofieArray->NumSensors();
1087   vecEntries.ResizeTo(nsensors);
1088   vecMedian.ResizeTo(nsensors);
1089   vecMean.ResizeTo(nsensors);
1090   vecRMS.ResizeTo(nsensors);
1091   TVectorF values;
1092   for (Int_t isensor=0; isensor<goofieArray->NumSensors();isensor++){
1093     AliDCSSensor *gsensor = goofieArray->GetSensor(isensor);
1094     if (gsensor &&  gsensor->GetGraph()){
1095       Int_t npoints = gsensor->GetGraph()->GetN();
1096       // filter zeroes
1097       values.ResizeTo(npoints);
1098       Int_t nused =0;
1099       for (Int_t ipoint=0; ipoint<npoints; ipoint++){
1100         if (TMath::Abs(gsensor->GetGraph()->GetY()[ipoint])>kEpsilon && 
1101            TMath::Abs(gsensor->GetGraph()->GetY()[ipoint])<kBig ){
1102           values[nused]=gsensor->GetGraph()->GetY()[ipoint];
1103           nused++;
1104         }
1105       }
1106       //
1107       vecEntries[isensor]= nused;      
1108       if (nused>1){
1109         vecMedian[isensor] = TMath::Median(nused,values.GetMatrixArray());
1110         vecMean[isensor]   = TMath::Mean(nused,values.GetMatrixArray());
1111         vecRMS[isensor]    = TMath::RMS(nused,values.GetMatrixArray());
1112       }
1113     }
1114   }
1115 }
1116
1117
1118
1119 AliGRPObject * AliTPCcalibDB::MakeGRPObjectFromMap(TMap *map){
1120   //
1121   // Function to covert old GRP run information from TMap to GRPObject
1122   //
1123   //  TMap * map = AliTPCcalibDB::GetGRPMap(52406);
1124   if (!map) return 0;
1125   AliDCSSensor * sensor = 0;
1126   TObject *osensor=0;
1127   osensor = ((*map)("fP2Pressure"));
1128   sensor  =dynamic_cast<AliDCSSensor *>(osensor); 
1129   //
1130   if (!sensor) return 0;
1131   //
1132   AliDCSSensor * sensor2 = new AliDCSSensor(*sensor);
1133   osensor = ((*map)("fCavernPressure"));
1134   TGraph * gr = new TGraph(2);
1135   gr->GetX()[0]= -100000.;
1136   gr->GetX()[1]= 1000000.;
1137   gr->GetY()[0]= atof(osensor->GetName());
1138   gr->GetY()[1]= atof(osensor->GetName());
1139   sensor2->SetGraph(gr);
1140   sensor2->SetFit(0);
1141   
1142
1143   AliGRPObject *grpRun = new AliGRPObject; 
1144   grpRun->ReadValuesFromMap(map);
1145   grpRun->SetCavernAtmosPressure(sensor2);
1146   grpRun->SetSurfaceAtmosPressure(sensor);
1147   return grpRun;
1148 }
1149
1150
1151