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