]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PHOS/AliPHOSCalibrManager.cxx
Corrections to comply with coding conventions
[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//_________________________________________________________________________
19// Short description
20//
21//*-- Author : D.Peressounko (RRC KI & SUBATECH)
22//////////////////////////////////////////////////////////////////////////////
23
24
25// --- ROOT system ---
26#include "TROOT.h"
27#include "TKey.h"
28#include "TFile.h"
29
30// --- Standard library ---
31#include <fstream>
32
33// --- AliRoot header files ---
34#include "AliPHOSConTableDB.h"
35#include "AliPHOSCalibrManager.h"
36#include "AliPHOSCalibrationData.h"
37ClassImp(AliPHOSCalibrManager)
38
39
40AliPHOSCalibrManager * AliPHOSCalibrManager::fgCaMa = 0 ;
41//____________________________________________________________________________
42AliPHOSCalibrManager::AliPHOSCalibrManager():TNamed()
43{
44 fctdb = 0 ;
45 fFileName="" ;
46 Fatal("Default constructor","Should not use") ;
47}
48//____________________________________________________________________________
49AliPHOSCalibrManager::AliPHOSCalibrManager(const char* filename ):
50 TNamed("AliPHOSCalibrManager",filename){
51 fFileName = filename ;
52 fctdb = 0 ;
53}
54//____________________________________________________________________________
55 AliPHOSCalibrManager::~AliPHOSCalibrManager()
56{
57 TFile * f = gROOT->GetFile(fFileName) ;
58 if(f && f->IsOpen())
59 f->Close() ;
60 if(fctdb)
61 delete fctdb ;
62 fctdb = 0 ;
63}
64//____________________________________________________________________________
65AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(void)
66{
67 return fgCaMa ;
68}
69//____________________________________________________________________________
70AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(const char* filename )
71{
72 if(!fgCaMa)
73 fgCaMa = new AliPHOSCalibrManager(filename) ;
74 else{
75 if(strcmp(filename,fgCaMa->fFileName.Data())){
76 //Close previous file, construct new Manager
77 delete fgCaMa ;
78 fgCaMa = new AliPHOSCalibrManager(filename) ;
79 }
80 }
81 return fgCaMa ;
82}
83//____________________________________________________________________________
84void AliPHOSCalibrManager::ReadFromASCII(AliPHOSCalibrationData &data,const char * filename){
85 //reads calibration parameters from ascii file
86
87 if(!fctdb){
88 Error("ReadCalibrationParameters", "Specify Connections Table Database first") ;
89 return ;
90 }
91 ifstream file(filename) ;
92 for(Int_t i = 1; i<=64; i++){
93 Float_t inp ;
94 file >> inp ;
95 data.SetData(fctdb->Raw2AbsId(i),static_cast<Float_t>(inp));
96 }
97 file.close();
98}
99//____________________________________________________________________________
100void AliPHOSCalibrManager::ReadFromRoot(AliPHOSCalibrationData & data,Int_t run){
101 //reads calibration parameters from root file
102
103 //construct name
104 TString searchname(data.GetCategory()) ;
105 searchname+='_' ;
106 searchname+=data.GetVersion() ;
107 searchname+='_' ;
108 TFile * file = gROOT->GetFile(fFileName) ;
109 if(!file || !file->IsOpen())
110 file = TFile::Open(fFileName) ;
111 if(!file->IsOpen()){
112 Error("ReadFromRoot","Can not open file %s\n",fFileName.Data() ) ;
113 return ;
114 }
115 TList * list = file->GetListOfKeys() ;
116 TIter next(list) ;
117 TKey * key ;
118 while((key=((TKey*)next()))){
119 TString kname(key->GetName()) ;
120 if(kname.BeginsWith(searchname)){
121 TString sbegin = kname(searchname.Sizeof()-1,
122 kname.Last('_')-searchname.Sizeof()+1) ;
123 TString send = kname(kname.Last('_')+1,kname.Sizeof()) ;
124 Int_t begin,end ;
125 if(sscanf(sbegin.Data(),"%d",&begin) &&
126 sscanf(send.Data(),"%d",&end)){
127 if(begin<=run && end >=run){
128 data=(*dynamic_cast<AliPHOSCalibrationData*>(file->Get(key->GetName()) ));
129 return ;
130 }
131 }
132 }
133 }
134 Error("ReadFromRoot","Can not find key %s for run %d in file %s \n",searchname.Data(),run,fFileName.Data()) ;
135}
136//____________________________________________________________________________
137void AliPHOSCalibrManager::WriteData(AliPHOSCalibrationData * data){
138 TFile * file = gROOT->GetFile(fFileName) ;
139 if(!file || !file->IsOpen()){
140 file = TFile::Open(fFileName,"UPDATE") ;
141 if(!file->IsOpen()){
142 Error("WriteData","Can not open file %s\n",fFileName.Data() ) ;
143 return ;
144 }
145 }
146 file->cd() ;
147 TString kname(data->GetCategory()) ;
148 kname+='_';
149 kname+=data->GetVersion() ;
150 kname+='_';
151 Int_t begin,end ;
152 data->GetValidityRange(begin,end) ;
153 kname+=begin ;
154 kname+='_' ;
155 kname+=end ;
156 data->Write(kname,TObject::kOverwrite) ;
157
158}
159//____________________________________________________________________________
160AliPHOSCalibrManager& AliPHOSCalibrManager::operator=(AliPHOSCalibrManager const & cdb)
161{
162 //
163 fFileName = cdb.fFileName;
164 return *this ;
165}