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