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

Last change on this file since 5250 was 4754, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 16.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-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//
141Byte_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//
150Byte_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 Byte_t nHiSamp = GetNumHiGainSamples();
180 const Byte_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 ";
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 numlo = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : 0;
371 const Int_t numhi = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : 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/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 Byte_t ns = data->GetSize();
430 const Byte_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 fin.read((char*)poslo, fRunHeader->GetNumSamplesLoGain());
467
468 //
469 // This is to fill the data of one pixel to the MRawEvtHeader Class.
470 // The parameters are the pixelnumber and the FADC_SLICES values of ADCs
471 // Add to lo gains if lflag = 1
472 //
473 fHiGainPixId->AddAt(npix, fConnectedPixels);
474
475 // FIXME: Not implemented in the raw files yet
476 //if (IsLoGainOn(i, j))
477 //{
478 fLoGainPixId->AddAt(npix, fConnectedPixels);
479 //}
480
481 if (ab)
482 SETBIT((*fABFlags)[npix/8], npix%8);
483 else
484 CLRBIT((*fABFlags)[npix/8], npix%8);
485
486 fConnectedPixels++;
487}
488
489// --------------------------------------------------------------------------
490//
491// Fills members with information from a magic binary file.
492// WARNING: you have to use Init() before you can do this
493//
494/*
495void MRawEvtData::ReadEvt(istream &fin, Int_t posinarray)
496{
497
498 const UShort_t npic = fRunHeader->GetNumPixInCrate();
499
500 const UShort_t npos = npic*posinarray;
501
502 //const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
503
504 for (int i=npos; i<npic+npos; i++)
505 {
506 // calc the spiral hardware pixel number
507 const UShort_t ipos = i;
508
509 // Get Hardware Id
510 const Short_t hwid = fRunHeader->GetPixAssignment(ipos);
511
512 // Check whether the pixel is connected or not
513 if (hwid==0)
514 {
515 const UShort_t n = fRunHeader->GetNumSamplesLoGain()+fRunHeader->GetNumSamplesHiGain();
516 fin.seekg(n, ios::cur);
517 return;
518 }
519
520 // -1 converts the hardware pixel Id into the software pixel index
521 const Int_t npix = (Int_t)hwid-1;
522
523 const Byte_t ab = fCrateArray->GetEntry(posinarray)->GetABFlags();
524 AddPixel(fin, npix, TESTBIT(ab, i-npos));
525 }
526}
527*/
528
529// --------------------------------------------------------------------------
530//
531// Return the size in bytes of one event data block
532//
533Int_t MRawEvtData::GetNumBytes() const
534{
535 const UShort_t nlo = fRunHeader->GetNumSamplesLoGain();
536 const UShort_t nhi = fRunHeader->GetNumSamplesHiGain();
537 const UShort_t npic = fRunHeader->GetNumPixInCrate();
538
539 return (nhi+nlo)*npic;
540}
541
542// --------------------------------------------------------------------------
543//
544// Make sure, that you skip the whole event. This function only skips a part
545// of the event - see MRawRead::SkipEvent
546//
547void MRawEvtData::SkipEvt(istream &fin)
548{
549 fin.seekg(GetNumBytes(), ios::cur);
550}
551
552Bool_t MRawEvtData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
553{
554 MRawEvtPixelIter Next(const_cast<MRawEvtData*>(this));
555 if (!Next.Jump(idx))
556 return kFALSE;
557
558 switch (type)
559 {
560 case 0:
561 val = Next.GetSumHiGainSamples()-(float)GetNumHiGainSamples()*fHiGainFadcSamples->GetArray()[0];
562 val*= cam.GetPixRatio(idx);
563 break;
564 case 1:
565 val = Next.GetMaxHiGainSample();
566 break;
567 case 2:
568 val = Next.GetMaxLoGainSample();
569 break;
570 case 3:
571 val = Next.GetIdxMaxHiGainSample();
572 break;
573 case 4:
574 val = Next.GetIdxMaxLoGainSample();
575 break;
576 case 5:
577 val = Next.GetIdxMaxHiLoGainSample();
578 return val >= 0;
579 }
580
581 return kTRUE;
582}
583
584void MRawEvtData::Copy(TObject &named)
585#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
586const
587#endif
588{
589 MRawEvtData &evt = (MRawEvtData &)named;
590
591 *evt.fHiGainPixId = *fHiGainPixId;
592 *evt.fLoGainPixId = *fLoGainPixId;
593
594 *evt.fHiGainFadcSamples = *fHiGainFadcSamples;
595 *evt.fLoGainFadcSamples = *fLoGainFadcSamples;
596
597 *evt.fABFlags = *fABFlags;
598
599 evt.fConnectedPixels = fConnectedPixels;
600}
Note: See TracBrowser for help on using the repository browser.