New detector numbering scheme (common for DAQ/HLT/Offline). All the subdetectors...
[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//____________________________________________________________________________
57AliPHOSCalibrManager::AliPHOSCalibrManager():TNamed()
58{
cd228525 59 // default ctor: not to be used
e4778e0c 60 fInputKind = 0 ;
61 fFileName= "" ;
f3bd42f5 62 fctdb = 0 ;
6f06e050 63 AliFatal(Form("Should not be used")) ;
f3bd42f5 64}
65//____________________________________________________________________________
e4778e0c 66AliPHOSCalibrManager::AliPHOSCalibrManager(const char* filename,const char * kind ):
f3bd42f5 67 TNamed("AliPHOSCalibrManager",filename){
e4778e0c 68 if(strcmp(kind,"root")==0){
69 fInputKind = 0 ;
70 }
71 else{
72 fInputKind = 1 ;
73 }
74 fFileName = filename;
f3bd42f5 75 fctdb = 0 ;
76}
77//____________________________________________________________________________
e4778e0c 78AliPHOSCalibrManager::~AliPHOSCalibrManager()
f3bd42f5 79{
00953b03 80 //dtor
e4778e0c 81 if(fInputKind==0){
82 TFile * f = gROOT->GetFile(fFileName) ;
83 if(f && f->IsOpen())
84 f->Close() ;
85 }
f3bd42f5 86}
87//____________________________________________________________________________
88AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(void)
89{
00953b03 90 // gets the instance of the unique object
e4778e0c 91 return fgCaMa ;
f3bd42f5 92}
93//____________________________________________________________________________
e4778e0c 94AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(const char* filename,const char * kind)
f3bd42f5 95{
cd228525 96 // Opens source (now only root file) and
97 // returns the instance of the unique object
e4778e0c 98
f3bd42f5 99 if(!fgCaMa)
e4778e0c 100 fgCaMa = new AliPHOSCalibrManager(filename,kind) ;
f3bd42f5 101 else{
e4778e0c 102 if(strcmp(filename,fgCaMa->fFileName.Data())){
103 //Close previous file, construct new Manager
104 delete fgCaMa ;
105 fgCaMa = new AliPHOSCalibrManager(filename,kind) ;
106 }
f3bd42f5 107 }
108 return fgCaMa ;
109}
110//____________________________________________________________________________
e4778e0c 111void AliPHOSCalibrManager::GetParameters(AliPHOSCalibrationData &data){
112 if(fInputKind == 0){
113 ReadFromRoot(data) ;
114 }
115 else{
116 ReadFromASCII(data) ;
117 }
118}
119//____________________________________________________________________________
120void AliPHOSCalibrManager::ReadFromASCII(AliPHOSCalibrationData &data){
121 // We read pedestals and gains from *.dat file with following format:
122 // 0 0 0 0 37.09 1972. // next nmodrows*nmodcols*ncryrows*ncrycols lines
123 // 0 0 0 1 28.53 2072. // contains <RR CC r c ped peak>
124 // 0 0 0 2 30.93 1938. //
125 // where module is an array of 8*8 crystals and RR and CC are module raw and column position
f3bd42f5 126
127 if(!fctdb){
6f06e050 128 AliError(Form("Specify Connections Table Database first")) ;
f3bd42f5 129 return ;
130 }
e4778e0c 131
132 FILE * file = fopen(fFileName, "r");
133 if (!file) {
134 Error("ReadFromASCII", "could not open file %s", fFileName.Data());
135 return;
136 }
137
138 Bool_t isPed = kFALSE ;
139 if(strcmp(data.GetCategory(),"Pedestals")==0){
140 isPed = 1 ;
141 }
142 else{
143 if(strcmp(data.GetCategory(),"Gains")){
144 Error("ReadFromASCII","Unknown data category: %s",data.GetCategory()) ;
145 return ;
146 }
147 }
148
149 Int_t modRaw,modCol,raw,col;
150 Float_t ped,pik;
151 Int_t nread = 0 ;
152 while(fscanf(file,"%d %d %d %d %f %f",&modRaw,&modCol,&raw,&col,&ped,&pik)==6){
153 //Calculate plain crystal position:
154 Int_t rawPosition = (modRaw*8+raw)*fctdb->GetNColumns()+modCol*8+col ;
155 Int_t absId = fctdb->Raw2AbsId(rawPosition);
156 if(isPed){
157 data.SetData(absId,ped) ;
158 }
159 else{
160 if(pik!=0.)
161 data.SetData(absId,1./pik);
162 else
163 data.SetData(absId,0.);
164 }
165 nread++ ;
166 }
167 if(nread != fctdb->GetNColumns()*fctdb->GetNRaws()){
168 Error("ReadFromASCII","Read %d parameters instead of %d\n",nread,fctdb->GetNColumns()*fctdb->GetNRaws()) ;
f3bd42f5 169 }
e4778e0c 170 fclose(file) ;
171
f3bd42f5 172}
173//____________________________________________________________________________
e4778e0c 174void AliPHOSCalibrManager::ReadFromRoot(AliPHOSCalibrationData & data)
00953b03 175{
f3bd42f5 176 //reads calibration parameters from root file
e4778e0c 177
f3bd42f5 178 //construct name
179 TString searchname(data.GetCategory()) ;
180 searchname+='_' ;
181 searchname+=data.GetVersion() ;
f3bd42f5 182 TFile * file = gROOT->GetFile(fFileName) ;
183 if(!file || !file->IsOpen())
184 file = TFile::Open(fFileName) ;
185 if(!file->IsOpen()){
6f06e050 186 AliError(Form("Can not open file %s\n",fFileName.Data() )) ;
f3bd42f5 187 return ;
188 }
6f06e050 189
e4778e0c 190 AliPHOSCalibrationData * tmp = dynamic_cast<AliPHOSCalibrationData*>(file->Get(searchname) ) ;
191 if(!tmp)
192 Error("ReadFromRoot","there is no data %s in file %s",fFileName.Data(),fFileName.Data()) ;
193 else
194 data=(*tmp);
195
196
197// TList * list = file->GetListOfKeys() ;
198// TIter next(list) ;
199// TKey * key ;
200// while((key=((TKey*)next()))){
201// TString kname(key->GetName()) ;
202// if(kname.BeginsWith(searchname)){
203// TString sbegin = kname(searchname.Sizeof()-1,
204// kname.Last('_')-searchname.Sizeof()+1) ;
205// TString send = kname(kname.Last('_')+1,kname.Sizeof()) ;
206// Int_t begin,end ;
207// if(sscanf(sbegin.Data(),"%d",&begin) &&
208// sscanf(send.Data(),"%d",&end)){
209// if(begin<=run && end >=run){
210// data=(*dynamic_cast<AliPHOSCalibrationData*>(file->Get(key->GetName()) ));
211// return ;
212// }
213// }
214// }
215// }
216// Error("ReadFromRoot","Can not find key %s for run %d in file %s \n",searchname.Data(),run,fFileName.Data()) ;
f3bd42f5 217}
218//____________________________________________________________________________
e4778e0c 219void AliPHOSCalibrManager::WriteData(AliPHOSCalibrationData &data)
00953b03 220{
221 //Writes data
f3bd42f5 222 TFile * file = gROOT->GetFile(fFileName) ;
223 if(!file || !file->IsOpen()){
224 file = TFile::Open(fFileName,"UPDATE") ;
225 if(!file->IsOpen()){
6f06e050 226 AliError(Form("Can not open file %s\n",fFileName.Data() )) ;
f3bd42f5 227 return ;
228 }
229 }
230 file->cd() ;
e4778e0c 231 TString kname(data.GetCategory()) ;
f3bd42f5 232 kname+='_';
e4778e0c 233 kname+=data.GetVersion() ;
234// kname+='_';
235// Int_t begin,end ;
236// data->GetValidityRange(begin,end) ;
237// kname+=begin ;
238// kname+='_' ;
239// kname+=end ;
240 data.Write(kname,TObject::kOverwrite) ;
f3bd42f5 241
242}
243//____________________________________________________________________________
244AliPHOSCalibrManager& AliPHOSCalibrManager::operator=(AliPHOSCalibrManager const & cdb)
245{
00953b03 246 //overloads = operator
f3bd42f5 247 fFileName = cdb.fFileName;
248 return *this ;
249}