]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCcalibDB.cxx
Adding fucntions to get timpe dependent Time0, Drift and GY correction
[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 #include <AliSplineFit.h>
88
89 #include "AliTPCcalibDB.h"
90 #include "AliTPCAltroMapping.h"
91 #include "AliTPCExB.h"
92
93 #include "AliTPCCalROC.h"
94 #include "AliTPCCalPad.h"
95 #include "AliTPCSensorTempArray.h"
96 #include "AliGRPObject.h"
97 #include "AliTPCTransform.h"
98
99 class AliCDBStorage;
100 class AliTPCCalDet;
101 //
102 //
103
104 #include "TFile.h"
105 #include "TKey.h"
106
107 #include "TObjArray.h"
108 #include "TObjString.h"
109 #include "TString.h"
110 #include "TDirectory.h"
111 #include "AliTPCCalPad.h"
112 #include "AliTPCCalibPulser.h"
113 #include "AliTPCCalibPedestal.h"
114 #include "AliTPCCalibCE.h"
115 #include "AliTPCExBFirst.h"
116 #include "AliTPCTempMap.h"
117 #include "AliTPCCalibVdrift.h"
118 #include "AliTPCCalibRaw.h"
119
120 #include "AliTPCPreprocessorOnline.h"
121
122
123 ClassImp(AliTPCcalibDB)
124
125 AliTPCcalibDB* AliTPCcalibDB::fgInstance = 0;
126 Bool_t AliTPCcalibDB::fgTerminated = kFALSE;
127 TObjArray    AliTPCcalibDB::fgExBArray;    // array of ExB corrections
128
129
130 //_ singleton implementation __________________________________________________
131 AliTPCcalibDB* AliTPCcalibDB::Instance()
132 {
133   //
134   // Singleton implementation
135   // Returns an instance of this class, it is created if neccessary
136   //
137   
138   if (fgTerminated != kFALSE)
139     return 0;
140
141   if (fgInstance == 0)
142     fgInstance = new AliTPCcalibDB();
143   
144   return fgInstance;
145 }
146
147 void AliTPCcalibDB::Terminate()
148 {
149   //
150   // Singleton implementation
151   // Deletes the instance of this class and sets the terminated flag, instances cannot be requested anymore
152   // This function can be called several times.
153   //
154   
155   fgTerminated = kTRUE;
156   
157   if (fgInstance != 0)
158   {
159     delete fgInstance;
160     fgInstance = 0;
161   }
162 }
163
164 //_____________________________________________________________________________
165 AliTPCcalibDB::AliTPCcalibDB():
166   TObject(),
167   fRun(-1),
168   fTransform(0),
169   fExB(0),
170   fPadGainFactor(0),
171   fDedxGainFactor(0),
172   fPadTime0(0),
173   fPadNoise(0),
174   fPedestals(0),
175   fCalibRaw(0),
176   fALTROConfigData(0),
177   fPulserData(0),
178   fCEData(0),
179   fTemperature(0),
180   fMapping(0),
181   fParam(0),
182   fClusterParam(0),
183   fTimeGainSplines(0),
184   fTimeGainSplinesArray(100000),
185   fGRPArray(100000),            //! array of GRPs  -  per run  - JUST for calibration studies
186   fGRPMaps(100000),            //! array of GRPs  -  per run  - JUST for calibration studies
187   fGoofieArray(100000),         //! array of GOOFIE values -per run - Just for calibration studies
188   fVoltageArray(100000),
189   fTemperatureArray(100000),    //! array of temperature sensors - per run - Just for calibration studies
190   fVdriftArray(100000),                 //! array of v drift interfaces
191   fDriftCorrectionArray(100000),  //! array of drift correction
192   fRunList(100000)              //! run list - indicates try to get the run param 
193
194 {
195   //
196   // constructor
197   //  
198   //
199   Update();    // temporary
200 }
201
202 AliTPCcalibDB::AliTPCcalibDB(const AliTPCcalibDB& ):
203   TObject(),
204   fRun(-1),
205   fTransform(0),
206   fExB(0),
207   fPadGainFactor(0),
208   fDedxGainFactor(0),
209   fPadTime0(0),
210   fPadNoise(0),
211   fPedestals(0),
212   fCalibRaw(0),
213   fALTROConfigData(0),
214   fPulserData(0),
215   fCEData(0),
216   fTemperature(0),
217   fMapping(0),
218   fParam(0),
219   fClusterParam(0),
220   fTimeGainSplines(0),
221   fTimeGainSplinesArray(100000),
222   fGRPArray(0),          //! array of GRPs  -  per run  - JUST for calibration studies
223   fGRPMaps(0),          //! array of GRPs  -  per run  - JUST for calibration studies
224   fGoofieArray(0),        //! array of GOOFIE values -per run - Just for calibration studies
225   fVoltageArray(0),
226   fTemperatureArray(0),   //! array of temperature sensors - per run - Just for calibration studies
227   fVdriftArray(0),         //! array of v drift interfaces
228   fDriftCorrectionArray(0),         //! array of v drift interfaces
229   fRunList(0)              //! run list - indicates try to get the run param 
230 {
231   //
232   // Copy constructor invalid -- singleton implementation
233   //
234    Error("copy constructor","invalid -- singleton implementation");
235 }
236
237 AliTPCcalibDB& AliTPCcalibDB::operator= (const AliTPCcalibDB& )
238 {
239 //
240 // Singleton implementation - no assignment operator
241 //
242   Error("operator =", "assignment operator not implemented");
243   return *this;
244 }
245
246
247
248 //_____________________________________________________________________________
249 AliTPCcalibDB::~AliTPCcalibDB() 
250 {
251   //
252   // destructor
253   //
254   
255   // don't delete anything, CDB cache is active!
256   //if (fPadGainFactor) delete fPadGainFactor;
257   //if (fPadTime0) delete fPadTime0;
258   //if (fPadNoise) delete fPadNoise;
259 }
260
261
262 //_____________________________________________________________________________
263 AliCDBEntry* AliTPCcalibDB::GetCDBEntry(const char* cdbPath)
264 {
265   // 
266   // Retrieves an entry with path <cdbPath> from the CDB.
267   //
268   char chinfo[1000];
269     
270   AliCDBEntry* entry = AliCDBManager::Instance()->Get(cdbPath, fRun); 
271   if (!entry) 
272   { 
273     sprintf(chinfo,"AliTPCcalibDB: Failed to get entry:\t%s ", cdbPath);
274     AliError(chinfo); 
275     return 0; 
276   }
277   return entry;
278 }
279
280
281 //_____________________________________________________________________________
282 void AliTPCcalibDB::SetRun(Long64_t run)
283 {
284   //
285   // Sets current run number. Calibration data is read from the corresponding file. 
286   //  
287   if (fRun == run)
288     return;  
289         fRun = run;
290   Update();
291 }
292   
293
294
295 void AliTPCcalibDB::Update(){
296         //
297         AliCDBEntry * entry=0;
298   
299   Bool_t cdbCache = AliCDBManager::Instance()->GetCacheFlag(); // save cache status
300   AliCDBManager::Instance()->SetCacheFlag(kTRUE); // activate CDB cache
301   
302   //
303   entry          = GetCDBEntry("TPC/Calib/PadGainFactor");
304   if (entry){
305     //if (fPadGainFactor) delete fPadGainFactor;
306     entry->SetOwner(kTRUE);
307     fPadGainFactor = (AliTPCCalPad*)entry->GetObject();
308   }
309   //
310   entry          = GetCDBEntry("TPC/Calib/TimeGain");
311   if (entry){
312     //if (fTimeGainSplines) delete fTimeGainSplines;
313     entry->SetOwner(kTRUE);
314     fTimeGainSplines = (TObjArray*)entry->GetObject();
315   }
316   //
317   entry          = GetCDBEntry("TPC/Calib/GainFactorDedx");
318   if (entry){
319     entry->SetOwner(kTRUE);
320     fDedxGainFactor = (AliTPCCalPad*)entry->GetObject();
321   }
322   //
323   entry          = GetCDBEntry("TPC/Calib/PadTime0");
324   if (entry){
325     //if (fPadTime0) delete fPadTime0;
326     entry->SetOwner(kTRUE);
327     fPadTime0 = (AliTPCCalPad*)entry->GetObject();
328   }
329   //
330   //
331   entry          = GetCDBEntry("TPC/Calib/PadNoise");
332   if (entry){
333     //if (fPadNoise) delete fPadNoise;
334     entry->SetOwner(kTRUE);
335     fPadNoise = (AliTPCCalPad*)entry->GetObject();
336   }
337
338   entry          = GetCDBEntry("TPC/Calib/Pedestals");
339   if (entry){
340     //if (fPedestals) delete fPedestals;
341     entry->SetOwner(kTRUE);
342     fPedestals = (AliTPCCalPad*)entry->GetObject();
343   }
344
345   entry          = GetCDBEntry("TPC/Calib/Temperature");
346   if (entry){
347     //if (fTemperature) delete fTemperature;
348     entry->SetOwner(kTRUE);
349     fTemperature = (AliTPCSensorTempArray*)entry->GetObject();
350   }
351
352   entry          = GetCDBEntry("TPC/Calib/Parameters");
353   if (entry){
354     //if (fPadNoise) delete fPadNoise;
355     entry->SetOwner(kTRUE);
356     fParam = (AliTPCParam*)(entry->GetObject()->Clone());
357   }
358
359   entry          = GetCDBEntry("TPC/Calib/ClusterParam");
360   if (entry){
361     entry->SetOwner(kTRUE);
362     fClusterParam = (AliTPCClusterParam*)(entry->GetObject()->Clone());
363   }
364
365   //ALTRO configuration data
366   entry          = GetCDBEntry("TPC/Calib/AltroConfig");
367   if (entry){
368     entry->SetOwner(kTRUE);
369     fALTROConfigData=(TObjArray*)(entry->GetObject());
370   }
371   
372   //Calibration Pulser data
373   entry          = GetCDBEntry("TPC/Calib/Pulser");
374   if (entry){
375     entry->SetOwner(kTRUE);
376     fPulserData=(TObjArray*)(entry->GetObject());
377   }
378   
379   //CE data
380   entry          = GetCDBEntry("TPC/Calib/CE");
381   if (entry){
382     entry->SetOwner(kTRUE);
383     fCEData=(TObjArray*)(entry->GetObject());
384   }
385   //RAW calibration data
386   entry          = GetCDBEntry("TPC/Calib/Raw");
387   if (entry){
388     entry->SetOwner(kTRUE);
389     TObjArray *arr=(TObjArray*)(entry->GetObject());
390     if (arr) fCalibRaw=(AliTPCCalibRaw*)arr->At(0);
391   }
392   
393   entry          = GetCDBEntry("TPC/Calib/Mapping");
394   if (entry){
395     //if (fPadNoise) delete fPadNoise;
396     entry->SetOwner(kTRUE);
397     TObjArray * array = dynamic_cast<TObjArray*>(entry->GetObject());
398     if (array && array->GetEntriesFast()==6){
399       fMapping = new AliTPCAltroMapping*[6];
400       for (Int_t i=0; i<6; i++){
401         fMapping[i] =  dynamic_cast<AliTPCAltroMapping*>(array->At(i));
402       }
403     }
404   }
405
406
407
408   //entry          = GetCDBEntry("TPC/Calib/ExB");
409   //if (entry) {
410   //  entry->SetOwner(kTRUE);
411   //  fExB=dynamic_cast<AliTPCExB*>(entry->GetObject()->Clone());
412   //}
413   //
414   // ExB  - calculate during initialization - in simulation /reconstruction
415   //      - not invoked here anymore
416   //fExB =  GetExB(-5,kTRUE);
417      //
418   if (!fTransform) {
419     fTransform=new AliTPCTransform(); 
420     fTransform->SetCurrentRun(AliCDBManager::Instance()->GetRun());
421   }
422
423   //
424   AliCDBManager::Instance()->SetCacheFlag(cdbCache); // reset original CDB cache
425   
426 }
427
428
429
430 void AliTPCcalibDB::CreateObjectList(const Char_t *filename, TObjArray *calibObjects)
431 {
432 //
433 // Create calibration objects and read contents from OCDB
434 //
435    if ( calibObjects == 0x0 ) return;
436    ifstream in;
437    in.open(filename);
438    if ( !in.is_open() ){
439       fprintf(stderr,"Error: cannot open list file '%s'", filename);
440       return;
441    }
442    
443    AliTPCCalPad *calPad=0x0;
444    
445    TString sFile;
446    sFile.ReadFile(in);
447    in.close();
448    
449    TObjArray *arrFileLine = sFile.Tokenize("\n");
450    
451    TIter nextLine(arrFileLine);
452    
453    TObjString *sObjLine=0x0;
454    while ( (sObjLine = (TObjString*)nextLine()) ){
455       TString sLine(sObjLine->GetString());
456       
457       TObjArray *arrNextCol = sLine.Tokenize("\t");
458       
459       TObjString *sObjType     = (TObjString*)(arrNextCol->At(0));
460       TObjString *sObjFileName = (TObjString*)(arrNextCol->At(1));
461       
462       if ( !sObjType || ! sObjFileName ) continue;
463       TString sType(sObjType->GetString());
464       TString sFileName(sObjFileName->GetString());
465       printf("%s\t%s\n",sType.Data(),sFileName.Data());
466       
467       TFile *fIn = TFile::Open(sFileName);
468       if ( !fIn ){
469          fprintf(stderr,"File not found: '%s'", sFileName.Data());
470          continue;
471       }
472       
473       if ( sType == "CE" ){
474          AliTPCCalibCE *ce = (AliTPCCalibCE*)fIn->Get("AliTPCCalibCE");
475          
476          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadT0());         
477          calPad->SetNameTitle("CETmean","CETmean");
478          calibObjects->Add(calPad);
479          
480          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadQ());         
481          calPad->SetNameTitle("CEQmean","CEQmean");
482          calibObjects->Add(calPad);        
483          
484          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadRMS());
485          calPad->SetNameTitle("CETrms","CETrms");
486          calibObjects->Add(calPad);         
487                   
488       } else if ( sType == "Pulser") {
489          AliTPCCalibPulser *sig = (AliTPCCalibPulser*)fIn->Get("AliTPCCalibPulser");
490          
491          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadT0());         
492          calPad->SetNameTitle("PulserTmean","PulserTmean");
493          calibObjects->Add(calPad);
494          
495          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadQ());         
496          calPad->SetNameTitle("PulserQmean","PulserQmean");
497          calibObjects->Add(calPad);        
498          
499          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadRMS());
500          calPad->SetNameTitle("PulserTrms","PulserTrms");
501          calibObjects->Add(calPad);         
502       
503       } else if ( sType == "Pedestals") {
504          AliTPCCalibPedestal *ped = (AliTPCCalibPedestal*)fIn->Get("AliTPCCalibPedestal");
505          
506          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadPedestal());         
507          calPad->SetNameTitle("Pedestals","Pedestals");
508          calibObjects->Add(calPad);
509          
510          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadRMS());         
511          calPad->SetNameTitle("Noise","Noise");
512          calibObjects->Add(calPad);        
513      
514       } else {
515          fprintf(stderr,"Undefined Type: '%s'",sType.Data());
516          
517       }
518       delete fIn;
519    }
520 }
521
522
523
524 void AliTPCcalibDB::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad* outlierPad, Float_t ltmFraction) {
525   //
526   // Write a tree with all available information
527   // if mapFileName is specified, the Map information are also written to the tree
528   // pads specified in outlierPad are not used for calculating statistics
529   //  - the same function as AliTPCCalPad::MakeTree - 
530   //
531    AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
532
533    TObjArray* mapIROCs = 0;
534    TObjArray* mapOROCs = 0;
535    TVectorF *mapIROCArray = 0;
536    TVectorF *mapOROCArray = 0;
537    Int_t mapEntries = 0;
538    TString* mapNames = 0;
539    
540    if (mapFileName) {
541       TFile mapFile(mapFileName, "read");
542       
543       TList* listOfROCs = mapFile.GetListOfKeys();
544       mapEntries = listOfROCs->GetEntries()/2;
545       mapIROCs = new TObjArray(mapEntries*2);
546       mapOROCs = new TObjArray(mapEntries*2);
547       mapIROCArray = new TVectorF[mapEntries];
548       mapOROCArray = new TVectorF[mapEntries];
549       
550       mapNames = new TString[mapEntries];
551       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
552         TString nameROC(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
553          nameROC.Remove(nameROC.Length()-4, 4);
554          mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "IROC").Data()), ivalue);
555          mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((nameROC + "OROC").Data()), ivalue);
556          mapNames[ivalue].Append(nameROC);
557       }
558       
559       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
560          mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
561          mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
562       
563          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
564             (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
565          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
566             (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
567       }
568
569    } //  if (mapFileName)
570   
571    TTreeSRedirector cstream(fileName);
572    Int_t arrayEntries = array->GetEntries();
573    
574    TString* names = new TString[arrayEntries];
575    for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
576       names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
577
578    for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
579       //
580       // get statistic for given sector
581       //
582       TVectorF median(arrayEntries);
583       TVectorF mean(arrayEntries);
584       TVectorF rms(arrayEntries);
585       TVectorF ltm(arrayEntries);
586       TVectorF ltmrms(arrayEntries);
587       TVectorF medianWithOut(arrayEntries);
588       TVectorF meanWithOut(arrayEntries);
589       TVectorF rmsWithOut(arrayEntries);
590       TVectorF ltmWithOut(arrayEntries);
591       TVectorF ltmrmsWithOut(arrayEntries);
592       
593       TVectorF *vectorArray = new TVectorF[arrayEntries];
594       for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
595          vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
596       
597       for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
598          AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
599          AliTPCCalROC* calROC = calPad->GetCalROC(isector);
600          AliTPCCalROC* outlierROC = 0;
601          if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
602          if (calROC) {
603             median[ivalue] = calROC->GetMedian();
604             mean[ivalue] = calROC->GetMean();
605             rms[ivalue] = calROC->GetRMS();
606             Double_t ltmrmsValue = 0;
607             ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
608             ltmrms[ivalue] = ltmrmsValue;
609             if (outlierROC) {
610                medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
611                meanWithOut[ivalue] = calROC->GetMean(outlierROC);
612                rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
613                ltmrmsValue = 0;
614                ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
615                ltmrmsWithOut[ivalue] = ltmrmsValue;
616             }
617          }
618          else {
619             median[ivalue] = 0.;
620             mean[ivalue] = 0.;
621             rms[ivalue] = 0.;
622             ltm[ivalue] = 0.;
623             ltmrms[ivalue] = 0.;
624             medianWithOut[ivalue] = 0.;
625             meanWithOut[ivalue] = 0.;
626             rmsWithOut[ivalue] = 0.;
627             ltmWithOut[ivalue] = 0.;
628             ltmrmsWithOut[ivalue] = 0.;
629          }
630       }
631       
632       //
633       // fill vectors of variable per pad
634       //
635       TVectorF *posArray = new TVectorF[8];
636       for (Int_t ivalue = 0; ivalue < 8; ivalue++)
637          posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
638
639       Float_t posG[3] = {0};
640       Float_t posL[3] = {0};
641       Int_t ichannel = 0;
642       for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
643          for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
644             tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
645             tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
646             posArray[0][ichannel] = irow;
647             posArray[1][ichannel] = ipad;
648             posArray[2][ichannel] = posL[0];
649             posArray[3][ichannel] = posL[1];
650             posArray[4][ichannel] = posG[0];
651             posArray[5][ichannel] = posG[1];
652             posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
653             posArray[7][ichannel] = ichannel;
654             
655             // loop over array containing AliTPCCalPads
656             for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
657                AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
658                AliTPCCalROC* calROC = calPad->GetCalROC(isector);
659                if (calROC)
660                   (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
661                else
662                   (vectorArray[ivalue])[ichannel] = 0;
663             }
664             ichannel++;
665          }
666       }
667       
668       cstream << "calPads" <<
669          "sector=" << isector;
670       
671       for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
672          cstream << "calPads" <<
673             (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
674             (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
675             (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
676             (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
677             (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
678          if (outlierPad) {
679             cstream << "calPads" <<
680                (Char_t*)((names[ivalue] + "_Median_OutlierCutted=").Data()) << medianWithOut[ivalue] <<
681                (Char_t*)((names[ivalue] + "_Mean_OutlierCutted=").Data()) << meanWithOut[ivalue] <<
682                (Char_t*)((names[ivalue] + "_RMS_OutlierCutted=").Data()) << rmsWithOut[ivalue] <<
683                (Char_t*)((names[ivalue] + "_LTM_OutlierCutted=").Data()) << ltmWithOut[ivalue] <<
684                (Char_t*)((names[ivalue] + "_RMS_LTM_OutlierCutted=").Data()) << ltmrmsWithOut[ivalue];
685          }
686       }
687
688       for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
689          cstream << "calPads" <<
690             (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
691       }
692
693       if (mapFileName) {
694          for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
695             if (isector < 36)
696                cstream << "calPads" <<
697                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
698             else
699                cstream << "calPads" <<
700                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
701          }
702       }
703
704       cstream << "calPads" <<
705          "row.=" << &posArray[0] <<
706          "pad.=" << &posArray[1] <<
707          "lx.=" << &posArray[2] <<
708          "ly.=" << &posArray[3] <<
709          "gx.=" << &posArray[4] <<
710          "gy.=" << &posArray[5] <<
711          "rpad.=" << &posArray[6] <<
712          "channel.=" << &posArray[7];
713          
714       cstream << "calPads" <<
715          "\n";
716
717       delete[] posArray;
718       delete[] vectorArray;
719    }
720    
721
722    delete[] names;
723    if (mapFileName) {
724       delete mapIROCs;
725       delete mapOROCs;
726       delete[] mapIROCArray;
727       delete[] mapOROCArray;
728       delete[] mapNames;
729    }
730 }
731
732
733
734 void AliTPCcalibDB::RegisterExB(Int_t index, Float_t bz, Bool_t bdelete){
735   //
736   // Register static ExB correction map
737   // index - registration index - used for visualization
738   // bz    - bz field in kGaus
739
740   //  Float_t factor =  bz/(-5.);  // default b filed in Cheb with minus sign
741   Float_t factor =  bz/(5.);  // default b filed in Cheb with minus sign
742                               // was chenged in the Revision ???? (Ruben can you add here number)
743   
744   AliMagF*   bmap = new AliMagF("MapsExB","MapsExB", 2,factor,1., 10.,AliMagF::k5kG,"$(ALICE_ROOT)/data/maps/mfchebKGI_sym.root");
745   
746   AliTPCExBFirst *exb  = new  AliTPCExBFirst(bmap,0.88*2.6400e+04,50,50,50);
747   AliTPCExB::SetInstance(exb);
748   
749   if (bdelete){
750     delete bmap;
751   }else{
752     AliTPCExB::RegisterField(index,bmap);
753   }
754   if (index>=fgExBArray.GetEntries()) fgExBArray.Expand((index+1)*2+11);
755   fgExBArray.AddAt(exb,index);
756 }
757
758
759 AliTPCExB*    AliTPCcalibDB::GetExB(Float_t bz, Bool_t deleteB) {
760   //
761   // bz filed in KGaus not in tesla
762   // Get ExB correction map
763   // if doesn't exist - create it
764   //
765   Int_t index = TMath::Nint(5+bz);
766   if (index>fgExBArray.GetEntries()) fgExBArray.Expand((index+1)*2+11);
767   if (!fgExBArray.At(index)) AliTPCcalibDB::RegisterExB(index,bz,deleteB);
768   return (AliTPCExB*)fgExBArray.At(index);
769 }
770
771
772 void  AliTPCcalibDB::SetExBField(Float_t bz){
773   //
774   // Set magnetic filed for ExB correction
775   //
776   fExB = GetExB(bz,kFALSE);
777 }
778
779 void  AliTPCcalibDB::SetExBField(const AliMagF*   bmap){
780   //
781   // Set magnetic field for ExB correction
782   //
783   AliTPCExBFirst *exb  = new  AliTPCExBFirst(bmap,0.88*2.6400e+04,50,50,50);
784   AliTPCExB::SetInstance(exb);
785   fExB=exb;
786 }
787
788
789
790
791
792 void AliTPCcalibDB::UpdateRunInformations( Int_t run, Bool_t force){
793   //
794   // - > Don't use it for reconstruction - Only for Calibration studies
795   //
796   AliCDBEntry * entry = 0;
797   if (run>= fRunList.GetSize()){
798     fRunList.Set(run*2+1);
799     fGRPArray.Expand(run*2+1);
800     fGRPMaps.Expand(run*2+1);
801     fGoofieArray.Expand(run*2+1);
802     fVoltageArray.Expand(run*2+1); 
803     fTemperatureArray.Expand(run*2+1);
804     fVdriftArray.Expand(run*2+1);
805     fDriftCorrectionArray.Expand(run*2+1);
806     fTimeGainSplinesArray.Expand(run*2+1);
807   }
808   if (fRunList[run]>0 &&force==kFALSE) return;
809   //
810   entry = AliCDBManager::Instance()->Get("GRP/GRP/Data",run);
811   if (entry)  {
812     AliGRPObject * grpRun = dynamic_cast<AliGRPObject*>(entry->GetObject());
813     if (!grpRun){
814       TMap* map = dynamic_cast<TMap*>(entry->GetObject());
815       if (map){
816         //grpRun = new AliGRPObject; 
817         //grpRun->ReadValuesFromMap(map);
818         grpRun =  MakeGRPObjectFromMap(map);
819
820         fGRPMaps.AddAt(map,run);
821       }
822     }
823     fGRPArray.AddAt(grpRun,run);
824   }
825   entry = AliCDBManager::Instance()->Get("TPC/Calib/Goofie",run);
826   if (entry){
827     fGoofieArray.AddAt(entry->GetObject(),run);
828   }
829   //
830   entry = AliCDBManager::Instance()->Get("TPC/Calib/HighVoltage",run);
831   if (entry)  {
832     fVoltageArray.AddAt(entry->GetObject(),run);
833   }
834   //
835   entry = AliCDBManager::Instance()->Get("TPC/Calib/TimeGain",run);
836   if (entry)  {
837     fTimeGainSplinesArray.AddAt(entry->GetObject(),run);
838   }
839   //
840   entry = AliCDBManager::Instance()->Get("TPC/Calib/TimeDrift",run);
841   if (entry)  {
842     fDriftCorrectionArray.AddAt(entry->GetObject(),run);
843   }
844   //
845   entry = AliCDBManager::Instance()->Get("TPC/Calib/Temperature",run);
846   if (entry)  {
847     fTemperatureArray.AddAt(entry->GetObject(),run);
848   }
849   fRunList[run]=1;  // sign as used
850
851   AliDCSSensor * press = GetPressureSensor(run,0);
852   AliTPCSensorTempArray * temp = GetTemperatureSensor(run);
853   if (press && temp){
854     AliTPCCalibVdrift * vdrift = new AliTPCCalibVdrift(temp, press,0);
855     fVdriftArray.AddAt(vdrift,run);
856   }
857 }
858
859
860 Float_t AliTPCcalibDB::GetGain(Int_t sector, Int_t row, Int_t pad){
861   //
862   //
863   AliTPCCalPad *calPad = Instance()->fDedxGainFactor;;
864   if (!calPad) return 0;
865   return calPad->GetCalROC(sector)->GetValue(row,pad);
866 }
867
868 AliSplineFit* AliTPCcalibDB::GetVdriftSplineFit(const char* name, Int_t run){
869   //
870   //
871   //
872   TObjArray *arr=GetTimeVdriftSplineRun(run);
873   if (!arr) return 0;
874   return dynamic_cast<AliSplineFit*>(arr->FindObject(name));
875 }
876
877 AliSplineFit* AliTPCcalibDB::CreateVdriftSplineFit(const char* graphName, Int_t run){
878   //
879   // create spline fit from the drift time graph in TimeDrift
880   //
881   TObjArray *arr=GetTimeVdriftSplineRun(run);
882   if (!arr) return 0;
883   TGraph *graph=dynamic_cast<TGraph*>(arr->FindObject(graphName));
884   if (!graph) return 0;
885   AliSplineFit *fit = new AliSplineFit();
886   fit->SetGraph(graph);
887   fit->SetMinPoints(graph->GetN()+1);
888   fit->InitKnots(graph,2,0,0.001);
889   fit->SplineFit(0);
890   return fit;
891 }
892
893 AliGRPObject *AliTPCcalibDB::GetGRP(Int_t run){
894   //
895   // Get GRP object for given run 
896   //
897   AliGRPObject * grpRun = dynamic_cast<AliGRPObject *>((Instance()->fGRPArray).At(run));
898   if (!grpRun) {
899     Instance()->UpdateRunInformations(run);
900     grpRun = dynamic_cast<AliGRPObject *>(Instance()->fGRPArray.At(run));
901     if (!grpRun) return 0; 
902   }
903   return grpRun;
904 }
905
906 TMap *  AliTPCcalibDB::GetGRPMap(Int_t run){
907   //
908   //
909   //
910   TMap * grpRun = dynamic_cast<TMap *>((Instance()->fGRPMaps).At(run));
911   if (!grpRun) {
912     Instance()->UpdateRunInformations(run);
913     grpRun = dynamic_cast<TMap *>(Instance()->fGRPMaps.At(run));
914     if (!grpRun) return 0; 
915   }
916   return grpRun;
917 }
918
919
920 AliDCSSensor * AliTPCcalibDB::GetPressureSensor(Int_t run, Int_t type){
921   //
922   // Get Pressure sensor
923   // run  = run number
924   // type = 0 - Cavern pressure
925   //        1 - Suface pressure
926   // First try to get if trom map - if existing  (Old format of data storing)
927   //
928
929
930   TMap *map = GetGRPMap(run);  
931   if (map){
932     AliDCSSensor * sensor = 0;
933     TObject *osensor=0;
934     if (type==0) osensor = ((*map)("fCavernPressure"));
935     if (type==1) osensor = ((*map)("fP2Pressure"));
936     sensor =dynamic_cast<AliDCSSensor *>(osensor); 
937     if (sensor) return sensor;
938   }
939   //
940   // If not map try to get it from the GRPObject
941   //
942   AliGRPObject * grpRun = dynamic_cast<AliGRPObject *>(fGRPArray.At(run)); 
943   if (!grpRun) {
944     UpdateRunInformations(run);
945     grpRun = dynamic_cast<AliGRPObject *>(fGRPArray.At(run));
946     if (!grpRun) return 0; 
947   }
948   AliDCSSensor * sensor = grpRun->GetCavernAtmosPressure();
949   if (type==1) sensor = grpRun->GetSurfaceAtmosPressure();
950   return sensor; 
951 }
952
953 AliTPCSensorTempArray * AliTPCcalibDB::GetTemperatureSensor(Int_t run){
954   //
955   // Get temperature sensor array
956   //
957   AliTPCSensorTempArray * tempArray = (AliTPCSensorTempArray *)fTemperatureArray.At(run);
958   if (!tempArray) {
959     UpdateRunInformations(run);
960     tempArray = (AliTPCSensorTempArray *)fTemperatureArray.At(run);
961   }
962   return tempArray;
963 }
964
965
966 TObjArray * AliTPCcalibDB::GetTimeGainSplinesRun(Int_t run){
967   //
968   // Get temperature sensor array
969   //
970   TObjArray * gainSplines = (TObjArray *)fTimeGainSplinesArray.At(run);
971   if (!gainSplines) {
972     UpdateRunInformations(run);
973     gainSplines = (TObjArray *)fTimeGainSplinesArray.At(run);
974   }
975   return gainSplines;
976 }
977
978 TObjArray * AliTPCcalibDB::GetTimeVdriftSplineRun(Int_t run){
979   //
980   // Get drift spline array
981   //
982   TObjArray * driftSplines = (TObjArray *)fDriftCorrectionArray.At(run);
983   if (!driftSplines) {
984     UpdateRunInformations(run);
985     driftSplines = (TObjArray *)fDriftCorrectionArray.At(run);
986   }
987   return driftSplines;
988 }
989
990 AliDCSSensorArray * AliTPCcalibDB::GetVoltageSensors(Int_t run){
991   //
992   // Get temperature sensor array
993   //
994   AliDCSSensorArray * voltageArray = (AliDCSSensorArray *)fVoltageArray.At(run);
995   if (!voltageArray) {
996     UpdateRunInformations(run);
997     voltageArray = (AliDCSSensorArray *)fVoltageArray.At(run);
998   }
999   return voltageArray;
1000 }
1001
1002 AliDCSSensorArray * AliTPCcalibDB::GetGoofieSensors(Int_t run){
1003   //
1004   // Get temperature sensor array
1005   //
1006   AliDCSSensorArray * goofieArray = (AliDCSSensorArray *)fGoofieArray.At(run);
1007   if (!goofieArray) {
1008     UpdateRunInformations(run);
1009     goofieArray = (AliDCSSensorArray *)fGoofieArray.At(run);
1010   }
1011   return goofieArray;
1012 }
1013
1014
1015
1016 AliTPCCalibVdrift *     AliTPCcalibDB::GetVdrift(Int_t run){
1017   //
1018   // Get the interface to the the vdrift 
1019   //
1020   AliTPCCalibVdrift  * vdrift = (AliTPCCalibVdrift*)fVdriftArray.At(run);
1021   if (!vdrift) {
1022     UpdateRunInformations(run);
1023     vdrift= (AliTPCCalibVdrift*)fVdriftArray.At(run);
1024   }
1025   return vdrift;
1026 }
1027
1028 Float_t AliTPCcalibDB::GetCEdriftTime(Int_t run, Int_t sector, Double_t timeStamp, Int_t *entries)
1029 {
1030   //
1031   // GetCE drift time information for 'sector'
1032   // sector 72 is the mean drift time of the A-Side
1033   // sector 73 is the mean drift time of the C-Side
1034   // it timestamp==-1 return mean value
1035   //
1036   AliTPCcalibDB::Instance()->SetRun(run);
1037   TGraph *gr=AliTPCcalibDB::Instance()->GetCErocTgraph(sector);
1038   if (!gr||sector<0||sector>73) {
1039     if (entries) *entries=0;
1040     return 0.;
1041   }
1042   Float_t val=0.;
1043   if (timeStamp==-1.){
1044     val=gr->GetMean(2);
1045   }else{
1046     for (Int_t ipoint=0;ipoint<gr->GetN();++ipoint){
1047       Double_t x,y;
1048       gr->GetPoint(ipoint,x,y);
1049       if (x<timeStamp) continue;
1050       val=y;
1051       break;
1052     }
1053   }
1054   return val;
1055 }
1056   
1057 Float_t AliTPCcalibDB::GetCEchargeTime(Int_t run, Int_t sector, Double_t timeStamp, Int_t *entries)
1058 {
1059   //
1060   // GetCE mean charge for 'sector'
1061   // it timestamp==-1 return mean value
1062   //
1063   AliTPCcalibDB::Instance()->SetRun(run);
1064   TGraph *gr=AliTPCcalibDB::Instance()->GetCErocQgraph(sector);
1065   if (!gr||sector<0||sector>71) {
1066     if (entries) *entries=0;
1067     return 0.;
1068   }
1069   Float_t val=0.;
1070   if (timeStamp==-1.){
1071     val=gr->GetMean(2);
1072   }else{
1073     for (Int_t ipoint=0;ipoint<gr->GetN();++ipoint){
1074       Double_t x,y;
1075       gr->GetPoint(ipoint,x,y);
1076       if (x<timeStamp) continue;
1077       val=y;
1078       break;
1079     }
1080   }
1081   return val;
1082 }
1083
1084 Float_t AliTPCcalibDB::GetDCSSensorValue(AliDCSSensorArray *arr, Int_t timeStamp, const char * sensorName, Int_t sigDigits)
1085 {
1086   //
1087   // Get Value for a DCS sensor 'sensorName', run 'run' at time 'timeStamp'
1088   //
1089   Float_t val=0;
1090   const TString sensorNameString(sensorName);
1091   AliDCSSensor *sensor = arr->GetSensor(sensorNameString);
1092   if (!sensor) return val;
1093   //use the dcs graph if possible
1094   TGraph *gr=sensor->GetGraph();
1095   if (gr){
1096     for (Int_t ipoint=0;ipoint<gr->GetN();++ipoint){
1097       Double_t x,y;
1098       gr->GetPoint(ipoint,x,y);
1099       Int_t time=TMath::Nint(sensor->GetStartTime()+x*3600); //time in graph is hours
1100       if (time<timeStamp) continue;
1101       val=y;
1102       break;
1103     }
1104     //if val is still 0, test if if the requested time if within 5min of the first/last
1105     //data point. If this is the case return the firs/last entry
1106     //the timestamps might not be syncronised for all calibration types, sometimes a 'pre'
1107     //and 'pos' period is requested. Especially to the HV this is not the case!
1108     //first point
1109     if (val==0 ){
1110       Double_t x,y;
1111       gr->GetPoint(0,x,y);
1112       Int_t time=TMath::Nint(sensor->GetStartTime()+x*3600); //time in graph is hours
1113       if ((time-timeStamp)<5*60) val=y;
1114     }
1115     //last point
1116     if (val==0 ){
1117       Double_t x,y;
1118       gr->GetPoint(gr->GetN()-1,x,y);
1119       Int_t time=TMath::Nint(sensor->GetStartTime()+x*3600); //time in graph is hours
1120       if ((timeStamp-time)<5*60) val=y;
1121     }
1122   } else {
1123     val=sensor->GetValue(timeStamp);
1124   }
1125   if (sigDigits>=0){
1126     val=(Float_t)TMath::Floor(val * TMath::Power(10., sigDigits) + .5) / TMath::Power(10., sigDigits);
1127   }
1128   return val;
1129 }
1130
1131 Float_t AliTPCcalibDB::GetDCSSensorMeanValue(AliDCSSensorArray *arr, const char * sensorName, Int_t sigDigits)
1132 {
1133   //
1134   // Get mean Value for a DCS sensor 'sensorName' during run 'run'
1135   //
1136   Float_t val=0;
1137   const TString sensorNameString(sensorName);
1138   AliDCSSensor *sensor = arr->GetSensor(sensorNameString);
1139   if (!sensor) return val;
1140
1141   //use dcs graph if it exists
1142   TGraph *gr=sensor->GetGraph();
1143   if (gr){
1144     val=gr->GetMean(2);
1145   } else {
1146     //if we don't have the dcs graph, try to get some meaningful information
1147     if (!sensor->GetFit()) return val;
1148     Int_t nKnots=sensor->GetFit()->GetKnots();
1149     Double_t tMid=(sensor->GetEndTime()-sensor->GetStartTime())/2.;
1150     for (Int_t iKnot=0;iKnot<nKnots;++iKnot){
1151       if (sensor->GetFit()->GetX()[iKnot]>tMid/3600.) break;
1152       val=(Float_t)sensor->GetFit()->GetY0()[iKnot];
1153     }
1154   }
1155   if (sigDigits>=0){
1156     val/=10;
1157     val=(Float_t)TMath::Floor(val * TMath::Power(10., sigDigits) + .5) / TMath::Power(10., sigDigits);
1158     val*=10;
1159   }
1160   return val;
1161 }
1162
1163 Float_t AliTPCcalibDB::GetChamberHighVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits) {
1164   //
1165   // return the chamber HV for given run and time: 0-35 IROC, 36-72 OROC
1166   // if timeStamp==-1 return mean value
1167   //
1168   Float_t val=0;
1169   TString sensorName="";
1170   TTimeStamp stamp(timeStamp);
1171   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1172   if (!voltageArray || (sector<0) || (sector>71)) return val;
1173   Char_t sideName='A';
1174   if ((sector/18)%2==1) sideName='C';
1175   if (sector<36){
1176     //IROC
1177     sensorName=Form("TPC_ANODE_I_%c%02d_VMEAS",sideName,sector%18);
1178   }else{
1179     //OROC
1180     sensorName=Form("TPC_ANODE_O_%c%02d_0_VMEAS",sideName,sector%18);
1181   }
1182   if (timeStamp==-1){
1183     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1184   } else {
1185     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1186   }
1187   return val;
1188 }
1189 Float_t AliTPCcalibDB::GetSkirtVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits)
1190 {
1191   //
1192   // Get the skirt voltage for 'run' at 'timeStamp' and 'sector': 0-35 IROC, 36-72 OROC
1193   // type corresponds to the following: 0 - IROC A-Side; 1 - IROC C-Side; 2 - OROC A-Side; 3 - OROC C-Side
1194   // if timeStamp==-1 return the mean value for the run
1195   //
1196   Float_t val=0;
1197   TString sensorName="";
1198   TTimeStamp stamp(timeStamp);
1199   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1200   if (!voltageArray || (sector<0) || (sector>71)) return val;
1201   Char_t sideName='A';
1202   if ((sector/18)%2==1) sideName='C';
1203   sensorName=Form("TPC_SKIRT_%c_VMEAS",sideName);
1204   if (timeStamp==-1){
1205     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1206   } else {
1207     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1208   }
1209   return val;
1210 }
1211
1212 Float_t AliTPCcalibDB::GetCoverVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits)
1213 {
1214   //
1215   // Get the cover voltage for run 'run' at time 'timeStamp'
1216   // type corresponds to the following: 0 - IROC A-Side; 1 - IROC C-Side; 2 - OROC A-Side; 3 - OROC C-Side
1217   // if timeStamp==-1 return the mean value for the run
1218   //
1219   Float_t val=0;
1220   TString sensorName="";
1221   TTimeStamp stamp(timeStamp);
1222   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1223   if (!voltageArray || (sector<0) || (sector>71)) return val;
1224   Char_t sideName='A';
1225   if ((sector/18)%2==1) sideName='C';
1226   if (sector<36){
1227     //IROC
1228     sensorName=Form("TPC_COVER_I_%c_VMEAS",sideName);
1229   }else{
1230     //OROC
1231     sensorName=Form("TPC_COVER_O_%c_VMEAS",sideName);
1232   }
1233   if (timeStamp==-1){
1234     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1235   } else {
1236     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1237   }
1238   return val;
1239 }
1240
1241 Float_t AliTPCcalibDB::GetGGoffsetVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits)
1242 {
1243   //
1244   // Get the GG offset voltage for run 'run' at time 'timeStamp'
1245   // type corresponds to the following: 0 - IROC A-Side; 1 - IROC C-Side; 2 - OROC A-Side; 3 - OROC C-Side
1246   // if timeStamp==-1 return the mean value for the run
1247   //
1248   Float_t val=0;
1249   TString sensorName="";
1250   TTimeStamp stamp(timeStamp);
1251   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1252   if (!voltageArray || (sector<0) || (sector>71)) return val;
1253   Char_t sideName='A';
1254   if ((sector/18)%2==1) sideName='C';
1255   if (sector<36){
1256     //IROC
1257     sensorName=Form("TPC_GATE_I_%c_OFF_VMEAS",sideName);
1258   }else{
1259     //OROC
1260     sensorName=Form("TPC_GATE_O_%c_OFF_VMEAS",sideName);
1261   }
1262   if (timeStamp==-1){
1263     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1264   } else {
1265     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1266   }
1267   return val;
1268 }
1269
1270 Float_t AliTPCcalibDB::GetGGnegVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits)
1271 {
1272   //
1273   // Get the GG offset voltage for run 'run' at time 'timeStamp'
1274   // type corresponds to the following: 0 - IROC A-Side; 1 - IROC C-Side; 2 - OROC A-Side; 3 - OROC C-Side
1275   // if timeStamp==-1 return the mean value for the run
1276   //
1277   Float_t val=0;
1278   TString sensorName="";
1279   TTimeStamp stamp(timeStamp);
1280   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1281   if (!voltageArray || (sector<0) || (sector>71)) return val;
1282   Char_t sideName='A';
1283   if ((sector/18)%2==1) sideName='C';
1284   if (sector<36){
1285     //IROC
1286     sensorName=Form("TPC_GATE_I_%c_NEG_VMEAS",sideName);
1287   }else{
1288     //OROC
1289     sensorName=Form("TPC_GATE_O_%c_NEG_VMEAS",sideName);
1290   }
1291   if (timeStamp==-1){
1292     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1293   } else {
1294     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1295   }
1296   return val;
1297 }
1298
1299 Float_t AliTPCcalibDB::GetGGposVoltage(Int_t run, Int_t sector, Int_t timeStamp, Int_t sigDigits)
1300 {
1301   //
1302   // Get the GG offset voltage for run 'run' at time 'timeStamp'
1303   // type corresponds to the following: 0 - IROC A-Side; 1 - IROC C-Side; 2 - OROC A-Side; 3 - OROC C-Side
1304   // if timeStamp==-1 return the mean value for the run
1305   //
1306   Float_t val=0;
1307   TString sensorName="";
1308   TTimeStamp stamp(timeStamp);
1309   AliDCSSensorArray* voltageArray = AliTPCcalibDB::Instance()->GetVoltageSensors(run);
1310   if (!voltageArray || (sector<0) || (sector>71)) return val;
1311   Char_t sideName='A';
1312   if ((sector/18)%2==1) sideName='C';
1313   if (sector<36){
1314     //IROC
1315     sensorName=Form("TPC_GATE_I_%c_POS_VMEAS",sideName);
1316   }else{
1317     //OROC
1318     sensorName=Form("TPC_GATE_O_%c_POS_VMEAS",sideName);
1319   }
1320   if (timeStamp==-1){
1321     val=AliTPCcalibDB::GetDCSSensorMeanValue(voltageArray, sensorName.Data(),sigDigits);
1322   } else {
1323     val=AliTPCcalibDB::GetDCSSensorValue(voltageArray, timeStamp, sensorName.Data(),sigDigits);
1324   }
1325   return val;
1326 }
1327
1328 Float_t AliTPCcalibDB::GetPressure(Int_t timeStamp, Int_t run, Int_t type){
1329   //
1330   // GetPressure for given time stamp and runt
1331   //
1332   TTimeStamp stamp(timeStamp);
1333   AliDCSSensor * sensor = Instance()->GetPressureSensor(run,type);
1334   if (!sensor) return 0;
1335   return sensor->GetValue(stamp);
1336 }
1337
1338 Float_t AliTPCcalibDB::GetL3Current(Int_t run, Int_t statType){
1339   //
1340   // return L3 current
1341   // stat type is: AliGRPObject::Stats: kMean = 0, kTruncMean = 1, kMedian = 2, kSDMean = 3, kSDMedian = 4
1342   //
1343   Float_t current=-1;
1344   AliGRPObject *grp=AliTPCcalibDB::GetGRP(run);
1345   if (grp) current=grp->GetL3Current((AliGRPObject::Stats)statType);
1346   return current;
1347 }
1348
1349 Float_t AliTPCcalibDB::GetBz(Int_t run){
1350   //
1351   // calculate BZ in T from L3 current
1352   //
1353   Float_t bz=-1;
1354   Float_t current=AliTPCcalibDB::GetL3Current(run);
1355   if (current>-1) bz=5*current/30000.*.1;
1356   return bz;
1357 }
1358
1359 Char_t  AliTPCcalibDB::GetL3Polarity(Int_t run) {
1360   //
1361   // get l3 polarity from GRP
1362   //
1363   Char_t pol=-100;
1364   AliGRPObject *grp=AliTPCcalibDB::GetGRP(run);
1365   if (grp) pol=grp->GetL3Polarity();
1366   return pol;
1367 }
1368
1369 TString AliTPCcalibDB::GetRunType(Int_t run){
1370   //
1371   // return run type from grp
1372   //
1373
1374 //   TString type("UNKNOWN");
1375   AliGRPObject *grp=AliTPCcalibDB::GetGRP(run);
1376   if (grp) return grp->GetRunType();
1377   return "UNKNOWN";
1378 }
1379
1380 Float_t AliTPCcalibDB::GetValueGoofie(Int_t timeStamp, Int_t run, Int_t type){
1381   //
1382   // GetPressure for given time stamp and runt
1383   //
1384   TTimeStamp stamp(timeStamp);
1385   AliDCSSensorArray* goofieArray = AliTPCcalibDB::Instance()->GetGoofieSensors(run);
1386   if (!goofieArray) return 0;
1387   AliDCSSensor *sensor = goofieArray->GetSensor(type);
1388   return sensor->GetValue(stamp);
1389 }
1390
1391
1392
1393
1394
1395
1396 Bool_t  AliTPCcalibDB::GetTemperatureFit(Int_t timeStamp, Int_t run, Int_t side,TVectorD& fit){
1397   //
1398   //
1399   //
1400   TTimeStamp tstamp(timeStamp);
1401   AliTPCSensorTempArray* tempArray  = Instance()->GetTemperatureSensor(run);
1402   if (! tempArray) return kFALSE;
1403   AliTPCTempMap * tempMap = new AliTPCTempMap(tempArray);
1404   TLinearFitter * fitter = tempMap->GetLinearFitter(3,side,tstamp);
1405   if (fitter){
1406     fitter->Eval(); 
1407     fitter->GetParameters(fit);
1408   }
1409   delete fitter;
1410   delete tempMap;
1411   if (!fitter) return kFALSE;
1412   return kTRUE;
1413 }
1414
1415 Float_t AliTPCcalibDB::GetTemperature(Int_t timeStamp, Int_t run, Int_t side){
1416   //
1417   //
1418   //
1419   TVectorD vec(5);
1420   if (side==0) {
1421     GetTemperatureFit(timeStamp,run,0,vec);
1422     return vec[0];
1423   }
1424   if (side==1){
1425     GetTemperatureFit(timeStamp,run,0,vec);
1426     return vec[0];
1427   }
1428   return 0;
1429 }
1430
1431
1432 Double_t AliTPCcalibDB::GetPTRelative(UInt_t timeSec, Int_t run, Int_t side){
1433   //
1434   // Get relative P/T 
1435   // time - absolute time
1436   // run  - run number
1437   // side - 0 - A side   1-C side
1438   AliTPCCalibVdrift * vdrift =  Instance()->GetVdrift(run);
1439   if (!vdrift) return 0;
1440   return vdrift->GetPTRelative(timeSec,side);
1441 }
1442
1443 AliGRPObject * AliTPCcalibDB::MakeGRPObjectFromMap(TMap *map){
1444   //
1445   // Function to covert old GRP run information from TMap to GRPObject
1446   //
1447   //  TMap * map = AliTPCcalibDB::GetGRPMap(52406);
1448   if (!map) return 0;
1449   AliDCSSensor * sensor = 0;
1450   TObject *osensor=0;
1451   osensor = ((*map)("fP2Pressure"));
1452   sensor  =dynamic_cast<AliDCSSensor *>(osensor); 
1453   //
1454   if (!sensor) return 0;
1455   //
1456   AliDCSSensor * sensor2 = new AliDCSSensor(*sensor);
1457   osensor = ((*map)("fCavernPressure"));
1458   TGraph * gr = new TGraph(2);
1459   gr->GetX()[0]= -100000.;
1460   gr->GetX()[1]= 1000000.;
1461   gr->GetY()[0]= atof(osensor->GetName());
1462   gr->GetY()[1]= atof(osensor->GetName());
1463   sensor2->SetGraph(gr);
1464   sensor2->SetFit(0);
1465   
1466
1467   AliGRPObject *grpRun = new AliGRPObject; 
1468   grpRun->ReadValuesFromMap(map);
1469   grpRun->SetCavernAtmosPressure(sensor2);
1470   grpRun->SetSurfaceAtmosPressure(sensor);
1471   return grpRun;
1472 }
1473
1474 Bool_t AliTPCcalibDB::CreateGUITree(Int_t run, const char* filename)
1475 {
1476   //
1477   // Create a gui tree for run number 'run'
1478   //
1479
1480   if (!AliCDBManager::Instance()->GetDefaultStorage()){
1481     AliLog::Message(AliLog::kError, "Default Storage not set. Cannot create Calibration Tree!",
1482                     MODULENAME(), "AliTPCcalibDB", FUNCTIONNAME(), __FILE__, __LINE__);
1483     return kFALSE;
1484   }
1485   //db instance
1486   AliTPCcalibDB *db=AliTPCcalibDB::Instance();
1487   // retrieve cal pad objects
1488   db->SetRun(run);
1489   AliTPCPreprocessorOnline prep;
1490   //noise and pedestals
1491   prep.AddComponent(db->GetPedestals());
1492   prep.AddComponent(db->GetPadNoise());
1493   //pulser data
1494   prep.AddComponent(db->GetPulserTmean());
1495   prep.AddComponent(db->GetPulserTrms());
1496   prep.AddComponent(db->GetPulserQmean());
1497   //CE data
1498   prep.AddComponent(db->GetCETmean());
1499   prep.AddComponent(db->GetCETrms());
1500   prep.AddComponent(db->GetCEQmean());
1501   //Altro data
1502   prep.AddComponent(db->GetALTROAcqStart() );
1503   prep.AddComponent(db->GetALTROZsThr()    );
1504   prep.AddComponent(db->GetALTROFPED()     );
1505   prep.AddComponent(db->GetALTROAcqStop()  );
1506   prep.AddComponent(db->GetALTROMasked()   );
1507   //
1508   TString file(filename);
1509   if (file.IsNull()) file=Form("guiTreeRun_%d.root",run);
1510   prep.DumpToFile(file.Data());
1511   return kTRUE;
1512 }
1513
1514 Bool_t AliTPCcalibDB::CreateRefFile(Int_t run, const char* filename)
1515 {
1516   //
1517   // Create a gui tree for run number 'run'
1518   //
1519   
1520   if (!AliCDBManager::Instance()->GetDefaultStorage()){
1521     AliLog::Message(AliLog::kError, "Default Storage not set. Cannot create Calibration Tree!",
1522                     MODULENAME(), "AliTPCcalibDB", FUNCTIONNAME(), __FILE__, __LINE__);
1523     return kFALSE;
1524   }
1525   TString file(filename);
1526   if (file.IsNull()) file=Form("RefCalPads_%d.root",run);
1527   TDirectory *currDir=gDirectory;
1528   //db instance
1529   AliTPCcalibDB *db=AliTPCcalibDB::Instance();
1530   // retrieve cal pad objects
1531   db->SetRun(run);
1532   //open file
1533   TFile f(file.Data(),"recreate");
1534   //noise and pedestals
1535   db->GetPedestals()->Write("Pedestals");
1536   db->GetPadNoise()->Write("PadNoise");
1537   //pulser data
1538   db->GetPulserTmean()->Write("PulserTmean");
1539   db->GetPulserTrms()->Write("PulserTrms");
1540   db->GetPulserQmean()->Write("PulserQmean");
1541   //CE data
1542   db->GetCETmean()->Write("CETmean");
1543   db->GetCETrms()->Write("CETrms");
1544   db->GetCEQmean()->Write("CEQmean");
1545   //Altro data
1546   db->GetALTROAcqStart() ->Write("ALTROAcqStart");
1547   db->GetALTROZsThr()    ->Write("ALTROZsThr");
1548   db->GetALTROFPED()     ->Write("ALTROFPED");
1549   db->GetALTROAcqStop()  ->Write("ALTROAcqStop");
1550   db->GetALTROMasked()   ->Write("ALTROMasked");
1551   //
1552   f.Close();
1553   currDir->cd();
1554   return kTRUE;
1555 }
1556
1557
1558
1559 Double_t AliTPCcalibDB::GetVDriftCorrectionTime(Int_t timeStamp, Int_t run, Int_t side, Int_t mode){
1560   //
1561   // Get time dependent drift velocity correction
1562   // multiplication factor        vd = vdnom *(1+vdriftcorr)
1563   // Arguments:
1564   // mode determines the algorith how to combine the Laser Track, LaserCE and physics tracks
1565   // timestamp - timestamp
1566   // run       - run number
1567   // side      - the drift velocity per side (possible for laser and CE)
1568   //
1569   // Notice - Extrapolation outside of calibration range  - using constant function
1570   //
1571   return 0;
1572 }
1573
1574 Double_t AliTPCcalibDB::GetTime0CorrectionTime(Int_t timeStamp, Int_t run, Int_t side, Int_t mode){
1575   //
1576   // Get time dependent time 0 (trigger delay) correction
1577   // additive correction        time0 = time0+ GetTime0CorrectionTime
1578   // Value etracted combining the vdrift correction using laser tracks and CE and the physics track matchin
1579   // Arguments:
1580   // mode determines the algorith how to combine the Laser Track and physics tracks
1581   // timestamp - timestamp
1582   // run       - run number
1583   // side      - the drift velocity per side (possible for laser and CE)
1584   //
1585   // Notice - Extrapolation outside of calibration range  - using constant function
1586   //
1587   return 0;
1588 }
1589
1590
1591
1592
1593 Double_t AliTPCcalibDB::GetVDriftCorrectionGy(Int_t timeStamp, Int_t run, Int_t side, Int_t mode){
1594   //
1595   // Get global y correction drift velocity correction factor
1596   // additive factor        vd = vdnom*(1+GetVDriftCorrectionGy *gy)
1597   // Value etracted combining the vdrift correction using laser tracks and CE
1598   // Arguments:
1599   // mode determines the algorith how to combine the Laser Track, LaserCE
1600   // timestamp - timestamp
1601   // run       - run number
1602   // side      - the drift velocity gy correction per side (CE and Laser tracks)
1603   //
1604   // Notice - Extrapolation outside of calibration range  - using constant function
1605   //
1606   return 0;
1607 }