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

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