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

Last change on this file since 2563 was 2546, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 14.8 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 histhi->SetMinimum(0);
272 histhi->SetMaximum(255);
273
274 histhi->SetXTitle("Time/FADC Slices");
275 histhi->SetYTitle("Signal/FADC Units");
276
277 if (nl>0)
278 {
279 TGraph *graphlo = new TGraph;
280
281 for (int i=0; i<nl; i++)
282 graphlo->SetPoint(graphlo->GetN(), i, logains[i]);
283
284 graphlo->SetMaximum(256);
285 graphlo->SetMinimum(0);
286 graphlo->SetLineColor(kBlue);
287
288 graphlo->SetBit(kCanDelete);
289 graphlo->Draw("C*");
290
291 TH1F *histlo = graphlo->GetHistogram();
292
293 histlo->SetXTitle("Time/FADC Slices");
294 histlo->SetYTitle("Signal/FADC Units");
295 }
296
297 return;
298 }
299
300 if (str.BeginsWith("hist"))
301 {
302 // FIXME: Add Legend
303 *fLog << inf << "Drawing Histogram of Pixel with Idx #" << dec << pix.GetPixelId() << " to " << gPad << endl;
304
305 TH1F *histh = new TH1F(name, "FADC Samples", nh, -0.5, nh-.5);
306 histh->SetMinimum(0);
307 histh->SetMaximum(255);
308 histh->SetXTitle("Time [FADC Slices]");
309 histh->SetYTitle("Signal [FADC Units]");
310 histh->SetDirectory(NULL);
311 for (int i=0; i<nh; i++)
312 histh->Fill(i, higains[i]);
313 histh->SetBit(kCanDelete);
314 histh->Draw(same ? "same" : "");
315
316 if (nl>0)
317 {
318 TH1F *histl = new TH1F(name+";2", "FADC Samples", nl, -0.5, nl-.5);
319 histl->SetLineColor(kBlue);
320 histl->SetDirectory(NULL);
321 for (int i=0; i<nl; i++)
322 histl->Fill(i, logains[i]);
323 histl->SetBit(kCanDelete);
324 histl->Draw("same");
325 }
326 return;
327 }
328
329 *fLog << warn << dbginf << "Warning - You must specify either 'GRAPH' or 'HIST'" << endl;
330}
331
332// --------------------------------------------------------------------------
333//
334// Deletes all arrays describing the pixel Id and Samples in pixels.
335// The flag is for future usage.
336//
337void MRawEvtData::DeletePixels(Bool_t flag)
338{
339 if (fRunHeader && flag)
340 {
341 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
342 const int npix = fRunHeader->GetNumConnectedPixels();
343
344 if (fArraySize == npix)
345 {
346 fPosInArray = 0;
347 fConnectedPixels = 0;
348 return;
349 }
350 }
351
352 DeleteArrays();
353 InitArrays(flag);
354}
355
356// --------------------------------------------------------------------------
357//
358// Deletes all the arrays
359//
360void MRawEvtData::DeleteArrays()
361{
362 delete fHiGainPixId;
363 delete fLoGainPixId;
364 delete fHiGainFadcSamples;
365 delete fLoGainFadcSamples;
366}
367
368// --------------------------------------------------------------------------
369//
370// Deletes all the arrays
371// The flag is for future usage.
372//
373void MRawEvtData::InitArrays(Bool_t flag)
374{
375 if (flag && fRunHeader)
376 {
377 //const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
378 const int npix = fRunHeader->GetNumConnectedPixels();
379
380 fHiGainPixId = new MArrayS(npix);
381 fLoGainPixId = new MArrayS(npix);
382 fHiGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesHiGain());
383 fLoGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesLoGain());
384
385 fArraySize = npix;
386 }
387 else
388 {
389 fHiGainPixId = new MArrayS(0);
390 fLoGainPixId = new MArrayS(0);
391 fHiGainFadcSamples = new MArrayB(0);
392 fLoGainFadcSamples = new MArrayB(0);
393
394 fArraySize = 0;
395 }
396
397 fPosInArray = 0;
398 fConnectedPixels = 0;
399}
400
401// --------------------------------------------------------------------------
402//
403// This is to fill the data of one pixel to the MRawEvtHeader Class.
404// The parameters are the pixelnumber and the FADC_SLICES values of ADCs
405// Add to lo gains if lflag = 1
406//
407void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag)
408{
409 MArrayS *arrpix = lflag ? fLoGainPixId : fHiGainPixId;
410 MArrayB *arrsam = lflag ? fLoGainFadcSamples : fHiGainFadcSamples;
411
412 //
413 // check whether we got the right number of new samples
414 // if there are no samples already stored: this is the new number of samples
415 //
416 const Byte_t ns = data->GetSize();
417 const Byte_t nSamp = lflag ? GetNumLoGainSamples() : GetNumHiGainSamples();
418 if (nSamp && ns!=nSamp)
419 {
420 *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
421 *fLog << "TArrayC doesn't match actual number" << endl;
422 return;
423 }
424
425 //
426 // enhance pixel array by one
427 //
428 arrpix->Set(arrpix->GetSize()+1);
429
430 //
431 // add the number of the new pixel to the array as last entry
432 //
433 arrpix->AddAt(nOfPixel, arrpix->GetSize()-1);
434
435 //
436 // enhance the array by the number of new samples
437 //
438 arrsam->Set(arrsam->GetSize()+ns);
439
440 //
441 // add the new slices as last entries to array
442 //
443 arrsam->AddAt((Byte_t*)data->GetArray(), arrsam->GetSize()-ns, ns);
444}
445
446// --------------------------------------------------------------------------
447//
448// Fills members with information from a magic binary file.
449// WARNING: you have to use Init() before you can do this
450//
451void MRawEvtData::ReadEvt(istream &fin)
452{
453 const UShort_t nlo = fRunHeader->GetNumSamplesLoGain();
454 const UShort_t nhi = fRunHeader->GetNumSamplesHiGain();
455
456 const UShort_t npic = fRunHeader->GetNumPixInCrate();
457
458 const UShort_t npos = npic*fPosInArray;
459
460 Byte_t *higainsam = fHiGainFadcSamples->GetArray()+nhi*fConnectedPixels;
461 Byte_t *logainsam = fLoGainFadcSamples->GetArray()+nlo*fConnectedPixels;
462
463 for (int i=0; i<npic; i++)
464 {
465 fin.read((char*)higainsam, nhi);
466 fin.read((char*)logainsam, nlo);
467
468 // calc the spiral hardware pixel number
469 const UShort_t ipos = npos+i;
470
471 // -1 converts the hardware pixel Id into the software pixel index
472 const Int_t npix = (Int_t)fRunHeader->GetPixAssignment(ipos)-1;
473
474 // Check whether the pixel is connected or not
475 if (npix<0)
476 continue;
477
478 //
479 // This is to fill the data of one pixel to the MRawEvtHeader Class.
480 // The parameters are the pixelnumber and the FADC_SLICES values of ADCs
481 // Add to lo gains if lflag = 1
482 //
483 fHiGainPixId->AddAt(npix, fConnectedPixels);
484 higainsam += nhi;
485
486 // FIXME: Not implemented in the raw files yet
487 //if (IsLoGainOn(i, j))
488 //{
489 fLoGainPixId->AddAt(npix, fConnectedPixels);
490 logainsam += nlo;
491 //}
492
493 fConnectedPixels++;
494 }
495
496 fPosInArray++;
497}
498
499Bool_t MRawEvtData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
500{
501 MRawEvtPixelIter Next(const_cast<MRawEvtData*>(this));
502 if (!Next.Jump(idx))
503 return kFALSE;
504
505 switch (type)
506 {
507 case 0:
508 val = Next.GetSumHiGainSamples()-(float)GetNumHiGainSamples()*fHiGainFadcSamples->GetArray()[0];
509 val*= cam.GetPixRatio(idx);
510 break;
511 case 1:
512 val = Next.GetMaxHiGainSample();
513 break;
514 case 2:
515 val = Next.GetMaxLoGainSample();
516 break;
517 case 3:
518 val = Next.GetIdxMaxHiGainSample();
519 break;
520 case 4:
521 val = Next.GetIdxMaxLoGainSample();
522 break;
523 }
524
525 return kTRUE;
526}
527
528void MRawEvtData::Copy(TObject &named)
529#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
530const
531#endif
532{
533 MRawEvtData &evt = (MRawEvtData &)named;
534
535 *evt.fHiGainPixId = *fHiGainPixId;
536 *evt.fLoGainPixId = *fLoGainPixId;
537
538 *evt.fHiGainFadcSamples = *fHiGainFadcSamples;
539 *evt.fLoGainFadcSamples = *fLoGainFadcSamples;
540
541 evt.fPosInArray = fPosInArray;
542 evt.fConnectedPixels = fConnectedPixels;
543 evt.fArraySize = fArraySize;
544}
Note: See TracBrowser for help on using the repository browser.