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

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