source: trunk/Mars/mraw/MRawEvtData.cc@ 11014

Last change on this file since 11014 was 9227, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 19.4 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-2008
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 7
50// ------------------
51// + UShort_t fNumBytesPerSample;
52//
53// Version 6
54// ------------------
55// - The data can now be stoe in a single array keeping he full
56// compatibility
57//
58// Version 5 (0.8.5):
59// ------------------
60// - Changed type of ABFlags from TArrayC to MArrayB
61//
62// Version 4 (0.8.4):
63// ------------------
64// - Changed derivement from MCamEvent to MParContainer and MCamEvent
65//
66// Version 3 (0.8.4):
67// ------------------
68// - Added fABFlags
69//
70// Version 2:
71// ----------
72// - Derives from MCamEvent now
73//
74// Version 1:
75// ----------
76// - First implementation
77//
78/////////////////////////////////////////////////////////////////////////////
79#include "MRawEvtData.h"
80
81#include <fstream>
82
83#include <TH1.h>
84#include <TGraph.h>
85#include <TArrayC.h>
86#include <TVirtualPad.h>
87
88#include "MLog.h"
89#include "MLogManip.h"
90
91#include "MArrayS.h"
92#include "MArrayB.h"
93#include "MArrayI.h"
94#include "MGeomCam.h"
95
96#include "MRawCrateArray.h"
97#include "MRawCrateData.h"
98
99#include "MRawRunHeader.h"
100#include "MRawEvtPixelIter.h"
101
102ClassImp(MRawEvtData);
103
104using namespace std;
105
106// --------------------------------------------------------------------------
107//
108// Default constructor. It initializes all arrays with zero size.
109//
110MRawEvtData::MRawEvtData(const char *name, const char *title)
111 : fRunHeader(0), fNumBytesPerSample(1)
112{
113 fName = name ? name : "MRawEvtData";
114 fTitle = title ? title : "Raw Event Data Information";
115
116 InitArrays();
117}
118
119// --------------------------------------------------------------------------
120//
121// Destructor. Deletes all the arrays.
122//
123MRawEvtData::~MRawEvtData()
124{
125 DeleteArrays();
126}
127
128// --------------------------------------------------------------------------
129//
130// reset all arrays
131//
132void MRawEvtData::Clear(Option_t *)
133{
134 /*
135 FIXME:
136 Is Reset (set all entries to zero) what you want to do
137 or Set(0) (delete the array)
138 */
139 fHiGainPixId->Reset();
140 fLoGainPixId->Reset();
141 fHiGainFadcSamples->Reset();
142 fLoGainFadcSamples->Reset();
143 fABFlags->Reset();
144}
145
146// --------------------------------------------------------------------------
147//
148// return the number of hi gain samples per pixel
149//
150UShort_t MRawEvtData::GetNumHiGainSamples() const
151{
152 // If value<0 we are reading old MC files which don't have the
153 // value set so far. So we use the old methid to determin the
154 // numbers and calculate them from the length of the arrays.
155 return fHiGainPixId->GetSize() ? fHiGainFadcSamples->GetSize()/(fHiGainPixId->GetSize()*fNumBytesPerSample) : 0;
156}
157
158// --------------------------------------------------------------------------
159//
160// return the number of lo gain samples per pixel
161//
162UShort_t MRawEvtData::GetNumLoGainSamples() const
163{
164 // If value<0 we are reading old MC files which don't have the
165 // value set so far. So we use the old methid to determin the
166 // numbers and calculate them from the length of the arrays.
167 return fLoGainPixId->GetSize() ? fLoGainFadcSamples->GetSize()/(fLoGainPixId->GetSize()*fNumBytesPerSample) : 0;
168}
169
170// --------------------------------------------------------------------------
171//
172// return the number of stored pixel
173//
174UShort_t MRawEvtData::GetNumPixels() const
175{
176 return fHiGainPixId ? fHiGainPixId->GetSize() : 0;
177}
178
179// --------------------------------------------------------------------------
180//
181// Print out the onformation to *fLog.
182// Options:
183// "hex" Prints the time slices hexadecimal (default)
184// "dec" Prints the time slices decimal
185//
186void MRawEvtData::Print(Option_t *opt) const
187{
188 //
189 // print fadc inforation to screen
190 // Possible Options:
191 // - DEC: Print values decimal instead of hexadecimal (default)
192 //
193 const UShort_t nHiSamp = GetNumHiGainSamples();
194 const UShort_t nLoSamp = GetNumLoGainSamples();
195
196 const UShort_t bps = GetNumBytesPerSample();
197
198 fLog->unsetf(ios::showbase);
199
200 *fLog << dec << all;
201 *fLog << GetDescriptor() << ": " << endl;
202 *fLog << GetNumPixels() << " Pixels with " << (Int_t)nHiSamp << "/" << (Int_t)nLoSamp << " samples" << endl;
203
204 TString str(opt);
205 Int_t manip = str.Contains("dec", TString::kIgnoreCase);
206
207 MRawEvtPixelIter pixel(const_cast<MRawEvtData*>(this));
208 Int_t i = 0;
209 while (pixel.Next())
210 {
211 const UShort_t idx = pixel.GetPixelId();
212
213 *fLog << endl << dec << setfill(' ');
214 *fLog << " " << setw(3) << i++ << " - " << setw(3) << idx << " ";
215
216 if (pixel.IsABFlagValid())
217 *fLog << "<" << (pixel.HasABFlag()?"B":"A") << "> ";
218
219 *fLog << (manip?dec:hex) << setfill(manip?' ':'0');
220
221 const Byte_t *hi = (Byte_t*)pixel.GetHiGainSamples();
222 const Byte_t *lo = (Byte_t*)pixel.GetLoGainSamples();
223
224 for (int j=0; j<nHiSamp*bps; j++)
225 {
226 *fLog << setw(manip?3:2);
227 *fLog << (hi[j]&0xff);
228 if (manip)
229 *fLog << ' ';
230 }
231
232 for (int j=0; j<nLoSamp*bps; j++)
233 {
234 *fLog << setw(manip?3:2);
235 *fLog << ((UShort_t)lo[j]&0xff);
236 if (manip)
237 *fLog << ' ';
238 }
239 }
240 *fLog << dec << endl;
241}
242
243// --------------------------------------------------------------------------
244//
245// Draw a pixel. A Histogram or Graph is created and it's draw function is
246// called.
247// Options:
248// "GRAPH" A graph is drawn
249// "HIST" A histogram is drawn
250// <index> The pixel with the given index is drawn
251//
252void MRawEvtData::Draw(Option_t *opt)
253{
254 if (GetNumPixels()==0)
255 {
256 *fLog << warn << "Sorry, no pixel to draw!" << endl;
257 return;
258 }
259
260 TString str(opt);
261 str.ToLower();
262
263 UInt_t id = 0;
264
265 if (str.BeginsWith("graph"))
266 sscanf(str.Data()+5, "%d", &id);
267 if (str.BeginsWith("hist"))
268 sscanf(str.Data()+4, "%d", &id);
269
270 MRawEvtPixelIter pix(this);
271 if (!pix.Jump(id))
272 {
273 *fLog << warn << dec << "Pixel Idx #" << id << " doesn't exist!" << endl;
274 return;
275 }
276
277 const void *higains = pix.GetHiGainSamples();
278 const void *logains = pix.GetLoGainSamples();
279
280 const Int_t nh = GetNumHiGainSamples();
281 const Int_t nl = GetNumLoGainSamples();
282
283 TString name = "Pixel Idx.";
284 name += pix.GetPixelId();
285
286
287 Bool_t same = str.Contains("same");
288
289 if (str.BeginsWith("graph"))
290 {
291 *fLog << inf << "Drawing Graph: Pixel Idx #" << dec << pix.GetPixelId();
292 *fLog << " of " << (int)GetNumPixels() << "Pixels" << endl;
293
294 TGraph *graphhi = new TGraph;
295
296 for (int i=0; i<nh; i++)
297 graphhi->SetPoint(graphhi->GetN(), i, GetSample(higains, i));
298 for (int i=0; i<nl; i++)
299 graphhi->SetPoint(graphhi->GetN(), i+nh, GetSample(logains, i));
300
301 graphhi->SetMaximum(GetMax()+0.5);
302 graphhi->SetMinimum(0);
303
304 graphhi->SetBit(kCanDelete);
305 graphhi->Draw(same ? "C*" : "AC*");
306
307 TH1F *histhi = graphhi->GetHistogram();
308 histhi->SetMinimum(0);
309 histhi->SetMaximum(GetMax()+0.5);
310
311 histhi->SetXTitle("Time/FADC Slices");
312 histhi->SetYTitle("Signal/FADC Units");
313
314 return;
315 }
316
317 if (str.BeginsWith("hist"))
318 {
319 // FIXME: Add Legend
320 *fLog << inf << "Drawing Histogram of Pixel with Idx #" << dec << pix.GetPixelId() << " to " << gPad << endl;
321
322 TH1F *histh = new TH1F(name, "FADC Samples", nh+nl, -0.5, nh+nl-.5);
323 histh->SetMinimum(0);
324 histh->SetMaximum(GetMax()+0.5);
325 histh->SetXTitle("Time [FADC Slices]");
326 histh->SetYTitle("Signal [FADC Units]");
327 histh->SetDirectory(NULL);
328 for (int i=0; i<nh; i++)
329 histh->Fill(i, GetSample(higains, i));
330 for (int i=0; i<nl; i++)
331 histh->Fill(i+nl, GetSample(logains, i));
332 histh->SetBit(kCanDelete);
333 histh->Draw(same ? "same" : "");
334
335 return;
336 }
337
338 *fLog << warn << dbginf << "WARNING - You must specify either 'GRAPH' or 'HIST'" << endl;
339}
340
341// --------------------------------------------------------------------------
342//
343// Deletes all the arrays
344// The flag is for future usage.
345//
346void MRawEvtData::InitArrays(UShort_t numconnected, UShort_t maxid)
347{
348 // fRunHeader should not be set only in the constructor!
349 const Int_t numhi = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : 0;
350 const Int_t numlo = fRunHeader ? fRunHeader->GetNumSamplesLoGain() : 0;
351
352 fNumBytesPerSample = fRunHeader ? fRunHeader->GetNumBytesPerSample() : 1;
353
354 fHiGainPixId = new MArrayS(numconnected);
355 fLoGainPixId = new MArrayS(0);
356 fHiGainFadcSamples = new MArrayB(numconnected*(numhi+numlo)*fNumBytesPerSample);
357 fLoGainFadcSamples = new MArrayB(0);
358
359 fABFlags = new MArrayB(maxid==0 ? 0 : maxid/8+1);
360
361 fConnectedPixels = 0;
362}
363
364// --------------------------------------------------------------------------
365//
366// Deletes all the arrays
367//
368void MRawEvtData::DeleteArrays()
369{
370 delete fHiGainPixId;
371 delete fLoGainPixId;
372 delete fHiGainFadcSamples;
373 delete fLoGainFadcSamples;
374 delete fABFlags;
375}
376
377// --------------------------------------------------------------------------
378//
379// Deletes all arrays describing the pixel Id and Samples in pixels.
380// The flag is for future usage.
381//
382void MRawEvtData::ResetPixels(UShort_t numconnected, UShort_t maxid)
383{
384 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
385 if (fHiGainPixId && fHiGainPixId->GetSize()==numconnected && (UShort_t)fABFlags->GetSize()==(maxid/8+1))
386 {
387 fConnectedPixels = 0;
388 return;
389 }
390
391 DeleteArrays();
392 InitArrays(numconnected, maxid);
393}
394
395void MRawEvtData::ResetPixels()
396{
397 if (!fRunHeader)
398 return;
399
400 // FIXME: Better give NumNormalPixels if numconnected==0 ?
401
402 ResetPixels(fRunHeader->GetNumNormalPixels(), fRunHeader->GetNumNormalPixels()-1);
403}
404
405// --------------------------------------------------------------------------
406//
407// This is to fill the data of one pixel to the MRawEvtHeader Class.
408// The parameters are the pixelnumber and the FADC_SLICES values of ADCs
409//
410void MRawEvtData::AddPixel(UShort_t nOfPixel, const TArrayC &data)
411{
412 const Int_t n = fRunHeader->GetNumSamples();
413 if (data.GetSize()!=n)
414 {
415 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
416 *fLog << "TArrayC " << data.GetSize() << " doesn't match current number " << n << endl;
417 return;
418 }
419
420 //
421 // enhance pixel array by one
422 //
423 fHiGainPixId->Set(fHiGainPixId->GetSize()+1);
424
425 //
426 // add the number of the new pixel to the array as last entry
427 //
428 fHiGainPixId->AddAt(nOfPixel, fHiGainPixId->GetSize()-1);
429
430 //
431 // enhance the array by the number of new samples
432 //
433 fHiGainFadcSamples->Set(fHiGainFadcSamples->GetSize()+n);
434
435 //
436 // add the new slices as last entries to array
437 //
438 fHiGainFadcSamples->AddAt((Byte_t*)data.GetArray(), fHiGainFadcSamples->GetSize()-n, n);
439}
440
441// --------------------------------------------------------------------------
442//
443// Add the contents of the MArrayI to the fHiGainFadcSamples
444// One Integer is added to one sample in the array.
445//
446void MRawEvtData::Set(const MArrayI &data)
447{
448 fConnectedPixels = fHiGainPixId->GetSize();
449
450 UInt_t n = fConnectedPixels*fRunHeader->GetNumSamplesHiGain();
451 if (n!=data.GetSize())
452 {
453 *fLog << err << "MRawEvtData::Set: Size mismatch." << endl;
454 *fLog << " fConnectedPixels=" << fConnectedPixels << endl;
455 *fLog << " NumHiGainSamples=" << fRunHeader->GetNumSamplesHiGain() << endl;
456 *fLog << " data.GetSize()=" << data.GetSize() << endl;
457 return;
458 }
459
460 Byte_t *dest = fHiGainFadcSamples->GetArray();
461
462 UInt_t *src = reinterpret_cast<UInt_t*>(data.GetArray());
463 UInt_t *end = reinterpret_cast<UInt_t*>(data.GetArray()) + n;
464
465 switch (fNumBytesPerSample)
466 {
467 case 1:
468 {
469 Byte_t *ptr = reinterpret_cast<Byte_t*>(dest);
470 while (src<end)
471 *ptr++ = Byte_t(*src++);
472 }
473 return;
474
475 case 2:
476 {
477 UShort_t *ptr = reinterpret_cast<UShort_t*>(dest);
478 while (src<end)
479 *ptr++ = UShort_t(*src++);
480 }
481 return;
482
483 case 4:
484 memcpy(dest, data.GetArray(), n*4);
485 return;
486 }
487}
488
489// --------------------------------------------------------------------------
490//
491// Set indices according to the given array. The array must have the same
492// size as fHiGainPixId
493//
494void MRawEvtData::SetIndices(const MArrayS &idx)
495{
496 if (idx.GetSize()!=fHiGainPixId->GetSize())
497 return;
498
499 memcpy(fHiGainPixId->GetArray(), idx.GetArray(), idx.GetSize()*sizeof(UShort_t));
500}
501
502// --------------------------------------------------------------------------
503//
504// Set indices according to the length of the array fHiGainPixId
505// from 0 to n-1
506//
507void MRawEvtData::SetIndices()
508{
509 for (UInt_t i=0; i<fHiGainPixId->GetSize(); i++)
510 (*fHiGainPixId)[i] = i;
511}
512
513/*
514void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag)
515{
516 MArrayS *arrpix = lflag ? fLoGainPixId : fHiGainPixId;
517 MArrayB *arrsam = lflag ? fLoGainFadcSamples : fHiGainFadcSamples;
518
519 //
520 // check whether we got the right number of new samples
521 // if there are no samples already stored: this is the new number of samples
522 //
523 const UShort_t ns = data->GetSize();
524 const UShort_t nSamp = lflag ? GetNumLoGainSamples() : GetNumHiGainSamples();
525 if (nSamp && ns!=nSamp)
526 {
527 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
528 *fLog << "TArrayC " << ns << " doesn't match current number " << nSamp << endl;
529 return;
530 }
531
532 //
533 // enhance pixel array by one
534 //
535 arrpix->Set(arrpix->GetSize()+1);
536
537 //
538 // add the number of the new pixel to the array as last entry
539 //
540 arrpix->AddAt(nOfPixel, arrpix->GetSize()-1);
541
542 //
543 // enhance the array by the number of new samples
544 //
545 arrsam->Set(arrsam->GetSize()+ns);
546
547 //
548 // add the new slices as last entries to array
549 //
550 arrsam->AddAt((Byte_t*)data->GetArray(), arrsam->GetSize()-ns, ns);
551}
552*/
553
554void MRawEvtData::ReadPixel(istream &fin, Int_t npix)
555{
556 // number of samples
557 const UInt_t ns = fRunHeader->GetNumSamples();
558
559 // bytes per sample
560 const Int_t bps = fRunHeader->GetNumBytesPerSample();
561
562 // Number of bytes
563 const Int_t nb = ns*bps;
564
565 // position in higain array
566 Byte_t *pos = fHiGainFadcSamples->GetArray() + fConnectedPixels*nb;
567
568 // Set pixel index
569 fHiGainPixId->AddAt(npix, fConnectedPixels++);
570
571 // Read data for one pixel
572 fin.read((char*)pos, nb);
573}
574
575void MRawEvtData::SetABFlag(Int_t npix, Bool_t ab)
576{
577 if (ab)
578 SETBIT((*fABFlags)[npix/8], npix%8);
579 else
580 CLRBIT((*fABFlags)[npix/8], npix%8);
581}
582
583// --------------------------------------------------------------------------
584//
585// Fills members with information from a magic binary file.
586// WARNING: you have to use Init() before you can do this
587//
588/*
589void MRawEvtData::ReadEvt(istream &fin, Int_t posinarray)
590{
591
592 const UShort_t npic = fRunHeader->GetNumPixInCrate();
593
594 const UShort_t npos = npic*posinarray;
595
596 //const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
597
598 for (int i=npos; i<npic+npos; i++)
599 {
600 // calc the spiral hardware pixel number
601 const UShort_t ipos = i;
602
603 // Get Hardware Id
604 const Short_t hwid = fRunHeader->GetPixAssignment(ipos);
605
606 // Check whether the pixel is connected or not
607 if (hwid==0)
608 {
609 const UShort_t n = fRunHeader->GetNumSamplesLoGain()+fRunHeader->GetNumSamplesHiGain();
610 fin.seekg(n, ios::cur);
611 return;
612 }
613
614 // -1 converts the hardware pixel Id into the software pixel index
615 const Int_t npix = (Int_t)hwid-1;
616
617 const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
618 AddPixel(fin, npix, TESTBIT(ab, i-npos));
619 }
620}
621*/
622
623// --------------------------------------------------------------------------
624//
625// Return the size in bytes of one event data block
626//
627Int_t MRawEvtData::GetNumBytes() const
628{
629 const UShort_t nlo = fRunHeader->GetNumSamplesLoGain();
630 const UShort_t nhi = fRunHeader->GetNumSamplesHiGain();
631 const UShort_t npic = fRunHeader->GetNumPixInCrate();
632 const UShort_t nbps = fRunHeader->GetNumBytesPerSample();
633
634 return (nhi+nlo)*npic*nbps;
635}
636
637// --------------------------------------------------------------------------
638//
639// Make sure, that you skip the whole event. This function only skips a part
640// of the event - see MRawRead::SkipEvent
641//
642void MRawEvtData::SkipEvt(istream &fin)
643{
644 fin.seekg(GetNumBytes(), ios::cur);
645}
646
647Bool_t MRawEvtData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
648{
649 *fLog << warn << "WARNING - The use of MRawEvtData::GetPixelContent is deprecated!" << endl;
650
651 /*
652 MRawEvtPixelIter Next(const_cast<MRawEvtData*>(this));
653 if (!Next.Jump(idx))
654 return kFALSE;
655
656 switch (type)
657 {
658 case 0:
659 val = Next.GetSumHiGainSamples()-(float)GetNumHiGainSamples()*fHiGainFadcSamples->GetArray()[0];
660 val*= cam.GetPixRatio(idx);
661 break;
662 case 1:
663 val = Next.GetMaxHiGainSample();
664 break;
665 case 2:
666 val = Next.GetMaxLoGainSample();
667 break;
668 case 3:
669 val = Next.GetIdxMaxHiGainSample();
670 break;
671 case 4:
672 val = Next.GetIdxMaxLoGainSample();
673 break;
674 case 5:
675 val = Next.GetIdxMaxHiLoGainSample();
676 return val >= 0;
677 }
678*/
679 return kTRUE;
680}
681
682void MRawEvtData::Copy(TObject &named)
683#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
684const
685#endif
686{
687 MRawEvtData &evt = (MRawEvtData &)named;
688
689 *evt.fHiGainPixId = *fHiGainPixId;
690 *evt.fLoGainPixId = *fLoGainPixId;
691
692 *evt.fHiGainFadcSamples = *fHiGainFadcSamples;
693 *evt.fLoGainFadcSamples = *fLoGainFadcSamples;
694
695 *evt.fABFlags = *fABFlags;
696
697 evt.fConnectedPixels = fConnectedPixels;
698
699 evt.fNumBytesPerSample = fNumBytesPerSample;
700}
Note: See TracBrowser for help on using the repository browser.