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

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