a97972c14194686486d9c279db49338048745c72
[u/mrichter/AliRoot.git] / ITS / StoreCalibSDD.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 #include "AliCDBManager.h"
3 #include "AliITSCalibrationSDD.h"
4 #include "AliITSresponseSDD.h"
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
14 void StoreCalibSDD(Int_t firstRun=0,Int_t lastRun=9999999, Int_t lastRunResp=999999999 ){
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) //
20   //  1 file with the AliITSrespionseSDD object (RespSDD)              //
21   ///////////////////////////////////////////////////////////////////////
22   
23   if(!AliCDBManager::Instance()->IsDefaultStorageSet()) {
24     AliCDBManager::Instance()->SetDefaultStorage("local://OCDB");
25   }
26   
27
28   AliCDBMetaData *md1= new AliCDBMetaData(); // metaData describing the object
29   md1->SetObjectClassName("AliITSCalibration");
30   md1->SetResponsible("Elisabetta Crescio, Francesco Prino");
31   md1->SetBeamPeriod(0);
32   md1->SetAliRootVersion("Head 20 nov. 2007"); //root version
33   md1->SetComment("This is a test");
34   //  TObject* str;
35   // md1->SetProperty("key1",str);
36
37   AliCDBMetaData *md2 = new AliCDBMetaData();
38   md2->SetObjectClassName("AliITSresponse");
39   md2->SetResponsible("Elisabetta Crescio, Francesco Prino");
40   md2->SetBeamPeriod(0);
41   md2->SetAliRootVersion("Head 20 nov. 2007"); //root version
42   md2->SetComment("This is a test");
43
44
45   AliCDBId idCalSDD("ITS/Calib/CalibSDD",firstRun, lastRun);
46   TObjArray respSDD(260);
47   respSDD.SetOwner(kFALSE);
48
49
50
51   AliCDBId idRespSDD("ITS/Calib/RespSDD",firstRun, lastRunResp);
52   AliITSresponseSDD* rd = new AliITSresponseSDD();
53   const Int_t nData = 209;
54   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
55                         ,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
56                         ,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
57                         ,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
58                         ,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
59                         ,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
60                         ,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 };
61
62   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
63                           ,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
64                           ,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
65                           ,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
66                           ,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
67                           ,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
68                           ,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
69                           ,8,140,96,0,32,20,44};
70
71   TRandom3 *gran = new TRandom3();
72   
73   for(Int_t mod=0;mod<260;mod++){
74     AliITSCalibrationSDD* resd = new AliITSCalibrationSDD("simulated");
75
76     Int_t nBadUp = 0;
77     Int_t nBadDown = 0;
78       
79
80     // gain
81     for(Int_t iWing=0; iWing<2;iWing++){
82       for(Int_t iChip=0; iChip<4;iChip++){
83         Float_t chipgain=gran->Gaus(1.,0.1);
84         if(chipgain<0.1) chipgain=0.1;
85         for(Int_t iChan=0; iChan<64;iChan++){
86           Float_t gain=gran->Gaus(chipgain,0.01);
87           if(gain<0.1) gain=0.1;
88           Int_t ian=resd->GetAnodeNumber(iWing,iChip,iChan);
89           resd->SetGain(ian,gain);
90         }
91       }
92     }
93
94     // bad channels
95     Int_t count=0;
96     do {
97       Int_t ranMod = Int_t(nData*gran->Uniform());
98       nBadUp   = anodeUp[ranMod];
99       nBadDown = anodeDown[ranMod];
100     }while (nBadUp+nBadDown>25);
101
102     resd->SetDeadChannels(nBadDown+nBadUp);
103     Int_t remainingBad = nBadDown;
104     while (remainingBad>0) {
105       Int_t nBadChannel;
106       if (remainingBad<5) {
107         nBadChannel = remainingBad;
108       } else {
109         Int_t width = remainingBad-(5-1);
110         if (width>4) width = 4;
111         nBadChannel = 5 + Int_t(width*gran->Uniform());
112       }
113         
114
115       Int_t iChannelPos = Int_t( (4*64-nBadChannel)*gran->Uniform() );
116       //      Int_t iChip = iChannelPos/64;
117       //      Int_t iChan = iChannelPos - iChip*64;
118       if(resd->IsBadChannel(iChannelPos)) continue;     
119       Int_t *clus = new Int_t[nBadChannel];
120       Int_t ich = iChannelPos;
121       for(Int_t i=0;i<nBadChannel;i++){
122         clus[i]=ich;
123         ich++;
124       }
125
126       for(Int_t i=0;i<nBadChannel;i++){
127         if(resd->IsBadChannel(clus[i])) break;
128         resd->SetBadChannel(count,clus[i]);
129         count++;          
130       }
131       remainingBad -= nBadChannel;
132       delete [] clus;
133     }
134
135     // May happen that, due to overlapping clusters, we
136     // have less bad channels than requested
137     // Let's put the remaining one per one ...
138     Int_t nSeToBad = 0;
139     for (Int_t i=0; i<4; i++){
140       for(Int_t j=0;j<64;j++){
141         Int_t ian=resd->GetAnodeNumber(0,i,j);
142         if (resd->GetChannelGain(ian)<0.0001) nSeToBad++;
143       }
144     }
145     while (nSeToBad<nBadDown) {
146       Int_t i = Int_t(4*64*gran->Uniform());
147       if(resd->IsBadChannel(i)==kFALSE){
148         resd->SetBadChannel(count,i);
149         count++;
150         nSeToBad++;
151       }
152     }
153       
154     remainingBad = nBadUp;
155     while (remainingBad>0) {
156       Int_t nBadChannel;
157       if (remainingBad<5) {
158         nBadChannel = remainingBad;
159       } else {
160         Int_t width = remainingBad-(5-1);
161         if (width>4) width = 4;
162         nBadChannel = 5 + Int_t(width*gran->Uniform());
163       }
164
165       Int_t iChannelPos = Int_t( (4*64-nBadChannel)*gran->Uniform() );
166       //      Int_t iChip = iChannelPos/64;
167       //      Int_t iChan = iChannelPos - iChip*64;
168       if(resd->IsBadChannel(iChannelPos)) continue;
169         
170       Int_t *clus = new Int_t[nBadChannel];
171       Int_t ich = iChannelPos;
172       for(Int_t i=0;i<nBadChannel;i++){
173         clus[i]=ich+256;
174         ich++;
175       }
176       for(Int_t i=0;i<nBadChannel;i++){
177         
178         if(resd->IsBadChannel(clus[i])) break;  
179         resd->SetBadChannel(count,clus[i]);
180         count++;
181           
182       }
183       remainingBad -= nBadChannel;
184       delete [] clus;
185     }
186       
187     nSeToBad = 0;
188     for (Int_t i=0; i<4; i++){
189       for(Int_t j=0;j<64;j++){
190         Int_t ian=resd->GetAnodeNumber(1,i,j);
191         if (resd->GetChannelGain(ian)<0.0001) nSeToBad++;
192       }
193     }
194
195     while (nSeToBad<nBadUp) {
196       Int_t i = Int_t(4*64*gran->Uniform());
197       if(resd->IsBadChannel(i+256)==kFALSE){
198         resd->SetBadChannel(count,i+256);
199         count++;
200         nSeToBad++;
201       }
202     }
203       
204     //baselines
205     /*
206       for(Int_t nan=0;nan<512;nan++){
207         
208       Int_t baseline = resd->GetBaseline(0);
209       Double_t noise = resd->GetNoiseAfterElectronics(0);
210       resd->SetBaseline(nan,baseline+(Int_t)gran->Gaus(0,4)); //baseline di def. e' 20, ho messo variazione di 4
211       resd->SetNoiseAfterElectronics(nan,noise+(Double_t)gran->Gaus(0,0.6));              
212                           
213       }
214     */
215
216
217     if(mod==88) resd->SetBad();
218     if(mod==202) for(Int_t ichip=0;ichip<4;ichip++) resd->SetChipBad(ichip);
219     respSDD.Add(resd);
220     printf("Added module %d\n",mod);
221   }
222     
223   FILE* out = fopen("deadchannels.dat","w");
224   for(Int_t i=0;i<260;i++){
225     fprintf(out,"MODULE=%d\n",i);
226     AliITSCalibrationSDD* cl = (AliITSCalibrationSDD*)respSDD.At(i);
227     Int_t ndead=cl->GetDeadChannels();
228     fprintf(out,"n %d\n",ndead);
229     for(Int_t n=0;n<ndead;n++){
230       fprintf(out,"%d\n",cl->GetBadChannel(n));
231     }   
232   }
233   fclose(out);
234   AliCDBManager::Instance()->GetDefaultStorage()->Put(&respSDD, idCalSDD, md1);   
235   AliCDBManager::Instance()->GetDefaultStorage()->Put(rd, idRespSDD, md2);  
236 }