source: trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc@ 1869

Last change on this file since 1869 was 1849, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 34.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 11/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MCT1ReadPreProc
28//
29// Reads a output file of the CT1 preproc.
30//
31// Implements usage of a selector (see MRead) Use such a filter to skip
32// events before reading! But never use a filter which needs read data
33// as input...
34//
35// Input Containers:
36// -/-
37//
38// Output Containers:
39// MCerPhotEvt the data container for all data.
40// MPedestalCam ct1 pedestals
41// MMcEvt monte carlo data container for MC files
42// MMcTrig mc data container for trigger information
43// MSrcPosCam source position in the camera
44// MBlindPixels Array holding blind pixels
45//
46/////////////////////////////////////////////////////////////////////////////
47#include "MCT1ReadPreProc.h"
48
49#include <fstream.h>
50
51#include <TList.h>
52#include <TSystem.h>
53
54#define LINUX
55#define HISTO void
56#define HBOOK_FILE int
57#include "defines.h"
58#include "structures.h"
59
60#include "MLog.h"
61#include "MLogManip.h"
62
63#include "MTime.h"
64#include "MFilter.h"
65
66#include "MParList.h"
67#include "MCerPhotEvt.h"
68
69#include "MPedestalPix.h"
70#include "MPedestalCam.h"
71
72#include "MGeomCam.h"
73#include "MSrcPosCam.h"
74#include "MBlindPixels.h"
75
76#include "MRawRunHeader.h"
77#include "MTaskList.h"
78
79#include "MMcEvt.hxx"
80#include "MMcTrig.hxx"
81
82ClassImp(MCT1ReadPreProc);
83
84// --------------------------------------------------------------------------
85//
86// Default constructor. Creates an array which stores the file names of
87// the files which should be read. If a filename is given it is added
88// to the list.
89//
90MCT1ReadPreProc::MCT1ReadPreProc(const char *fname, const char *name,
91 const char *title) : fIn(NULL), fEntries(0)
92{
93 fName = name ? name : "MRead";
94 fTitle = title ? title : "Reads a CT1 preproc data file";
95
96 //
97 // remember file name for opening the file in the preprocessor
98 //
99 fFileNames = new TList;
100 fFileNames->SetOwner();
101
102 if (fname)
103 AddFile(fname);
104}
105
106// --------------------------------------------------------------------------
107//
108// Delete the filename list and the input stream if one exists.
109//
110MCT1ReadPreProc::~MCT1ReadPreProc()
111{
112 delete fFileNames;
113 if (fIn)
114 delete fIn;
115}
116
117// --------------------------------------------------------------------------
118//
119// Add this file as the last entry in the chain
120//
121void MCT1ReadPreProc::AddFile(const char *txt)
122{
123 const char *name = gSystem->ExpandPathName(txt);
124
125 TString fname(name);
126 delete [] name;
127
128 if (!CheckHeader(fname))
129 {
130 *fLog << warn << "WARNING - Problem reading header... ignored." << endl;
131 return;
132 }
133
134 const Int_t n = GetNumEvents(fname);
135 if (n==0)
136 {
137 *fLog << warn << "WARNING - File contains no data... ignored." << endl;
138 return;
139 }
140
141 fEntries += n;
142
143 *fLog << inf << "File " << txt << " contains " << n << " events (Total=" << fEntries << ")" << endl;
144
145 fFileNames->AddLast(new TNamed(txt, ""));
146}
147
148// --------------------------------------------------------------------------
149//
150// Print data from the header to the screen and analyse the header data,
151// means store and copy the needed data into Mars structures and
152// data members
153//
154void MCT1ReadPreProc::ProcessRunHeader(const struct outputpars &outpars)
155{
156 if (outpars.inumpixels != iMAXNUMPIX)
157 *fLog << warn << "WARNING! File doesn't contain " << iMAXNUMPIX << " Pixels... maybe corrupt." << endl;
158
159 fNumEventsInRun = 0;
160
161 //
162 // ------------------- Output some stuff -----------------------
163 //
164
165 // int itelescope; // number of the CT which took the data
166 *fLog << inf << "Telescope: CT" << outpars.itelescope;
167
168 // float flongitude_deg; // longitude (counted positive towards West) of CT position */
169 // float flatitude_deg; // latitude (counted positive towards North) of CT position */
170 *fLog << " located @ Longitude=" << outpars.flongitude_deg;
171 *fLog << "deg Latitude=" << outpars.flatitude_deg << "deg" << endl;
172
173 // int irunnum; // run number (from parameters file)
174 *fLog << inf << "Run number" << outpars.irunnum;
175
176 fRawRunHeader->SetRunNumber(outpars.irunnum);
177 fRawRunHeader->SetReadyToSave();
178
179 // enum onoroff {NEITHER_ON_NOR_OFF, OFF_SOURCE, ON_SOURCE} eruntype; // runtype
180 *fLog << "Run: #" << outpars.irunnum << " (";
181 switch (outpars.eruntype)
182 {
183 case NEITHER_ON_NOR_OFF: *fLog << "unknown"; break;
184 case OFF_SOURCE: *fLog << "off-source"; break;
185 case ON_SOURCE: *fLog << "on-source"; break;
186 default: *fLog << (int)outpars.eruntype; break;
187 }
188 *fLog << ", ";
189 switch (outpars.etrackmode)
190 {
191 case NORMAL: *fLog << "normal tracking"; break;
192 case REVERSE: *fLog << "reverse tracking"; break;
193 case DUNNO: *fLog << "unknown tracking"; break;
194 default: *fLog << (int)outpars.etrackmode; break;
195 }
196 *fLog << ")" << endl;
197
198 //double dsourcera_hours; // right ascension of observed source in hours
199 //double dsourcedec_deg; // declination of observed source in degrees
200 *fLog << "Source: RA=" << outpars.dsourcera_hours << "h DEC=";
201 *fLog << outpars.dsourcedec_deg << "deg" << endl;
202
203 //int inummuonpixels; // number of pixels in the muon shield
204 //int inumcointdcs; // number of coincidence tdcs recorded in the runfile
205 //float fpixdiameter_deg; // smallest pixel diameter (degrees) (from parameters file) */
206
207 // enum axes {RA, DEC, ALT, AZ} ese1_is; // name of the axis to which shaft encoder 1 is attached (implies the type of mount)
208 *fLog << "Shaftencoder 1 @ ";
209 switch (outpars.ese1_is)
210 {
211 case RA: *fLog << "RA"; break;
212 case DEC: *fLog << "DEC"; break;
213 case ALT: *fLog << "ALT"; break;
214 case AZ: *fLog << "AZ"; break;
215 default: *fLog << (int)outpars.ese1_is; break;
216 }
217 *fLog << endl;
218
219 // int isezeropos[2]; // zero position of shaftencoders 1 and 2 (from parameters file)
220 *fLog << "SE Zero: SE(1)=" << outpars.isezeropos[0] << " ";
221 *fLog << "SE(2)=" << outpars.isezeropos[1] << endl;
222
223 // int iaz_rev_track_corr; // correction for the azimuth shaft encoder (ALT/AZ mount only) in reverse tracking mode
224 // int ialt_rev_track_corr; // correction for the altitude shaft encoder (ALT/AZ mount only) in reverse tracking mode
225 *fLog << "Reverse tracking corrections: SE(az)=" << outpars.iaz_rev_track_corr;
226 *fLog << " SE(alt)=" << outpars.ialt_rev_track_corr << endl;
227
228 // float fbendingcorr; // bending correction factor (ALT/AZ mount only)
229 // float fextinction; // atmospheric extinction (typically taken from the Carlsberg Meridian Circle data)
230 *fLog << "Bending: Correction factor=" << outpars.fbendingcorr << " ";
231 *fLog << "Extinction=" << outpars.fextinction << endl;
232
233 // Boolean bdontusepix[iMAXNUMPIX]; // bdontusepix is set true if the pixel should not be used in image analysis, otherwise it is true;
234 fBlinds->Clear();
235 *fLog << "Don't use pixels: ";
236 for (int i=0; i<iMAXNUMPIX; i++)
237 if (outpars.bdontusepix[i])
238 {
239 *fLog << i << " ";
240 fBlinds->SetPixelBlind(i);
241 }
242 *fLog << endl;
243
244 *fLog << "Exclude: ";
245
246 // Boolean bexcludepix[iMAXNUMPIX];
247 for (int i=0; i<iMAXNUMPIX; i++)
248 if (outpars.bexcludepix[i])
249 {
250 *fLog << i << " ";
251 fBlinds->SetPixelBlind(i);
252 }
253 *fLog << endl;
254
255 fBlinds->SetReadyToSave();
256
257 /* bexcludepix[] is set TRUE (== exclude from pedestal, Laser
258 * calibration and the further analysis) when the Mean value
259 * of a pixel inside a pedestal Run is larger than 50 or ( || )
260 * if the pedestal RMS value of this pixel is larger than 5.0
261 * This is reflected in the (new for versions >= 0.4)
262 * variable "pixonoff" in the ntuple written by preproc:
263 * preproc.nt.hbook
264 *
265 * When the pixel is excluded by the user it will get a -2 otherwise
266 * pixonoff = 0.
267 * Additive to this a -1 is added when preproc excludes the pixel
268 * for a given Run. So the actual value tells you whether you caught
269 * it already by hand or not.
270 *
271 * A plot of pixonoff may also be handy to tell you the status of your
272 * ADC equipment. */
273
274 // float fphotoel_per_adccnt[iMAXNUMPIX]; // conversion factors for the pixel signals */
275 /*
276 float padc = outpars.fphotoel_per_adccnt[0];
277 *fLog << "Phe/ADC (pixel 0): " << padc << endl;
278 for (int i=0; i<iMAXNUMPIX; i++)
279 *fLog << outpars.fphotoel_per_adccnt[i] << " ";
280 *fLog << endl;
281 */
282 /*
283 --- USEFULL? NEEDED? ---
284 int irubminusutc_usecs; // difference between rubidium clock and UTC in microseconds
285 int isum_thresh_phot; // threshold for the total sum of photoelectrons filter
286 int i2out_thresh_phot; // threshold for the two-pixels-out-of-all software
287 int imuoncut_thresh_adccnt[iMAXNUMMUONPIX]; // thresholds for the muon cut
288 Boolean bmuon_suppression; // "Dolby" noise reduction flag
289 float ftolerated_pointerror_deg; // maximum tolerated pointing error in the position
290 */
291
292 // float fxpointcorr_deg; // pointing correction (to be added along the camera x axis) e.g. from point run */
293 // float fypointcorr_deg; // pointing correction (to be added along the camera y axis) e.g. from point run */
294 *fLog << "Pointing correction: dx=" << outpars.fxpointcorr_deg << "deg ";
295 *fLog << "dy=" << outpars.fypointcorr_deg << "deg" << endl;
296
297 // FIXME? Is x-y echanged between Mars CT1 geometry and CT1 definition?
298 fSrcPos->SetXY(-outpars.fypointcorr_deg/fGeom->GetConvMm2Deg(),
299 -outpars.fxpointcorr_deg/fGeom->GetConvMm2Deg());
300 fSrcPos->SetReadyToSave();
301
302 /*
303 --- USEFULL? NEEDED? ---
304 float fcamera_align_angle_deg; // the angle between the camera y-axis and the meridian when a culminating object is observed (defined counter-clockwise looking at the sky)
305 int iratecalc_numevents_odd; // number of events used in the rate calculation (must be odd)
306 int inumpedfile; // number of the pedestal file used
307 int inumpedrun; // number of the pedestal run used in the file (starting at 0)
308 int inumcalfile; // number of the calibration file used
309 int inumlaserrun; // number of the laserrun used in the file (starting at 0)
310 int inumtellogfile; // number of the TelLog file to be used
311 int inumtellogrun; // number of the tellog entry (Runnumber) used from the log file
312 int imcparticle; // CORSIKA-coded Monte Carlo particle type.
313 */
314
315 // ----- preprocessing results -----
316
317 // int istart_mjdate_day; // MJD of run start (first event) */
318 // int iend_mjdate_day; // MJD of run end (last event) */
319 // int irunduration_secs; // difference between start and end time (secs) */
320 *fLog << "Run Time: From " << outpars.istart_mjdate_day << " to ";
321 *fLog << outpars.iend_mjdate_day << " (MJD), Duration=";
322 *fLog << outpars.irunduration_secs/3600 << "h";
323 *fLog << (outpars.irunduration_secs/60)%60 << "m";
324 *fLog << outpars.irunduration_secs%60 << "s" << endl;
325
326 /*
327 --- USEFULL? NEEDED? ---
328 int iproc_mjdate; // MJD of data processing (i.e. creation of this file)
329 */
330
331 // int iproc_evts; // number of events processed */
332 *fLog << "Number of processed events: " << outpars.iproc_evts << endl;
333
334 // --- USEFULL? NEEDED? ---
335 // double dactual_sourcera_hours; // for off runs: the false source (that should have been) observed */
336
337 // float frms_pedsig_phot[iMAXNUMPIX]; // standard deviation of the calibrated signals from the pedestal run */
338 fPedest->InitSize(iMAXNUMPIX);
339
340 fPedRMS.Set(iMAXNUMPIX);
341
342 *fLog << "PedestalRMS : ";
343 for (Int_t i=0; i<iMAXNUMPIX; i++)
344 {
345 (*fPedest)[i].SetMeanRms(outpars.frms_pedsig_phot[i]);
346 *fLog << outpars.frms_pedsig_phot[i] << " ";
347 fPedRMS[i] = outpars.frms_pedsig_phot[i];
348 }
349 *fLog << endl;
350
351 fPedest->SetReadyToSave();
352
353 // Used to communicate the mean over all pixels
354 // pedestal RMS into the Runs NTuple, as it might
355 // be used for e.g. quality cuts.
356 // float fpedrms_mean;
357 *fLog << "Pedestal RMS: " << outpars.fpedrms_mean << endl;
358
359 // The average current over the active pixels
360 // for this run. This is done separately for
361 // ON and OF runs.
362 //float fcurrent_mean;
363
364 // enum eERRORTOLERANCE {CAUTIOUS=0, GOODPHYSICS, TANK} eerrortolerance;
365 /* 0 == "cautious", exits on any reason (but tells in
366 * the .err file,
367 * 1 == "goodphysics", exits when physics could be affected
368 * by the error,
369 * 2 == "tank", never exits except on coredumps and when
370 * all files have been processed. Do not use such files for
371 * physics analysis!
372 *
373 * NOTE: the capital letter words are the enums, the small letter
374 * words must be used inside the parameter file. */
375
376 // enum eMCTRIGGERFLAG {ALL=0, FLAG, NOFLAG} emctriggerflag;
377 /* all: all events which survive the filter are written to the
378 * events NTuple.
379 * flag: When Dorota's triggerflag is set to 1 for a particular
380 * event, it shall be written to the output. All others shall
381 * just be disregarded. (Default)
382 * noflag: Opposite of 'flag': only events with triggerflag = 0 shall
383 * be treated further. */
384
385 *fLog << "Particle Id #" << outpars.imcparticle << endl;
386 *fLog << "Right Ascension: " << outpars.dsourcera_hours << "h" << endl;
387 *fLog << "Declination: " << outpars.dsourcedec_deg << "deg" << endl;
388
389 // Next statement commented out because bmontecarlo was set wrongly
390 //fIsMcFile = outpars.bmontecarlo==TRUE;
391 fIsMcFile = (outpars.dsourcera_hours == 0.0 &&
392 outpars.dsourcedec_deg == 0.0 &&
393 outpars.imcparticle != 0 );
394
395 if (fIsMcFile != (outpars.bmontecarlo==TRUE))
396 {
397 *fLog << "File tells you that it is a ";
398 *fLog << (outpars.bmontecarlo ? "Monte Carlo" : "Real Data");
399 *fLog << " file." << endl;
400 }
401
402 *fLog << "File detected as a ";
403 *fLog << (fIsMcFile ? "Monte Carlo" : "Real Data");
404 *fLog << " file." << endl;
405 *fLog << " " << endl;
406}
407
408// --------------------------------------------------------------------------
409//
410// Read CT1 PreProc File Header:
411//
412Int_t MCT1ReadPreProc::ReadRunHeader()
413{
414 char cheadertitle[iHEADERTITLELENGTH];
415 fIn->read(cheadertitle, iHEADERTITLELENGTH);
416
417 TString s = cheadertitle;
418 TString m = cTITLE_TEMPLATE;
419
420 if (!s.BeginsWith(m(0, m.First('%'))))
421 return kFALSE;
422
423 *fLog << cheadertitle << flush;
424
425 // cTITLE_TEMPLATE "PREPROC V%f/S%f CT %d RUN %d %d PROCMJD %d\n"
426 struct outputpars outpars;
427
428 int dummy;
429
430 Float_t fpreprocversion, structversion;
431 sscanf(cheadertitle, cTITLE_TEMPLATE,
432 &fpreprocversion, &structversion,
433 &outpars.itelescope, &outpars.irunnum,
434 &dummy/*&outpars.eruntype*/, &outpars.iproc_mjdate);
435
436 if (fpreprocversion<0.6)
437 {
438 *fLog << err << "Sorry, only files from PreProc V0.6 and newer are supported." << endl;
439 return kFALSE;
440 }
441
442 //
443 // This is a stupid way of getting rid of numerical uncertanties when
444 // comparing floating point numbers (Argh...)
445 //
446 TString s1 = Form("%.2f", structversion);
447 TString s2 = Form("%.2f", STRUCT_VERSION);
448
449 if (s1 != s2)
450 {
451 *fLog << warn << "WARNING: Version of C-structures of file (V";
452 *fLog << s1 << ") not identical with current structures (V";
453 *fLog << s2 << ")" << endl;
454 }
455
456 fIn->read((Byte_t*)&outpars, sizeof(struct outputpars));
457
458 ProcessRunHeader(outpars);
459
460 //rwagner: ReInit whenever new run commences
461 // rc==-1 means: ReInit didn't work out
462
463 MTaskList *tlist = (MTaskList*)fParList->FindCreateObj("MTaskList");
464 if (!tlist)
465 return -1;
466
467 if (!tlist->ReInit(fParList))
468 return -1;
469
470 return kTRUE;
471}
472
473Int_t MCT1ReadPreProc::ReadRunFooter()
474{
475 char cheadertitle[iHEADERTITLELENGTH];
476 fIn->read(cheadertitle, iHEADERTITLELENGTH);
477 /*
478 sscanf(cheadertitle, cEND_EVENTS_TEMPLATE,
479 &filterres.ifilter_passed_evts);
480 */
481
482 TString s = cheadertitle;
483 TString m = cEND_EVENTS_TEMPLATE;
484 Int_t p = m.First('%');
485
486
487 if (!s.BeginsWith(m(0,p)))
488 {
489 fIn->seekg(-iHEADERTITLELENGTH, ios::cur);
490 return 0;
491 }
492
493 *fLog << inf << cheadertitle << flush;
494
495 struct filterresults filterres;
496 fIn->read((Byte_t*)&filterres, sizeof(struct filterresults));
497 /*
498 int imax_alt_arcs; // maximum altitude reached during the run
499 int iaz_at_max_alt_arcs; // azimuth at the time the max. alt. was reached
500 int itimeaverage_alt_arcs; // altitude averaged over the runtime
501 int icoord_inconsist_evts; // number of events with time-coordinate inconsistency in this run
502 int ifilter_passed_evts; // number of events which passed the filter
503 int imuon_fail_evts; // number of events rejected as muons (other filters passed)
504 int i2out_fail_evts; // number of events which failed in the two out of all pixels software trigger
505 int imuon_and_2out_fail_evts; // number of events failing in both muon and 2out filter
506 int isum_fail_evts; // number of events which failed the sum-of-all-calibrated ADC counts filter
507 int isum_and_muon_fail_evts; // number of events which failed in both the sum and the muon filter
508 int isum_and_2out_fail_evts; // number of events which failed in both the sum and the 2out filter
509 int iall_filters_fail_evts; // number of events which failed in all filters
510 float favg_event_rate_hz; // average rate before filtering
511 float fstddev_event_rate_hz; // standard deviation of the rate before filtering
512 */
513
514 if (fNumEventsInRun!=(UInt_t)filterres.ifilter_passed_evts)
515 {
516 *fLog << err << "ERROR! Number of events in run (" << (UInt_t)filterres.ifilter_passed_evts;
517 *fLog << ") doesn't match number of read events (";
518 *fLog << fNumEventsInRun << ")" << endl;
519 *fLog << " File corrupted." << endl;
520 return -1;
521 }
522
523 fNumFilterEvts += fNumEventsInRun;
524 fNumRuns++;
525
526 *fLog << inf << "Read " << fNumEventsInRun << " events from run (Total=";
527 *fLog << fNumFilterEvts << "/" << fEntries << " [";
528 *fLog << 100*fNumFilterEvts/fEntries << "%], Runs=" << fNumRuns << ")";
529 *fLog << endl;
530
531 return 1;
532}
533
534// --------------------------------------------------------------------------
535//
536// This opens the next file in the list and deletes its name from the list.
537//
538Bool_t MCT1ReadPreProc::OpenNextFile()
539{
540 //
541 // open the input stream and check if it is really open (file exists?)
542 //
543 if (fIn)
544 delete fIn;
545 fIn = NULL;
546
547 //
548 // Check for the existence of a next file to read
549 //
550 TNamed *file = (TNamed*)fFileNames->First();
551 if (!file)
552 return kFALSE;
553
554 //
555 // open the file which is the first one in the chain
556 //
557 const TString name = file->GetName();
558
559 const char *expname = gSystem->ExpandPathName(name);
560 const TString fname(expname);
561 delete [] expname;
562
563 //
564 // Remove this file from the list of pending files
565 //
566 fFileNames->Remove(file);
567
568 *fLog << inf << "Open file: '" << name << "'" << endl;
569
570 if (!CheckHeader(fname))
571 {
572 *fLog << "OpenNextFile : CheckHeader(fname) is FALSE" << endl;
573 return kFALSE;
574 }
575
576 fIn = new ifstream(fname);
577
578 *fLog << inf << "-----------------------------------------------------------------------" << endl;
579
580
581 switch (ReadRunHeader())
582 {
583 case kFALSE:
584 *fLog << warn << "Unable to read first run header... skipping file." << endl;
585 return kFALSE;
586 case -1:
587 *fLog << warn << "ReInit of Tasklist didn't succeed." << endl;
588 return kFALSE;
589 default:
590 *fLog << "After opening next file: Number of Events #" << fNumEventsInRun << endl;
591 return kTRUE;
592 }
593}
594
595Bool_t MCT1ReadPreProc::CheckHeader(const TString fname) const
596{
597 ifstream fin(fname);
598 if (!fin)
599 {
600 *fLog << dbginf << err << "ERROR - Cannot open file '" << fname << "'" << endl;
601 return kFALSE;
602 }
603
604 char cheadertitle[iHEADERTITLELENGTH];
605 fin.read(cheadertitle, iHEADERTITLELENGTH);
606
607 Float_t fpreprocversion, structversion;
608 Int_t dummyi;
609
610 sscanf(cheadertitle, cTITLE_TEMPLATE,
611 &fpreprocversion, &structversion,
612 &dummyi, &dummyi, &dummyi, &dummyi);
613
614 if (fpreprocversion < 0.6)
615 {
616 *fLog << dbginf << err << "ERROR - You must use PreProc V0.6 or higher." << endl;
617 return kFALSE;
618 }
619
620 if (STRUCT_VERSION > structversion)
621 {
622 *fLog << warn << "WARNING: Version of C-structures of file (V";
623 *fLog << structversion << ") newer than current structures (V";
624 *fLog << STRUCT_VERSION << ")" << endl;
625 }
626
627 *fLog << "Current structures: " << STRUCT_VERSION << " ";
628 *fLog << "Structures in file: " << structversion << " ";
629 *fLog << "Used preproc version: " << fpreprocversion << endl;
630
631 return kTRUE;
632}
633
634
635Int_t MCT1ReadPreProc::GetNumEvents(const TString fname) const
636{
637 *fLog << inf << "Scanning file " << fname << " for size" << flush;
638
639 ifstream fin(fname);
640 if (!fin)
641 {
642 *fLog << dbginf << err << "ERROR - Opening file." << endl;
643 return 0;
644 }
645
646 const TString m(cEND_EVENTS_TEMPLATE);
647 const Int_t p = m.First('%');
648 const TString test = m(0, p);
649
650 Int_t nevts = 0;
651 Int_t nruns = 0;
652
653 while (!fin.eof() && fin.peek()!=EOF)
654 {
655 fin.seekg(iHEADERTITLELENGTH, ios::cur);
656 fin.seekg(sizeof(struct outputpars), ios::cur);
657
658 while (1)
659 {
660 if (fin.peek()==cEND_EVENTS_TEMPLATE[0])
661 {
662 char cheadertitle[iHEADERTITLELENGTH];
663 fin.read(cheadertitle, iHEADERTITLELENGTH);
664
665 const TString s = cheadertitle;
666 if (s.BeginsWith(test))
667 {
668 fin.seekg(sizeof(struct filterresults), ios::cur);
669 nruns++;
670 break;
671 }
672
673 fin.seekg(-iHEADERTITLELENGTH, ios::cur);
674 }
675
676 fin.seekg(sizeof(struct eventrecord), ios::cur);
677 if (fin.eof())
678 break;
679
680 nevts++;
681 }
682 *fLog << "." << flush;
683 }
684
685 *fLog << "done." << endl;
686 *fLog << "Found " << nevts << " events in " << nruns << " runs." << endl;
687
688 return nevts;
689}
690
691// --------------------------------------------------------------------------
692//
693// Open the first file in the list. Check for the output containers or create
694// them if they don't exist.
695//
696// Initialize the size of the MPedestalCam container to 127 pixels (CT1 camera)
697//
698Bool_t MCT1ReadPreProc::PreProcess(MParList *pList)
699{
700
701 fParList = pList;
702
703 //
704 // look for the MCerPhotEvt class in the plist
705 //
706 fNphot = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt");
707 if (!fNphot)
708 return kFALSE;
709
710 //
711 // look for the pedestal class in the plist
712 //
713 fPedest = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
714 if (!fPedest)
715 return kFALSE;
716
717 //
718 // look for the time class in the plist
719 //
720 fTime = (MTime*)pList->FindCreateObj("MTime");
721 if (!fTime)
722 return kFALSE;
723
724 //
725 // look for the pedestal class in the plist
726 //
727 fBlinds = (MBlindPixels*)pList->FindCreateObj("MBlindPixels");
728 if (!fBlinds)
729 return kFALSE;
730
731 //
732 // look for the source position in the camera
733 //
734 fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam");
735 if (!fSrcPos)
736 return kFALSE;
737
738 //
739 // look for the camera geometry
740 //
741 fGeom = (MGeomCam*)pList->FindCreateObj("MGeomCamCT1", "MGeomCam");
742 if (!fGeom)
743 return kFALSE;
744
745 //
746 // look for the mc event class
747 //
748 fMcEvt = (MMcEvt*)pList->FindCreateObj("MMcEvt");
749 if (!fMcEvt)
750 return kFALSE;
751
752 //
753 // look for the mc trigger class
754 //
755 fMcTrig = (MMcTrig*)pList->FindCreateObj("MMcTrig");
756 if (!fMcTrig)
757 return kFALSE;
758
759 //
760 // look for the raw run header class
761 //
762 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
763 if (!fRawRunHeader)
764 return kFALSE;
765
766 fNumFilterEvts = 0;
767 fNumEvents = 0;
768 fNumRuns = 0;
769
770 fPedest->InitSize(iMAXNUMPIX);
771
772 return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
773}
774
775// --------------------------------------------------------------------------
776//
777// Analyse the event data, means store and copy the needed data into
778// Mars structures and data members
779//
780void MCT1ReadPreProc::ProcessEvent(const struct eventrecord &event)
781{
782 /*
783 if (fRawRunHeader->GetRunNumber() == 1)
784 {
785 *fLog << "eventrecord" << endl;
786 *fLog << "isecs_since_midday = " << event.isecs_since_midday << endl;
787 *fLog << "isecfrac_200ns = " << event.isecfrac_200ns << endl;
788 *fLog << "snot_ok_flags = " << event.snot_ok_flags << endl;
789 *fLog << "ialt_arcs = " << event.ialt_arcs << endl;
790 *fLog << "iaz_arcs = " << event.iaz_arcs << endl;
791 *fLog << "ipreproc_alt_arcs = " << event.ipreproc_alt_arcs << endl;
792 *fLog << "ipreproc_az_arcs = " << event.ipreproc_az_arcs << endl;
793 *fLog << "ifieldrot_arcs = " << event.ifieldrot_arcs << endl;
794
795 *fLog << "srate_millihz = " << event.srate_millihz << endl;
796 *fLog << "fhourangle = " << event.fhourangle << endl;
797 *fLog << "fmcenergy_tev = " << event.fmcenergy_tev << endl;
798 *fLog << "fmcsize_phel = " << event.fmcsize_phel << endl;
799 *fLog << "imcimpact_m = " << event.imcimpact_m << endl;
800 *fLog << "imcparticle = " << event.imcparticle << endl;
801 *fLog << "imctriggerflag = " << event.imctriggerflag << endl;
802 }
803 */
804
805 for (Int_t i=0; i<iMAXNUMPIX; i++)
806 (*fPedest)[i].SetMeanRms(fPedRMS[i]);
807
808 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
809 // int isecfrac_200ns; // fractional part of isecs_since_midday
810 fTime->SetTime(event.isecfrac_200ns, event.isecs_since_midday);
811 fTime->SetReadyToSave();
812
813 /*
814 --- USEFULL? NEEDED? ---
815 short snot_ok_flags; // the bits in these two bytes are flags for additional information on the event: Everything OK =: all Bits = 0
816
817 // for ALT-AZ mount telescopes: rotation angle of the field of
818 // view; this angle is defined mathematically positive looking
819 // towards the sky as the angle between the hour circle through
820 // the object being tracked and the line through pixel 1 and 2
821 int ifieldrot_arcs;
822
823 // event rate in milli Hertz before filtering calculated by
824 // iratecalc_numevents_odd/(time[i+iratecalc_numevents_odd/2] -
825 // time[i-iratecalc_numevents_odd/2])
826 // For the first and the last iratecalc_numevents_odd/2
827 // events the rate is assumed to be constant
828 unsigned short srate_millihz;
829
830 // This is the angle between the observation of this event and the
831 // culmination point. It is going to be written into the events NTuple.
832 float fhourangle;
833 */
834
835 //
836 // read in the number of cerenkov photons and add the 'new' pixel
837 // too the list with it's id, number of photons and error
838 //
839 fNphot->InitSize(iMAXNUMPIX);
840
841 // number of photoelectrons measured in each pixel only the
842 // actual number of pixels (outputpars.inumpixels) is written out
843 // short spixsig_10thphot[iMAXNUMPIX];
844 //*fLog << "spixsig_10thphot : " << endl;
845 for (Int_t i=0; i<iMAXNUMPIX; i++)
846 {
847 //*fLog << event.spixsig_10thphot[i] << " ";
848
849 if (event.spixsig_10thphot[i]==0)
850 continue;
851
852 fNphot->AddPixel(i, 0.1*event.spixsig_10thphot[i],
853 (*fPedest)[i].GetMeanRms());
854 }
855 //*fLog << "" << endl;
856
857 fNphot->SetReadyToSave();
858
859 // int ipreproc_alt_arcs; // "should be" alt according to preproc (arcseconds)
860 // int ipreproc_az_arcs; // "should be" az according to preproc (arcseconds)
861
862 fMcEvt->Fill(event.isecs_since_midday, //0, /*fEvtNum*/
863 fIsMcFile ? event.imcparticle : 0, /*corsika particle type*/
864 fIsMcFile ? event.fmcenergy_tev*1000 : 0,
865 0, /* fThi0 */
866 0, /* fFirTar */
867 0, /* fzFirInt */
868 // 0, /* fThet*/
869 // rwagner: The following should be theta, right? Check with
870 // altitude fill some lines down...
871 0, // altitude (arcseconds)
872 0, /* fPhii */
873 0, /* fCorD */
874 0, /* fCorX */
875 0, /* fCorY */
876 fIsMcFile ? event.imcimpact_m*100 : 0,
877 TMath::Pi()/180*event.iaz_arcs/3600, // azimuth (arcseconds)
878 TMath::Pi()*(0.5-1./180*event.ialt_arcs/3600), // altitude (arcseconds)
879 0, /* fTFirst */
880 0, /* fTLast */
881 0, /* fL_Nmax */
882 0, /* fL_t0 */
883 0, /* fL_tmax */
884 0, /* fL_a */
885 0, /* fL_b */
886 0, /* fL_c */
887 0, /* fL_chi2 */
888 0, /* uiPin */
889 0, /* uiPat */
890 0, /* uiPre */
891 0, /* uiPco */
892 0, /* uiPelS */
893 fIsMcFile ? event.fmcsize_phel : 0, /* uiPelC, Simulated SIZE */
894 0, /* elec */
895 0, /* muon */
896 0 /* other */
897 );
898
899 fMcTrig->SetFirstLevel(event.imctriggerflag); // MC data from Dorota get a triggerflag: 1 means triggered, 0 not. */
900
901 fMcTrig->SetReadyToSave();
902 fMcEvt->SetReadyToSave();
903}
904
905// --------------------------------------------------------------------------
906//
907// Because of the file format of the preproc output we have to check at any
908// event where in the file stream we are...
909//
910Bool_t MCT1ReadPreProc::CheckFilePosition()
911{
912 //
913 // Means: If no file is open (first call) try to open the first file
914 //
915 if (!fIn)
916 return kFALSE;
917
918 //
919 // Because we can have 0-event runs in the file we loop as often
920 // as we don't find a new footer-header combination.
921 //
922 while (1)
923 {
924 //
925 // If the first character isn't the first of the footer it must be
926 // an event
927 //
928 if (fIn->peek()!=cEND_EVENTS_TEMPLATE[0])
929 return kTRUE;
930
931 //
932 // Try reading the footer. If this isn't successful...
933 // must be an event
934 //
935 switch (ReadRunFooter())
936 {
937 case -1:
938 return kFALSE;
939 case 0:
940 return kTRUE;
941 }
942
943 *fLog << inf << "Footer found." << endl;
944
945 const char c = fIn->peek();
946
947 //
948 // No after reading the footer check if we reached the end of the file
949 //
950 if (fIn->eof() || c==EOF)
951 {
952 *fLog << "End of file." << endl;
953 return kFALSE;
954 }
955
956 //
957 // If the eof isn't reached a new header must follow. Check for it.
958 //
959 if (c!=cTITLE_TEMPLATE[0])
960 {
961 *fLog << inf << "Error finding new run header in file (possible EOF)... skipping rest of file." << endl;
962 return kFALSE;
963 }
964
965 *fLog << "-----------------------------------------------------------------------" << endl;
966
967
968 if (ReadRunHeader() < 0)
969 {
970 *fLog << warn << "ReInit of Tasklist didn't succeed." << endl;
971 return kFALSE;
972 }
973 }
974}
975
976// --------------------------------------------------------------------------
977//
978// Check for the event number and depending on this number decide if
979// pedestals or event data has to be read.
980//
981// If the end of the file is reached try to open the next in the list. If
982// there is now next file stop the eventloop.
983//
984Bool_t MCT1ReadPreProc::Process()
985{
986 //
987 // Check where in the file we are. If neither a new event, nor a
988 // footer/header combination is detected go to the next file.
989 //
990 if (!CheckFilePosition())
991 if (!OpenNextFile())
992 return kFALSE;
993
994 //
995 // Check for a selector. If one is given and returns kFALSE
996 // skip this event.
997 //
998 if (GetSelector())
999 {
1000 //
1001 // Make sure selector is processed
1002 //
1003 if (!GetSelector()->CallProcess())
1004 {
1005 *fLog << err << dbginf << "Processing Selector failed." << endl;
1006 return kFALSE;
1007 }
1008
1009 //
1010 // Skip Event
1011 //
1012 if (!GetSelector()->IsExpressionTrue())
1013 {
1014 fIn->seekg(sizeof(struct eventrecord), ios::cur);
1015
1016 fNumEvents++;
1017 fNumEventsInRun++;
1018
1019 return kCONTINUE;
1020 }
1021 }
1022
1023 // event data to be read from the file
1024 struct eventrecord event;
1025
1026 // read the eventrecord from the file
1027 fIn->read((Byte_t*)&event, sizeof(struct eventrecord));
1028
1029 ProcessEvent(event);
1030
1031 fNumEvents++;
1032 fNumEventsInRun++;
1033
1034 return kTRUE;
1035}
1036
1037Bool_t MCT1ReadPreProc::PostProcess()
1038{
1039 *fLog << all;
1040 *fLog << "Number events passed the filter: " << fNumFilterEvts << endl;
1041 *fLog << "Number of Events read from file: " << fNumEvents << endl;
1042 *fLog << "Number of Runs read from file: " << fNumRuns << endl;
1043 *fLog << "Number of events detected first: " << fEntries << endl;
1044
1045 if (fNumEvents!=fNumFilterEvts)
1046 {
1047 *fLog << warn << "WARNING! Number of events in file doesn't match number of read events..." << endl;
1048 *fLog << " File might be corrupt." << endl;
1049 }
1050
1051 return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
1052}
Note: See TracBrowser for help on using the repository browser.