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

Last change on this file since 9223 was 9212, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 18.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-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 Bool_t same = str.Contains("same");
287
288 if (str.BeginsWith("graph"))
289 {
290 *fLog << inf << "Drawing Graph: Pixel Idx #" << dec << pix.GetPixelId();
291 *fLog << " of " << (int)GetNumPixels() << "Pixels" << endl;
292
293 TGraph *graphhi = new TGraph;
294
295 for (int i=0; i<nh; i++)
296 graphhi->SetPoint(graphhi->GetN(), i, GetSample(higains, i));
297 for (int i=0; i<nl; i++)
298 graphhi->SetPoint(graphhi->GetN(), i+nh, GetSample(logains, i));
299
300 graphhi->SetMaximum(GetMax()+0.5);
301 graphhi->SetMinimum(0);
302
303 graphhi->SetBit(kCanDelete);
304 graphhi->Draw(same ? "C*" : "AC*");
305
306 TH1F *histhi = graphhi->GetHistogram();
307 histhi->SetMinimum(0);
308 histhi->SetMaximum(GetMax()+0.5);
309
310 histhi->SetXTitle("Time/FADC Slices");
311 histhi->SetYTitle("Signal/FADC Units");
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+nl, -0.5, nh+nl-.5);
322 histh->SetMinimum(0);
323 histh->SetMaximum(GetMax()+0.5);
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, GetSample(higains, i));
329 for (int i=0; i<nl; i++)
330 histh->Fill(i+nl, GetSample(logains, i));
331 histh->SetBit(kCanDelete);
332 histh->Draw(same ? "same" : "");
333
334 return;
335 }
336
337 *fLog << warn << dbginf << "WARNING - You must specify either 'GRAPH' or 'HIST'" << endl;
338}
339
340// --------------------------------------------------------------------------
341//
342// Deletes all the arrays
343// The flag is for future usage.
344//
345void MRawEvtData::InitArrays(UShort_t numconnected, UShort_t maxid)
346{
347 // fRunHeader should not be set only in the constructor!
348 const Int_t numhi = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : 0;
349 const Int_t numlo = fRunHeader ? fRunHeader->GetNumSamplesLoGain() : 0;
350
351 fNumBytesPerSample = fRunHeader ? fRunHeader->GetNumBytesPerSample() : 1;
352
353 fHiGainPixId = new MArrayS(numconnected);
354 fLoGainPixId = new MArrayS(0);
355 fHiGainFadcSamples = new MArrayB(numconnected*(numhi+numlo)*fNumBytesPerSample);
356 fLoGainFadcSamples = new MArrayB(0);
357
358 fABFlags = new MArrayB(maxid==0 ? 0 : maxid/8+1);
359
360 fConnectedPixels = 0;
361}
362
363// --------------------------------------------------------------------------
364//
365// Deletes all the arrays
366//
367void MRawEvtData::DeleteArrays()
368{
369 delete fHiGainPixId;
370 delete fLoGainPixId;
371 delete fHiGainFadcSamples;
372 delete fLoGainFadcSamples;
373 delete fABFlags;
374}
375
376// --------------------------------------------------------------------------
377//
378// Deletes all arrays describing the pixel Id and Samples in pixels.
379// The flag is for future usage.
380//
381void MRawEvtData::ResetPixels(UShort_t numconnected, UShort_t maxid)
382{
383 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
384 if (fHiGainPixId && fHiGainPixId->GetSize()==numconnected && (UShort_t)fABFlags->GetSize()==(maxid/8+1))
385 {
386 fConnectedPixels = 0;
387 return;
388 }
389
390 DeleteArrays();
391 InitArrays(numconnected, maxid);
392}
393
394// --------------------------------------------------------------------------
395//
396// This is to fill the data of one pixel to the MRawEvtHeader Class.
397// The parameters are the pixelnumber and the FADC_SLICES values of ADCs
398//
399void MRawEvtData::AddPixel(UShort_t nOfPixel, const TArrayC &data)
400{
401 const Int_t n = fRunHeader->GetNumSamples();
402 if (data.GetSize()!=n)
403 {
404 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
405 *fLog << "TArrayC " << data.GetSize() << " doesn't match current number " << n << endl;
406 return;
407 }
408
409 //
410 // enhance pixel array by one
411 //
412 fHiGainPixId->Set(fHiGainPixId->GetSize()+1);
413
414 //
415 // add the number of the new pixel to the array as last entry
416 //
417 fHiGainPixId->AddAt(nOfPixel, fHiGainPixId->GetSize()-1);
418
419 //
420 // enhance the array by the number of new samples
421 //
422 fHiGainFadcSamples->Set(fHiGainFadcSamples->GetSize()+n);
423
424 //
425 // add the new slices as last entries to array
426 //
427 fHiGainFadcSamples->AddAt((Byte_t*)data.GetArray(), fHiGainFadcSamples->GetSize()-n, n);
428}
429
430// --------------------------------------------------------------------------
431//
432// Add the contents of the MArrayI to the fHiGainFadcSamples
433// One Integer is added to one sample in the array.
434//
435void MRawEvtData::Set(const MArrayI &data)
436{
437 const UInt_t n = fRunHeader->GetNumSamples()*fConnectedPixels;
438
439 Byte_t *dest = fHiGainFadcSamples->GetArray();
440
441 UInt_t *src = reinterpret_cast<UInt_t*>(data.GetArray());
442 UInt_t *end = reinterpret_cast<UInt_t*>(data.GetArray()) + n;
443
444 switch (fNumBytesPerSample)
445 {
446 case 1:
447 {
448 Byte_t *ptr = reinterpret_cast<Byte_t*>(dest);
449 while (src<end)
450 *ptr++ = *src++;
451 }
452 return;
453
454 case 2:
455 {
456 UShort_t *ptr = reinterpret_cast<UShort_t*>(dest);
457 while (src<end)
458 *ptr++ = *src++;
459 }
460 return;
461
462 case 4:
463 memcpy(dest, data.GetArray(), n*4);
464 return;
465 }
466}
467
468/*
469void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag)
470{
471 MArrayS *arrpix = lflag ? fLoGainPixId : fHiGainPixId;
472 MArrayB *arrsam = lflag ? fLoGainFadcSamples : fHiGainFadcSamples;
473
474 //
475 // check whether we got the right number of new samples
476 // if there are no samples already stored: this is the new number of samples
477 //
478 const UShort_t ns = data->GetSize();
479 const UShort_t nSamp = lflag ? GetNumLoGainSamples() : GetNumHiGainSamples();
480 if (nSamp && ns!=nSamp)
481 {
482 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
483 *fLog << "TArrayC " << ns << " doesn't match current number " << nSamp << endl;
484 return;
485 }
486
487 //
488 // enhance pixel array by one
489 //
490 arrpix->Set(arrpix->GetSize()+1);
491
492 //
493 // add the number of the new pixel to the array as last entry
494 //
495 arrpix->AddAt(nOfPixel, arrpix->GetSize()-1);
496
497 //
498 // enhance the array by the number of new samples
499 //
500 arrsam->Set(arrsam->GetSize()+ns);
501
502 //
503 // add the new slices as last entries to array
504 //
505 arrsam->AddAt((Byte_t*)data->GetArray(), arrsam->GetSize()-ns, ns);
506}
507*/
508
509void MRawEvtData::ReadPixel(istream &fin, Int_t npix)
510{
511 // number of samples
512 const UInt_t ns = fRunHeader->GetNumSamples();
513
514 // bytes per sample
515 const Int_t bps = fRunHeader->GetNumBytesPerSample();
516
517 // Number of bytes
518 const Int_t nb = ns*bps;
519
520 // position in higain array
521 Byte_t *pos = fHiGainFadcSamples->GetArray() + fConnectedPixels*nb;
522
523 // Set pixel index
524 fHiGainPixId->AddAt(npix, fConnectedPixels++);
525
526 // Read data for one pixel
527 fin.read((char*)pos, nb);
528}
529
530void MRawEvtData::SetABFlag(Int_t npix, Bool_t ab)
531{
532 if (ab)
533 SETBIT((*fABFlags)[npix/8], npix%8);
534 else
535 CLRBIT((*fABFlags)[npix/8], npix%8);
536}
537
538// --------------------------------------------------------------------------
539//
540// Fills members with information from a magic binary file.
541// WARNING: you have to use Init() before you can do this
542//
543/*
544void MRawEvtData::ReadEvt(istream &fin, Int_t posinarray)
545{
546
547 const UShort_t npic = fRunHeader->GetNumPixInCrate();
548
549 const UShort_t npos = npic*posinarray;
550
551 //const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
552
553 for (int i=npos; i<npic+npos; i++)
554 {
555 // calc the spiral hardware pixel number
556 const UShort_t ipos = i;
557
558 // Get Hardware Id
559 const Short_t hwid = fRunHeader->GetPixAssignment(ipos);
560
561 // Check whether the pixel is connected or not
562 if (hwid==0)
563 {
564 const UShort_t n = fRunHeader->GetNumSamplesLoGain()+fRunHeader->GetNumSamplesHiGain();
565 fin.seekg(n, ios::cur);
566 return;
567 }
568
569 // -1 converts the hardware pixel Id into the software pixel index
570 const Int_t npix = (Int_t)hwid-1;
571
572 const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
573 AddPixel(fin, npix, TESTBIT(ab, i-npos));
574 }
575}
576*/
577
578// --------------------------------------------------------------------------
579//
580// Return the size in bytes of one event data block
581//
582Int_t MRawEvtData::GetNumBytes() const
583{
584 const UShort_t nlo = fRunHeader->GetNumSamplesLoGain();
585 const UShort_t nhi = fRunHeader->GetNumSamplesHiGain();
586 const UShort_t npic = fRunHeader->GetNumPixInCrate();
587 const UShort_t nbps = fRunHeader->GetNumBytesPerSample();
588
589 return (nhi+nlo)*npic*nbps;
590}
591
592// --------------------------------------------------------------------------
593//
594// Make sure, that you skip the whole event. This function only skips a part
595// of the event - see MRawRead::SkipEvent
596//
597void MRawEvtData::SkipEvt(istream &fin)
598{
599 fin.seekg(GetNumBytes(), ios::cur);
600}
601
602Bool_t MRawEvtData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
603{
604 *fLog << warn << "WARNING - The use of MRawEvtData::GetPixelContent is deprecated!" << endl;
605
606 /*
607 MRawEvtPixelIter Next(const_cast<MRawEvtData*>(this));
608 if (!Next.Jump(idx))
609 return kFALSE;
610
611 switch (type)
612 {
613 case 0:
614 val = Next.GetSumHiGainSamples()-(float)GetNumHiGainSamples()*fHiGainFadcSamples->GetArray()[0];
615 val*= cam.GetPixRatio(idx);
616 break;
617 case 1:
618 val = Next.GetMaxHiGainSample();
619 break;
620 case 2:
621 val = Next.GetMaxLoGainSample();
622 break;
623 case 3:
624 val = Next.GetIdxMaxHiGainSample();
625 break;
626 case 4:
627 val = Next.GetIdxMaxLoGainSample();
628 break;
629 case 5:
630 val = Next.GetIdxMaxHiLoGainSample();
631 return val >= 0;
632 }
633*/
634 return kTRUE;
635}
636
637void MRawEvtData::Copy(TObject &named)
638#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
639const
640#endif
641{
642 MRawEvtData &evt = (MRawEvtData &)named;
643
644 *evt.fHiGainPixId = *fHiGainPixId;
645 *evt.fLoGainPixId = *fLoGainPixId;
646
647 *evt.fHiGainFadcSamples = *fHiGainFadcSamples;
648 *evt.fLoGainFadcSamples = *fLoGainFadcSamples;
649
650 *evt.fABFlags = *fABFlags;
651
652 evt.fConnectedPixels = fConnectedPixels;
653
654 evt.fNumBytesPerSample = fNumBytesPerSample;
655}
Note: See TracBrowser for help on using the repository browser.