fix bugs add new features
[u/mrichter/AliRoot.git] / ITS / UpdateSSDGainOCDB.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 #include <Riostream.h>
3 #include <TFile.h>
4 #include <TString.H>
5 #include "AliITSGainSSDv2.h"
6 #include "AliCDBEntry.h"
7 #include "AliCDBManager.h"
8 #include "AliCDBMetaData.h"
9 #endif
10 Int_t GainCalibration(AliCDBManager * man, Float_t* gainP, Float_t* gainN) 
11 {
12         const Int_t fgkSSDMODULES = 1698;
13         static const Int_t fgkDefaultNStripsSSD = 768;
14         AliITSGainSSDv2 *gainSSD = new AliITSGainSSDv2();
15         AliCDBEntry *entryGainSSD = man->Get("ITS/Calib/GainSSD");
16         TObject *empty = (TObject *)entryGainSSD->GetObject();
17         TString objectname = empty->GetName();
18         if(objectname=="AliITSGainSSDv2") 
19         {
20                 cout<<"Reading the new format of the calibration file..."<<endl;
21                 gainSSD = (AliITSGainSSDv2 *)entryGainSSD->GetObject();
22         }
23         else
24         {
25                 cout<<"Now OCDB object"<<endl;
26                 return 0 ;
27         }
28         for (Int_t i = 0; i < fgkSSDMODULES; i++) 
29         {
30                         //for (Int_t i = 0; i < 1; i++) {
31                         //AliITSgeomTGeo::GetModuleId(i+500,layer,ladder,module);
32                 for(Int_t j = 0; j < fgkDefaultNStripsSSD; j++) 
33                 {
34                         gainP[i*fgkDefaultNStripsSSD+j] = gainSSD->GetGainP(i,j);
35                         gainN[i*fgkDefaultNStripsSSD+j]= gainSSD->GetGainN(i,j);
36         
37                 }//strip loop
38         }//module loop
39         return 1;
40 }
41
42
43 void UpdateSSDGainOCDB(TString gainfile,Float_t globalMPV=84.0,const char* type = "alien", Int_t runNumber = 0, Int_t runRangemin=0 ,Int_t runRangemax= AliCDBRunRange::Infinity()) 
44 {
45         if((runRangemax<runRangemin))
46         {
47                 cout<<"wrong run range "<<endl;
48                 return;
49         }       
50         const Int_t fgkPNStripsPerModule = 768;
51         const Int_t fgkNumberOfSSDModules = 1698;
52         Float_t gainPold[fgkPNStripsPerModule* fgkNumberOfSSDModules];
53         Float_t gainNold[fgkPNStripsPerModule* fgkNumberOfSSDModules];
54         
55         for(int i=0;i<fgkPNStripsPerModule* fgkNumberOfSSDModules;i++)
56         {
57                         gainPold[i]=0.0;
58                         gainNold[i]=0.0;
59         }
60         
61         TString gType = type;
62         AliCDBManager * man = AliCDBManager::Instance();
63         if(gType == "alien") 
64                  man->SetDefaultStorage("alien://folder=/alice/data/2010/OCDB/");
65         else if(gType == "local") 
66                 man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
67         else 
68         {
69                 cout<<"Allowed types: local or alien!!!"<<endl;
70                 return;
71         }
72           man->SetRun(runNumber);
73         if(!GainCalibration(man,gainPold,gainNold))
74                 return;
75                 
76         char buffer[256];
77         ifstream ingainfile(gainfile);
78         if(!ingainfile.good())
79                 return;
80         
81         
82          ingainfile.getline(buffer,256);
83         cout<<buffer<<endl;
84         
85         
86         Int_t module=0;
87         Int_t gainflag=0;
88         Float_t gainP=0;
89         Float_t gainN=0;
90         Float_t fMPV=0;
91         Float_t corrP[1698];
92         Float_t corrN[1698];
93         
94         for(int jj=0;jj<1698;jj++)
95         {
96                 ingainfile>>module;
97                 ingainfile>>gainflag;
98                 ingainfile>>gainP;
99                 ingainfile>>gainN;
100                 ingainfile>>fMPV;
101                 if(gainflag==1)
102                 {
103                         corrP[module]=gainP*globalMPV/fMPV;
104                         corrN[module]=gainN*globalMPV/fMPV;
105                         
106                 }
107                 else
108                 {
109                         corrP[module]=1.0;
110                         corrN[module]=1.0;
111                 }
112                         
113         }
114         
115         
116         TObjArray *array = new TObjArray();
117         array->SetName("Gain");
118         AliITSGainSSDv2  *mc;
119         mc = new AliITSGainSSDv2();
120         for(Int_t mind = 0; mind < fgkNumberOfSSDModules; mind++) 
121         {
122                 for (Int_t i = 0; i < fgkPNStripsPerModule; i++) 
123                 {
124                         mc->AddGainP(mind,i, gainPold[mind* fgkPNStripsPerModule+i]*corrP[mind]);
125                         mc->AddGainN(mind,i,gainNold[mind* fgkPNStripsPerModule+i]*corrN[mind]);                        
126                 }
127         }
128
129         man = AliCDBManager::Instance();
130         man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
131         man->SetRun(0);
132         //AliCDBId id("ITS/Calib/GainSSD",0,AliCDBRunRange::Infinity());
133         AliCDBId id("ITS/Calib/GainSSD",runRangemin,runRangemax);
134         AliCDBMetaData *metadata= new AliCDBMetaData();
135         metadata->SetResponsible("Marek.Chojnacki@cern.ch");
136         metadata->SetComment("Default values for the SSD gain calibration");
137         //man->Put(array,id,metadata);
138         man->Put(mc,id,metadata);
139          TFile* fout=TFile::Open("AliITSGainSSD_new.root","recreate");
140         mc ->Write();   
141                 fout->Close();
142         
143 }