]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSCalibrManager.cxx
Removing warnings (icc), adding more detailed description
[u/mrichter/AliRoot.git] / PHOS / AliPHOSCalibrManager.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 /* $Id$ */
17
18 //_________________________________________________________________________
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) ;
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 ---
45 #include <fstream> 
46
47 // --- AliRoot header files ---
48 #include "AliPHOSConTableDB.h"
49 #include "AliPHOSCalibrManager.h"
50 #include "AliPHOSCalibrationData.h"
51 ClassImp(AliPHOSCalibrManager)
52
53
54 AliPHOSCalibrManager * AliPHOSCalibrManager::fgCaMa = 0 ;
55 //____________________________________________________________________________ 
56 AliPHOSCalibrManager::AliPHOSCalibrManager():TNamed() 
57 {
58   // default ctor: not to be used
59   fctdb = 0 ;
60   fFileName="" ;
61   Fatal("Default constructor","Should not use") ;
62 }
63 //____________________________________________________________________________ 
64 AliPHOSCalibrManager::AliPHOSCalibrManager(const char* filename ):
65   TNamed("AliPHOSCalibrManager",filename){
66   fFileName = filename ;          
67   fctdb = 0 ;
68
69 //____________________________________________________________________________ 
70   AliPHOSCalibrManager::~AliPHOSCalibrManager()
71 {
72   //dtor
73   TFile * f = gROOT->GetFile(fFileName) ;
74   if(f && f->IsOpen())
75     f->Close() ;
76   if(fctdb)
77     delete fctdb ;
78   fctdb = 0 ;
79 }
80 //____________________________________________________________________________ 
81 AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(void)
82
83   // gets the instance of the unique object
84  return fgCaMa ;
85 }
86 //____________________________________________________________________________ 
87 AliPHOSCalibrManager * AliPHOSCalibrManager::GetInstance(const char* filename )
88 {
89   // Opens source (now only root file) and
90   // returns the instance of the unique object
91
92   if(!fgCaMa)
93     fgCaMa = new AliPHOSCalibrManager(filename) ;
94   else{
95    if(strcmp(filename,fgCaMa->fFileName.Data())){
96    //Close previous file, construct new Manager
97      delete fgCaMa ;
98      fgCaMa = new AliPHOSCalibrManager(filename) ;
99    }
100   }
101   return fgCaMa ;        
102 }
103 //____________________________________________________________________________
104 void AliPHOSCalibrManager::ReadFromASCII(AliPHOSCalibrationData &data,const char * filename)
105 {
106   //reads calibration parameters from ascii file
107
108   if(!fctdb){
109     Error("ReadCalibrationParameters", "Specify Connections Table Database first") ;
110     return ;
111   }
112   ifstream file(filename) ; 
113   for(Int_t i = 1; i<=64; i++){
114     Float_t inp ;
115     file >> inp  ;      
116     data.SetData(fctdb->Raw2AbsId(i),static_cast<Float_t>(inp));
117   }
118   file.close();   
119 }
120 //____________________________________________________________________________
121 void AliPHOSCalibrManager::ReadFromRoot(AliPHOSCalibrationData & data,Int_t run)
122 {
123   //reads calibration parameters from root file
124
125   //construct name
126   TString searchname(data.GetCategory()) ;
127   searchname+='_' ;
128   searchname+=data.GetVersion() ;
129   searchname+='_' ;
130   TFile * file = gROOT->GetFile(fFileName) ;
131   if(!file || !file->IsOpen())
132     file = TFile::Open(fFileName) ;
133   if(!file->IsOpen()){
134     Error("ReadFromRoot","Can not open file %s\n",fFileName.Data() ) ;
135     return ;
136   }
137   TList * list = file->GetListOfKeys() ;
138   TIter next(list) ;
139   TKey * key ;
140   while((key=((TKey*)next()))){
141     TString kname(key->GetName()) ;
142     if(kname.BeginsWith(searchname)){
143       TString sbegin = kname(searchname.Sizeof()-1,
144                              kname.Last('_')-searchname.Sizeof()+1) ;
145       TString send   = kname(kname.Last('_')+1,kname.Sizeof()) ;
146       Int_t begin,end ;
147       if(sscanf(sbegin.Data(),"%d",&begin) && 
148          sscanf(send.Data(),"%d",&end)){
149         if(begin<=run && end >=run){
150           data=(*dynamic_cast<AliPHOSCalibrationData*>(file->Get(key->GetName()) ));
151           return ;
152         }
153       }
154     }
155   }
156   Error("ReadFromRoot","Can not find key %s for run %d in file %s \n",searchname.Data(),run,fFileName.Data()) ;
157 }
158 //____________________________________________________________________________
159 void AliPHOSCalibrManager::WriteData(AliPHOSCalibrationData * data)
160 {
161   //Writes data
162   TFile * file = gROOT->GetFile(fFileName) ;
163   if(!file || !file->IsOpen()){
164     file = TFile::Open(fFileName,"UPDATE") ;
165     if(!file->IsOpen()){
166       Error("WriteData","Can not open file %s\n",fFileName.Data() ) ;
167       return ;
168      }
169   }
170   file->cd() ;
171   TString kname(data->GetCategory()) ;
172   kname+='_';
173   kname+=data->GetVersion() ;
174   kname+='_';
175   Int_t begin,end ;
176   data->GetValidityRange(begin,end) ;
177   kname+=begin ;
178   kname+='_' ;
179   kname+=end ;
180   data->Write(kname,TObject::kOverwrite) ;
181   
182 }
183 //____________________________________________________________________________
184 AliPHOSCalibrManager& AliPHOSCalibrManager::operator=(AliPHOSCalibrManager const & cdb)
185 {
186   //overloads = operator
187   fFileName = cdb.fFileName;
188   return *this ;
189 }