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

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