Make separate, specialized geometries for RPhi and RhoZ views.
[u/mrichter/AliRoot.git] / PHOS / AliPHOSCalibrManager.cxx
CommitLineData
f3bd42f5 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/* $Id$ */
17
18//_________________________________________________________________________
cd228525 19// Class provides interface between classes
20// consuming/producing calibration data,
21// such as AliPHOSClusterizer and AliPHOSCalibrator
22// from one side and database (now just a root file,
23// later - AliEn etc.)
24// Use case:
25// First Manager should be configured so that is reads data
26// from a given source. e.g. from a root file:
27// AliPHOSCalibrManager * m = AliPHOSCalibrManager::GetInstance("MyCalibrationRootFile.root") ;
28// After that one can real/write data to this source:
29// AliPHOSCalibrationData mydata("Gains","v1") ;
30// ......
31// m->Write(&mydata) ;
32// AliPHOSCalibrationData myanotherdata("Gains","v2") ;
33// m->ReadFromRoot(&myanotherdata) ;
f3bd42f5 34//
35//*-- Author : D.Peressounko (RRC KI & SUBATECH)
36//////////////////////////////////////////////////////////////////////////////
37
38
39// --- ROOT system ---
40#include "TROOT.h"
41#include "TKey.h"
42#include "TFile.h"
43
44// --- Standard library ---
00953b03 45#include <fstream>
f3bd42f5 46
47// --- AliRoot header files ---
6f06e050 48#include "AliLog.h"
f3bd42f5 49#include "AliPHOSConTableDB.h"
50#include "AliPHOSCalibrManager.h"
51#include "AliPHOSCalibrationData.h"
52ClassImp(AliPHOSCalibrManager)
53
e4778e0c 54
55 AliPHOSCalibrManager * AliPHOSCalibrManager::fgCaMa = 0 ;
f3bd42f5 56//____________________________________________________________________________
3f7dbdb7 57AliPHOSCalibrManager::AliPHOSCalibrManager() :
58 fFileName(""),
59 fInputKind(0),
60 fctdb(0)
f3bd42f5 61{
cd228525 62 // default ctor: not to be used
6f06e050 63 AliFatal(Form("Should not be used")) ;
f3bd42f5 64}
65//____________________________________________________________________________
e4778e0c 66AliPHOSCalibrManager::AliPHOSCalibrManager(const char* filename,const char * kind ):
3f7dbdb7 67 TNamed("AliPHOSCalibrManager",filename),
68 fFileName(filename),
69 fInputKind(0),
70 fctdb(0)
71{
e4778e0c 72 if(strcmp(kind,"root")==0){
73 fInputKind = 0 ;
74 }
75 else{
76 fInputKind = 1 ;
77 }
f3bd42f5 78}
3f7dbdb7 79
80//____________________________________________________________________________
81AliPHOSCalibrManager::AliPHOSCalibrManager(const AliPHOSCalibrManager & manager) :
82 TNamed(manager),
83 fFileName(""),
84 fInputKind(0),
85 fctdb(0)
86{
87 // cpy ctor: no need
88 // requested by the Coding Convention
89 Fatal("cpy ctor", "not implemented") ;
90}
91
f3bd42f5 92//____________________________________________________________________________
e4778e0c 93AliPHOSCalibrManager::~AliPHOSCalibrManager()
f3bd42f5 94{
00953b03 95 //dtor
e4778e0c 96 if(fInputKind==0){
97 TFile * f = gROOT->GetFile(fFileName) ;
98 if(f && f->IsOpen())
99 f->Close() ;
100 }
f3bd42f5 101}
102//____________________________________________________________________________
103AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(void)
104{
00953b03 105 // gets the instance of the unique object
e4778e0c 106 return fgCaMa ;
f3bd42f5 107}
108//____________________________________________________________________________
e4778e0c 109AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(const char* filename,const char * kind)
f3bd42f5 110{
cd228525 111 // Opens source (now only root file) and
112 // returns the instance of the unique object
e4778e0c 113
f3bd42f5 114 if(!fgCaMa)
e4778e0c 115 fgCaMa = new AliPHOSCalibrManager(filename,kind) ;
f3bd42f5 116 else{
e4778e0c 117 if(strcmp(filename,fgCaMa->fFileName.Data())){
118 //Close previous file, construct new Manager
119 delete fgCaMa ;
120 fgCaMa = new AliPHOSCalibrManager(filename,kind) ;
121 }
f3bd42f5 122 }
123 return fgCaMa ;
124}
125//____________________________________________________________________________
e4778e0c 126void AliPHOSCalibrManager::GetParameters(AliPHOSCalibrationData &data){
127 if(fInputKind == 0){
128 ReadFromRoot(data) ;
129 }
130 else{
131 ReadFromASCII(data) ;
132 }
133}
134//____________________________________________________________________________
135void AliPHOSCalibrManager::ReadFromASCII(AliPHOSCalibrationData &data){
136 // We read pedestals and gains from *.dat file with following format:
137 // 0 0 0 0 37.09 1972. // next nmodrows*nmodcols*ncryrows*ncrycols lines
138 // 0 0 0 1 28.53 2072. // contains <RR CC r c ped peak>
139 // 0 0 0 2 30.93 1938. //
140 // where module is an array of 8*8 crystals and RR and CC are module raw and column position
f3bd42f5 141
142 if(!fctdb){
6f06e050 143 AliError(Form("Specify Connections Table Database first")) ;
f3bd42f5 144 return ;
145 }
e4778e0c 146
147 FILE * file = fopen(fFileName, "r");
148 if (!file) {
149 Error("ReadFromASCII", "could not open file %s", fFileName.Data());
150 return;
151 }
152
153 Bool_t isPed = kFALSE ;
154 if(strcmp(data.GetCategory(),"Pedestals")==0){
155 isPed = 1 ;
156 }
157 else{
158 if(strcmp(data.GetCategory(),"Gains")){
159 Error("ReadFromASCII","Unknown data category: %s",data.GetCategory()) ;
160 return ;
161 }
162 }
163
164 Int_t modRaw,modCol,raw,col;
165 Float_t ped,pik;
166 Int_t nread = 0 ;
167 while(fscanf(file,"%d %d %d %d %f %f",&modRaw,&modCol,&raw,&col,&ped,&pik)==6){
168 //Calculate plain crystal position:
169 Int_t rawPosition = (modRaw*8+raw)*fctdb->GetNColumns()+modCol*8+col ;
170 Int_t absId = fctdb->Raw2AbsId(rawPosition);
171 if(isPed){
172 data.SetData(absId,ped) ;
173 }
174 else{
175 if(pik!=0.)
176 data.SetData(absId,1./pik);
177 else
178 data.SetData(absId,0.);
179 }
180 nread++ ;
181 }
182 if(nread != fctdb->GetNColumns()*fctdb->GetNRaws()){
183 Error("ReadFromASCII","Read %d parameters instead of %d\n",nread,fctdb->GetNColumns()*fctdb->GetNRaws()) ;
f3bd42f5 184 }
e4778e0c 185 fclose(file) ;
186
f3bd42f5 187}
188//____________________________________________________________________________
e4778e0c 189void AliPHOSCalibrManager::ReadFromRoot(AliPHOSCalibrationData & data)
00953b03 190{
f3bd42f5 191 //reads calibration parameters from root file
e4778e0c 192
f3bd42f5 193 //construct name
194 TString searchname(data.GetCategory()) ;
195 searchname+='_' ;
196 searchname+=data.GetVersion() ;
f3bd42f5 197 TFile * file = gROOT->GetFile(fFileName) ;
198 if(!file || !file->IsOpen())
199 file = TFile::Open(fFileName) ;
200 if(!file->IsOpen()){
6f06e050 201 AliError(Form("Can not open file %s\n",fFileName.Data() )) ;
f3bd42f5 202 return ;
203 }
6f06e050 204
e4778e0c 205 AliPHOSCalibrationData * tmp = dynamic_cast<AliPHOSCalibrationData*>(file->Get(searchname) ) ;
206 if(!tmp)
207 Error("ReadFromRoot","there is no data %s in file %s",fFileName.Data(),fFileName.Data()) ;
208 else
209 data=(*tmp);
210
211
212// TList * list = file->GetListOfKeys() ;
213// TIter next(list) ;
214// TKey * key ;
215// while((key=((TKey*)next()))){
216// TString kname(key->GetName()) ;
217// if(kname.BeginsWith(searchname)){
218// TString sbegin = kname(searchname.Sizeof()-1,
219// kname.Last('_')-searchname.Sizeof()+1) ;
220// TString send = kname(kname.Last('_')+1,kname.Sizeof()) ;
221// Int_t begin,end ;
222// if(sscanf(sbegin.Data(),"%d",&begin) &&
223// sscanf(send.Data(),"%d",&end)){
224// if(begin<=run && end >=run){
225// data=(*dynamic_cast<AliPHOSCalibrationData*>(file->Get(key->GetName()) ));
226// return ;
227// }
228// }
229// }
230// }
231// Error("ReadFromRoot","Can not find key %s for run %d in file %s \n",searchname.Data(),run,fFileName.Data()) ;
f3bd42f5 232}
233//____________________________________________________________________________
e4778e0c 234void AliPHOSCalibrManager::WriteData(AliPHOSCalibrationData &data)
00953b03 235{
236 //Writes data
f3bd42f5 237 TFile * file = gROOT->GetFile(fFileName) ;
238 if(!file || !file->IsOpen()){
239 file = TFile::Open(fFileName,"UPDATE") ;
240 if(!file->IsOpen()){
6f06e050 241 AliError(Form("Can not open file %s\n",fFileName.Data() )) ;
f3bd42f5 242 return ;
243 }
244 }
245 file->cd() ;
e4778e0c 246 TString kname(data.GetCategory()) ;
f3bd42f5 247 kname+='_';
e4778e0c 248 kname+=data.GetVersion() ;
249// kname+='_';
250// Int_t begin,end ;
251// data->GetValidityRange(begin,end) ;
252// kname+=begin ;
253// kname+='_' ;
254// kname+=end ;
255 data.Write(kname,TObject::kOverwrite) ;
f3bd42f5 256
257}
258//____________________________________________________________________________
259AliPHOSCalibrManager& AliPHOSCalibrManager::operator=(AliPHOSCalibrManager const & cdb)
260{
00953b03 261 //overloads = operator
f3bd42f5 262 fFileName = cdb.fFileName;
263 return *this ;
264}