source: trunk/MagicSoft/Mars/mraw/MRawEvtData.cc@ 2406

Last change on this file since 2406 was 2372, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 14.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MRawEvtData
28//
29// Storage container to store the raw FADC values.
30//
31// MArrayS fHiGainPixId
32// ---------------------
33// Array of Pixel Numbers for their high voltage channel in the order the
34// FADC values are stored in fHiGainFadcSamples
35//
36// MArrayB fHiGainFadcSaples
37// -------------------------
38// FADC samples (hi gain) of all pixels
39//
40// MArrayS fLoGainPixId
41// --------------------
42// see fHiGainPixId
43//
44// MArrayB fLoGainFadcSamples
45// --------------------------
46// see fHiGainFadcSaples
47//
48//
49// Version 2: Derives from MCamEvent now
50//
51/////////////////////////////////////////////////////////////////////////////
52
53#include "MRawEvtData.h"
54
55#include <fstream>
56
57#include <TH1.h>
58#include <TGraph.h>
59#include <TArrayC.h>
60
61#include "MLog.h"
62#include "MLogManip.h"
63
64#include "MArrayS.h"
65#include "MArrayB.h"
66#include "MGeomCam.h"
67#include "MRawRunHeader.h"
68#include "MRawEvtPixelIter.h"
69
70ClassImp(MRawEvtData);
71
72using namespace std;
73
74// --------------------------------------------------------------------------
75//
76// Default constructor. It initializes all arrays with zero size.
77//
78MRawEvtData::MRawEvtData(const char *name, const char *title)
79{
80 fName = name ? name : "MRawEvtData";
81 fTitle = title ? title : "Raw Event Data Information";
82
83 InitArrays();
84}
85
86// --------------------------------------------------------------------------
87//
88// Destructor. Deletes all the arrays.
89//
90MRawEvtData::~MRawEvtData()
91{
92 DeleteArrays();
93}
94
95// --------------------------------------------------------------------------
96//
97// reset all arrays
98//
99void MRawEvtData::Clear(Option_t *)
100{
101 /*
102 FIXME:
103 Is Reset (set all entries to zero) what you want to do
104 or Set(0) (delete the array)
105 */
106 fHiGainPixId->Reset();
107 fLoGainPixId->Reset();
108 fHiGainFadcSamples->Reset();
109 fLoGainFadcSamples->Reset();
110}
111
112// --------------------------------------------------------------------------
113//
114// return the number of hi gain samples per pixel
115//
116Byte_t MRawEvtData::GetNumHiGainSamples() const
117{
118 return fHiGainPixId->GetSize() ? fHiGainFadcSamples->GetSize()/fHiGainPixId->GetSize() : 0;
119}
120
121// --------------------------------------------------------------------------
122//
123// return the number of lo gain samples per pixel
124//
125Byte_t MRawEvtData::GetNumLoGainSamples() const
126{
127 return fLoGainPixId->GetSize() ? fLoGainFadcSamples->GetSize()/fLoGainPixId->GetSize() : 0;
128}
129
130// --------------------------------------------------------------------------
131//
132// return the number of stored pixel
133//
134UShort_t MRawEvtData::GetNumPixels() const
135{
136 return fHiGainPixId->GetSize();
137}
138
139
140// --------------------------------------------------------------------------
141//
142// Print out the onformation to *fLog.
143// Options:
144// "hex" Prints the time slices hexadecimal (default)
145// "dec" Prints the time slices decimal
146//
147void MRawEvtData::Print(Option_t *opt) const
148{
149 //
150 // print fadc inforation to screen
151 // Possible Options:
152 // - DEC: Print values decimal instead of hexadecimal (default)
153 //
154 const Byte_t nHiSamp = GetNumHiGainSamples();
155 const Byte_t nLoSamp = GetNumLoGainSamples();
156
157 const UShort_t nHiPix = fHiGainPixId->GetSize();;
158 const UShort_t nLoPix = fLoGainPixId->GetSize();;
159
160 fLog->unsetf(ios::showbase);
161
162 *fLog << dec << all;
163 *fLog << "HiGain: " << nHiPix << " Pixels with " << (Int_t)nHiSamp << " Samples" << endl;
164 *fLog << "LoGain: " << nLoPix << " Pixels with " << (Int_t)nLoSamp << " Samples";;
165
166 TString str(opt);
167 Int_t manip = str.Contains("dec", TString::kIgnoreCase);
168
169 Int_t l=0;
170 for (int i=0; i<nHiPix; i++)
171 {
172 *fLog << endl;
173 *fLog << " " << setfill(' ') << setw(3) << dec << i << " -";
174 *fLog << " " << setfill(' ') << setw(3) << dec << (*fHiGainPixId)[i] << ": ";
175 *fLog << (manip?dec:hex) << flush;
176
177 if (!manip)
178 *fLog << setfill('0');
179
180 for (int j=0; j<nHiSamp; j++)
181 {
182 *fLog << setw(manip?3:2);
183 *fLog << ((UShort_t)(*fHiGainFadcSamples)[j+i*nHiSamp]&0xff);
184 if (manip)
185 *fLog << ' ';
186 *fLog << flush;
187 }
188
189 if (!(l<nLoPix && (*fLoGainPixId)[l]==(*fHiGainPixId)[i]))
190 continue;
191
192 if (manip)
193 *fLog << "/ ";
194
195 for (int j=0; j<nLoSamp; j++)
196 {
197 *fLog << setw(manip?3:2);
198 *fLog << ((UShort_t)(*fLoGainFadcSamples)[j+i*nLoSamp]&0xff);
199 if (manip)
200 *fLog << ' ';
201 *fLog << flush;
202 }
203 l++;
204 }
205 *fLog << endl;
206}
207
208// --------------------------------------------------------------------------
209//
210// Draw a pixel. A Histogram or Graph is created and it's draw function is
211// called.
212// Options:
213// "GRAPH" A graph is drawn
214// "HIST" A histogram is drawn
215// <index> The pixel with the given index is drawn
216//
217void MRawEvtData::Draw(Option_t *opt)
218{
219 if (GetNumPixels()==0)
220 {
221 *fLog << warn << "Sorry, no pixel to draw!" << endl;
222 return;
223 }
224
225 TString str(opt);
226 str.ToLower();
227
228 UInt_t id = 0;
229
230 if (str.BeginsWith("graph"))
231 if (str.Length()>5)
232 sscanf(&str[5], "%d", &id);
233 if (str.BeginsWith("hist"))
234 if (str.Length()>4)
235 sscanf(&str[4], "%d", &id);
236
237 MRawEvtPixelIter pix(this);
238 if (!pix.Jump(id))
239 {
240 *fLog << warn << "Pixel Idx #" << id << " doesn't exist!" << endl;
241 return;
242 }
243
244 const Byte_t *higains = pix.GetHiGainSamples();
245 const Byte_t *logains = pix.GetLoGainSamples();
246
247 const Int_t nh = GetNumHiGainSamples();
248 const Int_t nl = GetNumLoGainSamples();
249
250 TString name = "Pixel Idx.";
251 name += pix.GetPixelId();
252
253 Bool_t same = str.Contains("same");
254
255 if (str.BeginsWith("graph"))
256 {
257 *fLog << inf << "Drawing Graph: Pixel Idx #" << pix.GetPixelId();
258 *fLog << " of " << (int)GetNumPixels() << "Pixels" << endl;
259
260 TGraph *graphhi = new TGraph;
261
262 for (int i=0; i<nh; i++)
263 graphhi->SetPoint(graphhi->GetN(), i, higains[i]);
264
265 graphhi->SetMaximum(256);
266 graphhi->SetMinimum(0);
267
268 graphhi->SetBit(kCanDelete);
269 graphhi->Draw(same ? "C*" : "AC*");
270
271 TH1F *histhi = graphhi->GetHistogram();
272
273 histhi->SetXTitle("Time/FADC Slices");
274 histhi->SetYTitle("Signal/FADC Units");
275
276 if (nl>0)
277 {
278 TGraph *graphlo = new TGraph;
279
280 for (int i=0; i<nl; i++)
281 graphlo->SetPoint(graphlo->GetN(), i, logains[i]);
282
283 graphlo->SetMaximum(256);
284 graphlo->SetMinimum(0);
285 graphlo->SetLineColor(kBlue);
286
287 graphlo->SetBit(kCanDelete);
288 graphlo->Draw("C*");
289
290 TH1F *histlo = graphlo->GetHistogram();
291
292 histlo->SetXTitle("Time/FADC Slices");
293 histlo->SetYTitle("Signal/FADC Units");
294 }
295
296 return;
297 }
298
299 if (str.BeginsWith("hist"))
300 {
301 // FIXME: Add Legend
302 *fLog << inf << "Drawing Histogram of Pixel with Idx #" << pix.GetPixelId() << endl;
303
304 TH1F *histh = new TH1F(name, "FADC Samples", nh, -0.5, nh-.5);
305 histh->SetXTitle("Time [FADC Slices]");
306 histh->SetYTitle("Signal [FADC Units]");
307 histh->SetDirectory(NULL);
308 for (int i=0; i<nh; i++)
309 histh->Fill(i, higains[i]);
310 histh->SetBit(kCanDelete);
311 histh->Draw(same ? "same" : "");
312
313 if (nl>0)
314 {
315 TH1F *histl = new TH1F(name+";2", "FADC Samples", nl, -0.5, nl-.5);
316 histl->SetLineColor(kBlue);
317 histl->SetDirectory(NULL);
318 for (int i=0; i<nl; i++)
319 histl->Fill(i, logains[i]);
320 histl->SetBit(kCanDelete);
321 histl->Draw("same");
322 }
323 return;
324 }
325
326 *fLog << warn << dbginf << "Warning - You must specify either 'GRAPH' or 'HIST'" << endl;
327}
328
329// --------------------------------------------------------------------------
330//
331// Deletes all arrays describing the pixel Id and Samples in pixels.
332// The flag is for future usage.
333//
334void MRawEvtData::DeletePixels(Bool_t flag)
335{
336 if (fRunHeader && flag)
337 {
338 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
339 const int npix = fRunHeader->GetNumConnectedPixels();
340
341 if (fArraySize == npix)
342 {
343 fPosInArray = 0;
344 fConnectedPixels = 0;
345 return;
346 }
347 }
348
349 DeleteArrays();
350 InitArrays(flag);
351}
352
353// --------------------------------------------------------------------------
354//
355// Deletes all the arrays
356//
357void MRawEvtData::DeleteArrays()
358{
359 delete fHiGainPixId;
360 delete fLoGainPixId;
361 delete fHiGainFadcSamples;
362 delete fLoGainFadcSamples;
363}
364
365// --------------------------------------------------------------------------
366//
367// Deletes all the arrays
368// The flag is for future usage.
369//
370void MRawEvtData::InitArrays(Bool_t flag)
371{
372 if (flag && fRunHeader)
373 {
374 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
375 const int npix = fRunHeader->GetNumConnectedPixels();
376
377 fHiGainPixId = new MArrayS(npix);
378 fLoGainPixId = new MArrayS(npix);
379 fHiGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesHiGain());
380 fLoGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesLoGain());
381
382 fArraySize = npix;
383 }
384 else
385 {
386 fHiGainPixId = new MArrayS(0);
387 fLoGainPixId = new MArrayS(0);
388 fHiGainFadcSamples = new MArrayB(0);
389 fLoGainFadcSamples = new MArrayB(0);
390
391 fArraySize = 0;
392 }
393
394 fPosInArray = 0;
395 fConnectedPixels = 0;
396}
397
398// --------------------------------------------------------------------------
399//
400// This is to fill the data of one pixel to the MRawEvtHeader Class.
401// The parameters are the pixelnumber and the FADC_SLICES values of ADCs
402// Add to lo gains if lflag = 1
403//
404void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag)
405{
406 MArrayS *arrpix = lflag ? fLoGainPixId : fHiGainPixId;
407 MArrayB *arrsam = lflag ? fLoGainFadcSamples : fHiGainFadcSamples;
408
409 //
410 // check whether we got the right number of new samples
411 // if there are no samples already stored: this is the new number of samples
412 //
413 const Byte_t ns = data->GetSize();
414 const Byte_t nSamp = lflag ? GetNumLoGainSamples() : GetNumHiGainSamples();
415 if (nSamp && ns!=nSamp)
416 {
417 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
418 *fLog << "TArrayC doesn't match actual number" << endl;
419 return;
420 }
421
422 //
423 // enhance pixel array by one
424 //
425 arrpix->Set(arrpix->GetSize()+1);
426
427 //
428 // add the number of the new pixel to the array as last entry
429 //
430 arrpix->AddAt(nOfPixel, arrpix->GetSize()-1);
431
432 //
433 // enhance the array by the number of new samples
434 //
435 arrsam->Set(arrsam->GetSize()+ns);
436
437 //
438 // add the new slices as last entries to array
439 //
440 arrsam->AddAt((Byte_t*)data->GetArray(), arrsam->GetSize()-ns, ns);
441}
442
443// --------------------------------------------------------------------------
444//
445// Fills members with information from a magic binary file.
446// WARNING: you have to use Init() before you can do this
447//
448void MRawEvtData::ReadEvt(istream &fin)
449{
450 const UShort_t nlo = fRunHeader->GetNumSamplesLoGain();
451 const UShort_t nhi = fRunHeader->GetNumSamplesHiGain();
452
453 const UShort_t npic = fRunHeader->GetNumPixInCrate();
454
455 const UShort_t npos = npic*fPosInArray;
456
457 Byte_t *higainsam = fHiGainFadcSamples->GetArray()+nhi*fConnectedPixels;
458 Byte_t *logainsam = fLoGainFadcSamples->GetArray()+nlo*fConnectedPixels;
459
460 for (int i=0; i<npic; i++)
461 {
462 fin.read((char*)higainsam, nhi);
463 fin.read((char*)logainsam, nlo);
464
465 // calc the spiral hardware pixel number
466 const UShort_t ipos = npos+i;
467
468 // -1 converts the hardware pixel Id into the software pixel index
469 const Int_t npix = (Int_t)fRunHeader->GetPixAssignment(ipos)-1;
470
471 // Check whether the pixel is connected or not
472 if (npix<0)
473 continue;
474
475 //
476 // This is to fill the data of one pixel to the MRawEvtHeader Class.
477 // The parameters are the pixelnumber and the FADC_SLICES values of ADCs
478 // Add to lo gains if lflag = 1
479 //
480 fHiGainPixId->AddAt(npix, fConnectedPixels);
481 higainsam += nhi;
482
483 // FIXME: Not implemented in the raw files yet
484 //if (IsLoGainOn(i, j))
485 //{
486 fLoGainPixId->AddAt(npix, fConnectedPixels);
487 logainsam += nlo;
488 //}
489
490 fConnectedPixels++;
491 }
492
493 fPosInArray++;
494}
495
496Bool_t MRawEvtData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
497{
498 MRawEvtPixelIter Next(const_cast<MRawEvtData*>(this));
499 if (!Next.Jump(idx))
500 return kFALSE;
501
502 switch (type)
503 {
504 case 0:
505 val = Next.GetSumHiGainSamples()-(float)GetNumHiGainSamples()*fHiGainFadcSamples->GetArray()[0];
506 val*= cam.GetPixRatio(idx);
507 break;
508 case 1:
509 val = Next.GetMaxHiGainSample();
510 break;
511 case 2:
512 val = Next.GetMaxLoGainSample();
513 break;
514 }
515
516 return kTRUE;
517}
Note: See TracBrowser for help on using the repository browser.