source: trunk/MagicSoft/Simulation/Detector/StarResponse/MStarLight.cxx@ 11169

Last change on this file since 11169 was 5408, checked in by moralejo, 20 years ago
*** empty log message ***
File size: 10.3 KB
Line 
1#include "MStarLight.hxx"
2
3using namespace std;
4
5MStarLight::MStarLight(Float_t fadc_slices_per_ns, Int_t response_slices_fadc) {
6 //
7 // default constructor
8 //
9
10 fBrightness = 0.;
11 fTimeRange = TIMERANGE;
12
13 fFadcSlicesPerNanosec = fadc_slices_per_ns; // "real" number of FADC slices per ns
14
15 fResponseSlicesFadc = response_slices_fadc;
16 // total number of bins in the histogram containing the response of the FADC to a
17 // single photoelectron. The bins are narrower than the true FADC slices by a factor
18 // equal to SUBBINS (see MFadcDefine.h)
19
20
21 fBinsTrig = (Int_t)(TRIG_SLICES_PER_NSEC*fTimeRange); // Default value 4*10000=40000
22 fTrigShape = 0;
23 fAmplTrig = 0.;
24 fFwhmTrig = 0.;
25
26 fBinsFadc = (Int_t)(fFadcSlicesPerNanosec*fTimeRange); // Default value 0.3*10000=3000
27 fFadcShape = 0;
28 fIntegFadc = 0.;
29 fFwhmFadc = 0.;
30
31 fGainFluctuations = 1;
32
33 fTrig = new Float_t[fBinsTrig];
34 fTrigResp = new Float_t[RESPONSE_SLICES_TRIG];
35 fFadc = new Float_t[fBinsFadc];
36 fFadcResp = new Float_t[fResponseSlicesFadc];
37
38 for (Int_t i= 0; i< fBinsTrig ; i++)
39 fTrig[i] = 0. ;
40
41 for (Int_t i= 0; i < fBinsFadc; i++)
42 fFadc[i] = 0.;
43
44 for (Int_t i = 0; i < RESPONSE_SLICES_TRIG; i++)
45 fTrigResp[i] = 0.;
46
47 for (Int_t i = 0; i < fResponseSlicesFadc; i++)
48 fFadcResp[i] = 0.;
49}
50
51void MStarLight::Reset() {
52
53 fBrightness = 0. ;
54
55 fTrigShape = 0;
56 fAmplTrig = 0.;
57 fFwhmTrig = 0.;
58
59 fFadcShape = 0;
60 fIntegFadc = 0.;
61 fFwhmFadc = 0.;
62
63 fGainFluctuations = 1;
64
65 for (Int_t i= 0; i < fBinsTrig ; i++)
66 fTrig[i] = 0.;
67
68 for (Int_t i= 0; i < fBinsFadc; i++)
69 fFadc[i] = 0.;
70
71 for (Int_t i = 0; i < RESPONSE_SLICES_TRIG; i++)
72 fTrigResp[i] = 0.;
73
74 for (Int_t i = 0; i < fResponseSlicesFadc; i++)
75 fFadcResp[i] = 0.;
76}
77
78Float_t MStarLight::GetBrightness ()
79{
80 return fBrightness;
81}
82
83void MStarLight::SetBrightness (Float_t in )
84{
85 fBrightness = in;
86}
87
88Float_t MStarLight::GetAmplTrig ()
89{
90 return fAmplTrig ;
91}
92
93void MStarLight::SetAmplTrig (Float_t in )
94{
95 fAmplTrig = in;
96}
97
98Float_t MStarLight::GetFwhmTrig ()
99{
100 return fFwhmTrig;
101}
102
103void MStarLight::SetFwhmTrig (Float_t in )
104{
105 fFwhmTrig = in;
106}
107
108void MStarLight::SetFadcSlicesPerNanosec (Float_t in)
109{
110 fFadcSlicesPerNanosec = in;
111 fBinsFadc = (Int_t)(fFadcSlicesPerNanosec*fTimeRange);
112
113 if (fFadc)
114 delete [] fFadc;
115
116 fFadc = new Float_t[fBinsFadc];
117
118 for (Int_t i= 0; i < fBinsFadc; i++)
119 fFadc[i] = 0.;
120}
121
122Float_t MStarLight::GetIntegFadc ()
123{
124 return fIntegFadc;
125}
126
127void MStarLight::SetIntegFadc (Float_t in )
128{
129 fIntegFadc = in;
130}
131
132Float_t MStarLight::GetFwhmFadc ()
133{
134 return fFwhmFadc;
135}
136
137void MStarLight::SetFwhmFadc (Float_t in )
138{
139 fFwhmFadc = in;
140}
141
142
143void MStarLight::SetTrigResponse( Float_t *in )
144{
145 for (Int_t i = 0; i < RESPONSE_SLICES_TRIG; i++)
146 fTrigResp[i] = in[i];
147}
148
149void MStarLight::SetFadcResponse( Float_t *in )
150{
151 for (Int_t i = 0; i < fResponseSlicesFadc; i++)
152 fFadcResp[i] = in[i];
153}
154
155void MStarLight::FillResponse( Float_t ampl, Float_t time )
156{
157 // fill the trigger response
158
159 Int_t startbin = (Int_t) (time * ((Float_t)fBinsTrig/fTimeRange));
160
161 Int_t icount = 0;
162 Int_t idata;
163
164 for ( Int_t i = startbin ; i < startbin+RESPONSE_SLICES_TRIG ; i++)
165 {
166 if ( i < fBinsTrig )
167 idata = i;
168 else if ( i >= fBinsTrig )
169 idata = i - fBinsTrig;
170
171 fTrig[idata] = fTrig[idata] + ampl * fTrigResp[icount];
172 icount++ ;
173 }
174
175 //
176 // fill the FADC content
177 //
178
179 startbin = (Int_t) ( time * ((Float_t)(fBinsFadc*SUBBINS)/fTimeRange));
180
181 Int_t ichanfadc = 0 ;
182
183 //
184 // putting the response slices in the right sig slices.
185 // Be careful, because both slices have different widths.
186 //
187
188 //
189 // Changed, Jan 2004, A. Moralejo: now the FADC does not integrate all
190 // the signal within each FADC slice, but measures just the signal height
191 // at one point, like the real FADC does. By default, each FADC slice
192 // contains SUBBINS bins of the response histogram
193 // (fFadcResp). Warning: do not change this unless you do the corresponding
194 // modifications also in MFadc.cxx, or the signal and the noise photoelectrons
195 // will be treated differently!!
196 //
197
198 for ( Int_t i = 0 ; i < fResponseSlicesFadc; i += SUBBINS )
199 {
200 ichanfadc = (Int_t) ((startbin+i)/(Float_t)SUBBINS);
201
202 if ( ichanfadc < fBinsFadc )
203 idata = ichanfadc;
204 else if ( ichanfadc >= fBinsFadc )
205 idata = ichanfadc-fBinsFadc;
206
207 fFadc[idata] += (ampl * fFadcResp[i]);
208 }
209}
210
211void MStarLight::ElecNoise ( Float_t noiseTrig, Float_t noiseFadc ) {
212 //
213 // putting some noise to the baseline
214 //
215
216 TRandom2 wuerfel( (UInt_t) (noiseTrig*100) ) ;
217
218 for (Int_t i=0; i< fBinsTrig ; i++ )
219 fTrig[i] += wuerfel.Gaus(0., noiseTrig );
220
221 for (Int_t i=0; i< fBinsFadc ; i++ )
222 fFadc[i] += wuerfel.Gaus(0., noiseFadc );
223
224}
225
226Float_t MStarLight::GetTrig( Int_t i)
227{
228 //------------------------------------------------------------------
229 //
230 // It gets the value of the simulated trigger in the i bin
231 //
232 return fTrig[i];
233}
234
235Float_t MStarLight::GetFadc( Int_t i)
236{
237 //------------------------------------------------------------------
238 //
239 // It gets the value of the simulated FADC signal in the i bin
240 //
241 return fFadc[i];
242}
243
244void MStarLight::StoreHisto( char *filename)
245{
246
247 Float_t baseline = 0.;
248
249 // first the histograms for trigger
250 //
251 // the calculated trigger signal before baseline
252 //
253
254 TH1F trigresp ("trigresp", "Trigger Response", fBinsTrig, 0., fTimeRange);
255
256 for (Int_t i=0; i< fBinsTrig ; i++ )
257 {
258 trigresp.SetBinContent(i+1, fTrig[i]);
259 baseline += fTrig[i];
260 }
261
262 baseline /= fBinsTrig;
263
264 TH1F trigbase ("trigbase", "Response after Baseline shift",
265 fBinsTrig, 0., fTimeRange) ;
266
267 for (Int_t i = 0; i < fBinsTrig ; i++)
268 trigbase.SetBinContent(i+1, fTrig[i]-baseline );
269
270 TH1F trigdist ("trigdist", "Noise on the baseline",
271 1000, -25., 25. );
272
273 for (Int_t i = 0; i < fBinsTrig ; i++)
274 trigdist.Fill( (Float_t) trigbase.GetBinContent(i+1) );
275
276
277 //
278 // Now the histograms for the fadc
279 //
280
281 TH1F fadcresp ("fadcresp", "Fadc Response", fBinsFadc, 0., fTimeRange);
282
283 baseline = 0.;
284 for (Int_t i=0; i < fBinsFadc; i++)
285 {
286 fadcresp.SetBinContent(i+1, fFadc[i]);
287 baseline += fFadc[i];
288 }
289
290 baseline /= fBinsFadc;
291
292 TH1F fadcbase ("fadcbase", "Fadc after Baseline shift",
293 fBinsFadc, 0., fTimeRange) ;
294
295 for (Int_t i=0; i< fBinsFadc ; i++ )
296 fadcbase.SetBinContent(i+1, fFadc[i]-baseline );
297
298 TH1F fadcdist ("fadcdist", "Noise on fadc",
299 1000, -100., 100. ) ;
300
301 for (Int_t i=0; i< fBinsFadc ; i++ )
302 fadcdist.Fill( (Float_t) fadcbase.GetBinContent(i+1) );
303
304 TFile outfile( filename, "RECREATE");
305
306 trigresp.Write();
307 trigbase.Write();
308 trigdist.Write();
309
310 fadcresp.Write();
311 fadcbase.Write();
312 fadcdist.Write();
313
314
315 outfile.Close();
316}
317
318void MStarLight::WriteBinary( char *filename)
319{
320 //
321 // write the information to the binary file
322
323 FILE *datei ;
324
325 datei = fopen ( filename, "w" ) ;
326
327 if ( ! datei )
328 {
329 cout << " ERROR: Can't open the file " << filename << endl;
330 exit (230);
331 }
332
333 Float_t version = VERSIONSR;
334
335 // write them out
336
337 fwrite ( &version, sizeof(Float_t), 1, datei );
338 fwrite ( &fBrightness, sizeof(Float_t), 1, datei );
339 fwrite ( &fTimeRange, sizeof(Float_t), 1, datei );
340 fwrite ( &fFadcSlicesPerNanosec, sizeof(Float_t), 1, datei );
341
342 fwrite ( &fBinsTrig , sizeof(Int_t), 1, datei );
343 fwrite ( &fTrigShape , sizeof(Int_t), 1, datei );
344 fwrite ( &fAmplTrig , sizeof(Float_t), 1, datei );
345 fwrite ( &fFwhmTrig , sizeof(Float_t), 1, datei );
346
347 fwrite ( &fBinsFadc , sizeof(Int_t), 1, datei );
348 fwrite ( &fFadcShape , sizeof(Int_t), 1, datei );
349 fwrite ( &fIntegFadc , sizeof(Float_t), 1, datei );
350 fwrite ( &fFwhmFadc , sizeof(Float_t), 1, datei );
351
352 fwrite ( &fGainFluctuations, sizeof(Int_t), 1, datei );
353
354 fwrite (fTrig, sizeof(Float_t), fBinsTrig, datei);
355
356 // We want to store the FADC signal taking into account the AC
357 // coupling
358 //
359 // We calculate and substract the baseline
360 //
361
362 Float_t baseline = 0.;
363
364 for (Int_t i=0; i< fBinsFadc ; i++ )
365 baseline += fFadc[i];
366
367 baseline /= fBinsFadc;
368
369 Float_t *temp = new Float_t[fBinsFadc];
370
371 for (Int_t i=0; i < fBinsFadc; i++ )
372 temp[i] = fFadc[i]-baseline;
373
374 fwrite (temp, sizeof(Float_t), fBinsFadc, datei);
375
376 delete [] temp;
377
378 fclose ( datei );
379
380}
381
382void MStarLight::ReadBinary( char *filename) {
383
384 //
385 // read the things from the binary file
386
387 FILE *datei ;
388
389 datei = fopen ( filename, "r" ) ;
390
391 if ( ! datei ) {
392 cout << " ERROR: Can't open the file " << filename
393 << endl ;
394 cout<< " The database for the NSB may be too small. "
395 << endl;
396 cout<< " See the How to Use of the Camera simulation for more information"
397 << endl;
398 exit (230) ;
399 }
400
401 Float_t read_version;
402 Float_t current_version = VERSIONSR;
403
404 // Check that we read the right version of the Database
405 fread ( &read_version, sizeof(Float_t), 1, datei ) ;
406
407 if(Int_t(read_version)!=Int_t(current_version)){
408 cout<<" ERROR: You are trying to read database VERSION "<<
409 read_version << endl;
410 cout<<" You must generate a database for the current VERSION "<<
411 current_version << endl;
412 cout<<" See the NSB database section in the Camera How to Use note."
413 <<endl;
414 exit (230);
415 }
416
417 fread ( &fBrightness, sizeof(Float_t), 1, datei );
418 fread ( &fTimeRange , sizeof(Float_t), 1, datei );
419 fread ( &fFadcSlicesPerNanosec, sizeof(Float_t), 1, datei );
420
421 fread ( &fBinsTrig , sizeof(Int_t), 1, datei );
422 fread ( &fTrigShape , sizeof(Int_t), 1, datei );
423 fread ( &fAmplTrig , sizeof(Float_t), 1, datei );
424 fread ( &fFwhmTrig , sizeof(Float_t), 1, datei );
425
426 fread ( &fBinsFadc , sizeof(Int_t), 1, datei );
427 fread ( &fFadcShape , sizeof(Int_t), 1, datei );
428 fread ( &fIntegFadc , sizeof(Float_t), 1, datei );
429 fread ( &fFwhmFadc , sizeof(Float_t), 1, datei );
430
431 fread ( &fGainFluctuations, sizeof(Int_t), 1, datei );
432
433 if (fTrig)
434 delete [] fTrig;
435
436 if (fFadc)
437 delete [] fFadc;
438
439 fTrig = new Float_t[fBinsTrig];
440 fFadc = new Float_t[fBinsFadc];
441
442 fread (fTrig, sizeof(Float_t), fBinsTrig, datei);
443 fread (fFadc, sizeof(Float_t), fBinsFadc, datei);
444
445 fclose ( datei ) ;
446}
Note: See TracBrowser for help on using the repository browser.