Change of module numbers for display purposes
[u/mrichter/AliRoot.git] / ITS / macrosSDD / StoreCalibSDD.C
CommitLineData
d5d0cab2 1#if !defined(__CINT__) || defined(__MAKECINT__)
2#include "AliCDBManager.h"
3#include "AliITSCalibrationSDD.h"
74999ed1 4#include "AliITSgeomTGeo.h"
d5d0cab2 5#include "AliCDBMetaData.h"
6#include "AliCDBStorage.h"
7#include "AliCDBId.h"
8#include "AliCDBPath.h"
9#include "AliCDBEntry.h"
10#include <TObjArray.h>
11#include <TRandom3.h>
12#endif
13
dd462f40 14void StoreCalibSDD(Int_t firstRun=0,Int_t lastRun=AliCDBRunRange::Infinity(), Bool_t isIdeal=kFALSE){
d5d0cab2 15 ///////////////////////////////////////////////////////////////////////
16 // Macro to generate and store the calibration files for SDD //
17 // Generates: //
18 // 1 file with 260 AliITSCalibrationSDD objects with //
19 // baselines, noise, gain, drift speed for each module (CalibSDD) //
d5d0cab2 20 ///////////////////////////////////////////////////////////////////////
21
22 if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
23 AliCDBManager::Instance()->SetDefaultStorage("local://OCDB");
24 }
25
26
10add0d4 27 AliCDBMetaData *md= new AliCDBMetaData(); // metaData describing the object
28 md->SetObjectClassName("TObjArray");
29 md->SetResponsible("Francesco Prino");
30 md->SetBeamPeriod(0);
31 md->SetComment("Simulated data");
d5d0cab2 32
33 AliCDBId idCalSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
10add0d4 34 TObjArray calSDD(260);
35 calSDD.SetOwner(kFALSE);
74999ed1 36
37 // BAD modules data
38 const Int_t nbadmod=6;
39 Int_t idBadMod[nbadmod];
40 idBadMod[0]=AliITSgeomTGeo::GetModuleIndex(3,4,1);
41 idBadMod[1]=AliITSgeomTGeo::GetModuleIndex(3,4,2);
42 idBadMod[2]=AliITSgeomTGeo::GetModuleIndex(3,4,3);
43 idBadMod[3]=AliITSgeomTGeo::GetModuleIndex(4,1,1);
44 idBadMod[4]=AliITSgeomTGeo::GetModuleIndex(4,12,1);
45 idBadMod[5]=AliITSgeomTGeo::GetModuleIndex(4,21,3);
46
47 // Modules with bad left side
48 const Int_t nbadleft=5;
49 Int_t idBadLeft[nbadleft];
50 idBadLeft[0]=AliITSgeomTGeo::GetModuleIndex(3,13,6);
51 idBadLeft[1]=AliITSgeomTGeo::GetModuleIndex(4,4,8);
52 idBadLeft[2]=AliITSgeomTGeo::GetModuleIndex(4,5,3);
53 idBadLeft[3]=AliITSgeomTGeo::GetModuleIndex(4,18,4);
54 idBadLeft[4]=AliITSgeomTGeo::GetModuleIndex(4,21,5);
55
56
57 // BAD anodes data
d5d0cab2 58 const Int_t nData = 209;
59 Int_t anodeUp[209] = {0,36,0,12,20,32,0,0,12,76,28,8,16,0,0,0,8,0,0,0,20,4,0,0,0,0,0,0
60 ,0,0,8,0,0,0,0,0,0,0,0,0,0,0,12,0,8,0,4,4,0,160,0,0,0,252,16,0,8,8
61 ,0,0,12,0,0,0,12,0,15,20,0,0,0,0,0,0,0,12,12,0,0,0,0,0,8,8,3,240,212
62 ,12,0,8,0,12,0,0,12,0,8,24,0,0,12,0,0,0,0,40,0,0,40,12,28,0,0,12,12
63 ,0,0,0,0,20,0,0,0,12,0,24,0,0,0,0,0,0,0,8,16,0,0,0,0,0,256,0,0,0,0,0,20
64 ,0,12,0,0,0,0,24,0,0,0,0,0,0,0,20,0,0,16,0,0,0,0,24,0,0,0,8,0,16,40,0
65 ,0,0,0,0,0,0,0,0,4,0,32,12,8,28,0,76,0,0,0,12,60,144,0,0,0,0,16,0,16,0,3 };
66
67 Int_t anodeDown[209] = {0,8,0,48,0,12,0,8,0,80,12,0,4,4,0,0,24,0,0,20,0,0,0,0,20,0,0,0,0,0,0,0
68 ,0,0,0,0,4,0,24,4,0,0,8,0,0,36,20,0,12,236,0,0,12,256,16,8,32,4,0,0,24
69 ,24,10,0,16,8,0,2,40,0,0,0,24,0,0,0,8,0,0,0,0,0,32,240,0,92,60,28,8,0,0
70 ,2,0,0,0,0,12,48,0,0,0,0,0,36,11,12,0,0,0,12,12,11,0,20,0,0,12,20,0,0,4
71 ,0,8,12,0,0,0,16,16,0,32,72,12,0,88,20,16,112,8,0,244,28,256,28,0,24,236
72 ,56,0,68,0,4,20,208,20,12,4,28,12,0,0,20,12,0,100,0,16,8,8,0,24,16,0,12,12
73 ,16,0,16,20,0,28,0,8,24,0,12,8,4,40,0,104,96,32,140,20,12,8,20,24,16,16,20
74 ,8,140,96,0,32,20,44};
75
d5d0cab2 76 TRandom3 *gran = new TRandom3();
77
78 for(Int_t mod=0;mod<260;mod++){
79 AliITSCalibrationSDD* resd = new AliITSCalibrationSDD("simulated");
374200ee 80 resd->SetAMAt20MHz();
d5d0cab2 81 Int_t nBadUp = 0;
82 Int_t nBadDown = 0;
83
84
85 // gain
86 for(Int_t iWing=0; iWing<2;iWing++){
87 for(Int_t iChip=0; iChip<4;iChip++){
88 Float_t chipgain=gran->Gaus(1.,0.1);
89 if(chipgain<0.1) chipgain=0.1;
90 for(Int_t iChan=0; iChan<64;iChan++){
91 Float_t gain=gran->Gaus(chipgain,0.01);
92 if(gain<0.1) gain=0.1;
eefec958 93 Int_t ian=resd->GetAnodeNumber(iWing,iChip,iChan);
94 resd->SetGain(ian,gain);
d5d0cab2 95 }
96 }
97 }
98
99 // bad channels
dd462f40 100 if(!isIdeal){
101 Int_t count=0;
102 do {
103 Int_t ranMod = Int_t(nData*gran->Uniform());
104 nBadUp = anodeUp[ranMod];
105 nBadDown = anodeDown[ranMod];
106 }while (nBadUp+nBadDown>25);
107
108 resd->SetDeadChannels(nBadDown+nBadUp);
109 Int_t remainingBad = nBadDown;
110 while (remainingBad>0) {
111 Int_t nBadChannel;
112 if (remainingBad<5) {
113 nBadChannel = remainingBad;
114 } else {
115 Int_t width = remainingBad-(5-1);
116 if (width>4) width = 4;
117 nBadChannel = 5 + Int_t(width*gran->Uniform());
118 }
d5d0cab2 119
120
dd462f40 121 Int_t iChannelPos = Int_t( (4*64-nBadChannel)*gran->Uniform() );
122 // Int_t iChip = iChannelPos/64;
123 // Int_t iChan = iChannelPos - iChip*64;
124 if(resd->IsBadChannel(iChannelPos)) continue;
125 Int_t *clus = new Int_t[nBadChannel];
126 Int_t ich = iChannelPos;
127 for(Int_t i=0;i<nBadChannel;i++){
128 clus[i]=ich;
129 ich++;
130 }
131
132 for(Int_t i=0;i<nBadChannel;i++){
133 if(resd->IsBadChannel(clus[i])) break;
134 resd->SetBadChannel(count,clus[i]);
135 count++;
136 }
137 remainingBad -= nBadChannel;
138 delete [] clus;
d5d0cab2 139 }
d5d0cab2 140
dd462f40 141 // May happen that, due to overlapping clusters, we
142 // have less bad channels than requested
143 // Let's put the remaining one per one ...
144 Int_t nSeToBad = 0;
145 for (Int_t i=0; i<4; i++){
146 for(Int_t j=0;j<64;j++){
147 Int_t ian=resd->GetAnodeNumber(0,i,j);
148 if (resd->GetChannelGain(ian)<0.0001) nSeToBad++;
149 }
d5d0cab2 150 }
dd462f40 151 while (nSeToBad<nBadDown) {
152 Int_t i = Int_t(4*64*gran->Uniform());
153 if(resd->IsBadChannel(i)==kFALSE){
154 resd->SetBadChannel(count,i);
155 count++;
156 nSeToBad++;
157 }
d5d0cab2 158 }
d5d0cab2 159
dd462f40 160 remainingBad = nBadUp;
161 while (remainingBad>0) {
162 Int_t nBadChannel;
163 if (remainingBad<5) {
164 nBadChannel = remainingBad;
165 } else {
166 Int_t width = remainingBad-(5-1);
167 if (width>4) width = 4;
168 nBadChannel = 5 + Int_t(width*gran->Uniform());
169 }
d5d0cab2 170
dd462f40 171 Int_t iChannelPos = Int_t( (4*64-nBadChannel)*gran->Uniform() );
172 // Int_t iChip = iChannelPos/64;
173 // Int_t iChan = iChannelPos - iChip*64;
174 if(resd->IsBadChannel(iChannelPos)) continue;
d5d0cab2 175
dd462f40 176 Int_t *clus = new Int_t[nBadChannel];
177 Int_t ich = iChannelPos;
178 for(Int_t i=0;i<nBadChannel;i++){
179 clus[i]=ich+256;
180 ich++;
181 }
182 for(Int_t i=0;i<nBadChannel;i++){
183
184 if(resd->IsBadChannel(clus[i])) break;
185 resd->SetBadChannel(count,clus[i]);
186 count++;
d5d0cab2 187
dd462f40 188 }
189 remainingBad -= nBadChannel;
190 delete [] clus;
d5d0cab2 191 }
d5d0cab2 192
dd462f40 193 nSeToBad = 0;
194 for (Int_t i=0; i<4; i++){
195 for(Int_t j=0;j<64;j++){
196 Int_t ian=resd->GetAnodeNumber(1,i,j);
197 if (resd->GetChannelGain(ian)<0.0001) nSeToBad++;
198 }
d5d0cab2 199 }
d5d0cab2 200
dd462f40 201 while (nSeToBad<nBadUp) {
202 Int_t i = Int_t(4*64*gran->Uniform());
203 if(resd->IsBadChannel(i+256)==kFALSE){
204 resd->SetBadChannel(count,i+256);
205 count++;
206 nSeToBad++;
207 }
d5d0cab2 208 }
209 }
d5d0cab2 210 //baselines
211 /*
212 for(Int_t nan=0;nan<512;nan++){
213
214 Int_t baseline = resd->GetBaseline(0);
215 Double_t noise = resd->GetNoiseAfterElectronics(0);
216 resd->SetBaseline(nan,baseline+(Int_t)gran->Gaus(0,4)); //baseline di def. e' 20, ho messo variazione di 4
217 resd->SetNoiseAfterElectronics(nan,noise+(Double_t)gran->Gaus(0,0.6));
218
219 }
220 */
221
74999ed1 222 Int_t modId=mod+240;
223 // Bad modules
dd462f40 224 if(!isIdeal){
225 for(Int_t ibadm=0; ibadm<nbadmod; ibadm++){
226 if(modId==idBadMod[ibadm]) resd->SetBad();
227 }
228 // Modules with bad left side
229 for(Int_t ibadl=0; ibadl<nbadleft; ibadl++){
230 if(modId==idBadLeft[ibadl]) for(Int_t ichip=0;ichip<4;ichip++) resd->SetChipBad(ichip);
231 }
74999ed1 232
dd462f40 233 // Modules with bad pascal chips
234 if( modId==AliITSgeomTGeo::GetModuleIndex(4,4,2) ){
235 resd->SetChipBad(0);
236 resd->SetChipBad(3);
237 }
74999ed1 238 }
10add0d4 239 calSDD.Add(resd);
d5d0cab2 240 printf("Added module %d\n",mod);
dd462f40 241 }
d5d0cab2 242
243 FILE* out = fopen("deadchannels.dat","w");
244 for(Int_t i=0;i<260;i++){
245 fprintf(out,"MODULE=%d\n",i);
10add0d4 246 AliITSCalibrationSDD* cl = (AliITSCalibrationSDD*)calSDD.At(i);
d5d0cab2 247 Int_t ndead=cl->GetDeadChannels();
248 fprintf(out,"n %d\n",ndead);
249 for(Int_t n=0;n<ndead;n++){
250 fprintf(out,"%d\n",cl->GetBadChannel(n));
251 }
252 }
253 fclose(out);
10add0d4 254 AliCDBManager::Instance()->GetDefaultStorage()->Put(&calSDD, idCalSDD, md);
d5d0cab2 255}