source: tags/Mars-V0.8.7pre/mraw/MRawEvtData.cc

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