aboutsummaryrefslogtreecommitdiff
path: root/TODO
blob: 30779b1f16938051f68d11c3b5a28d6f06675de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Key:
    - To do
    p Partially done
    ^ Done
    ? Not sure if it's done

- write cross section section in paper
? explain integral in paper
    - not sure what integral I was talking about here. I just checked the git
      history and this was before I had written the fitter, so it must be
      talking about the integral to compute the expected event rate.
p develop signal generator
    - made signal generator for two particle decays
p develop high energy and multi-ring fitter
^ write talk for Monday
^ write a section on atmospheric backgrounds in paper
^ check index of refraction calculation against tables in the paper
^ write more tests for the solid angle calculation
    - use table from paper
^ make sure normal direction is normalized
^ speed up likelihood calculation
    - rotate and translate path points in path_init
    - create fast interp1d for equally spaced points
    - optimize with -O2
    - only calculate average time if mu_direct[i] is not too small and PMT is
      hit
    - add likelihood function which calculates total expected charge and only
      loops over hit PMTs
    - add fast likelihood which doesn't use a 2D lookup table
- add function to compute the Hessian via finite differences
- add function to calculate covariance matrix
- add function to compute the approximate volume in the likelihood space where
  the likelihood is greater than some value
^ add output to likelihood
^ add a set of seed points to start the likelihood
^ minimize the likelihood in a do while loop until it converges to a single
  point
^ once the first position is found, compute the Hough transform to look for
  other rings
p add rayleigh scattering to likelihood function
    - Done, but it's pretty hacky
p add muon shower PDF? delta rays?
    - Done for delta rays, but need to add shower photons
- calculate path length through acrylic?
    - did some tests on this and it seems to fix the radial energy bias for
      electrons, so it's definitely an issue. However, currently the small 5%
      energy bias is not really a big deal, although I would like to fix this
      eventually.
p update estar table with energies above 1 GeV
    - added values up to 10 GeV
^ speed up normalize() and interp1d()
^ add shower PDF to get_total_charge_approx()
x compute mean of starting points
^ speed up time PDF calculation for fast likelihood
p calculate psi parameter as a goodness of fit test
    - Still need to tweak this. It seems to have a strong nhit dependence
^ add a maximum length for the range
    - now only fit to the end of the PSUP
^ add a time limit for the fit
^ multi-particle fit
p optimize CHARGE_FRACTION
? find out why likelihood is sometimes returning nan
^ add pdf for cerenkov light from delta rays
^ tweak find peaks algorithm
^ update zebra to allow use of links
p minimum energy for each particle type
    - I do already have a minimum energy which is the Cerenkov threshold,
      however I think what I meant here was to have something significantly
      higher than that since a particle just at the Cerenkov threshold won't
      produce many PMT hits. I think I was mostly concerned about this issue
      because many fits were always fitting better by including a low energy
      electron, however I have solved that problem by simply dropping multi
      particle fits which have an electron with an energy less than 20 MeV.
^ check all zebra links
^ fix zero logical record size bug in zebra.c
p speed up fit
    - have optimized most of the likelihood function. I think further
      optimization will require a different technique for evaluating the
      integrals
^ nhit cut
p add flasher cut
p default max particles
    - submit-grid-jobs fits up to 3 particles by default
^ add environment variable to control where to look for pmt.txt and titles files
p update charge code to handle highest values for qhs and qlx
    - don't have this coded into the charge probability code, but I now use QHL
      if QHS is railed and flag any PMT with a railed QHL
^ use qhs and qlx based on variable in PMT bank
    - Note: I don't use the best charge variable in the PMT bank, but I do
      select QHL when QHS is railed
^ fix bug in path_init()
^ load DQXX file based on run number
^ update rayleigh scattering lengths
^ fix delta ray calculation
p figure out why electron energy bias is +10%
    - updated code to use a more accurate approximation for the number of
      shower photons which seems to bring the bias down to 5%
p update find peaks algorithm to do single particle quick fits
    - I did update the find peaks algorithm to work *much* better, but it still
      doesn't do single particle quick fits. I think the next improvement would
      be to add the ability to actually determine the number of rings.
^ figure out how to combine SNO fitter data with my fitter for final analysis.
  For example, how to apply neutron follower cut?
- double check that zdab-reprocess is correct for the D2O and salt phases since
  it appears to be from the NCD phase
? add code to compute expected deviation from nll_best to normalize psi
    - tried several different versions of this and nothing seemed to perform as
      well as psi/nhit.
^ add term to likelihood for probability that a channel is misccalibrated or
  didn't make it into the event
- when calculating the first order statistic for a gaussian is alpha = pi/8 a
  better choice than 0.375?
- speed up charge initialization
- determine *real* mean and standard deviation of single PE charge
  distribution. TELLIE SNO+ data?
^ extend electron range table
    - extended the electron range table up to 1 TeV
? fix time PDF. Currently take the first order statistic of dark noise +
  indirect light + direct light all together, but this isn't correct.
    - thought more about this and it's not actually obvious if what I'm doing
      is wrong. Need to think more about this.
a id='n458' href='#n458'>458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
//
// File:		Record_Info.h
//
// Description:	SNO datastream record definitions
//
// Revisions:   02/21/03 - PH Combined varioius versions of this file so now the exact
//                            same file is used by the builder, SHaRC, xsnoed and QSNO.
//              03/03/03 - PH Changed PmtEventRecord definition so it is now
//                            swapped like a set of 32-bit integers.
//              07/13/04 - PH Added new NCD run mask bits
//
// Notes:       For historical reasons, the structures used by SHaRC can be different
//              from the ones written to the data stream, so be careful which one you
//              are using!
//
#ifndef __RECORD_INFO_H__
#define __RECORD_INFO_H__

#include <stdint.h>

/*  version numbers...
 *  as of Feb 1997 (from UW) 
 */
#define DAQ_CODE_VERSION            0
#define RUN_RECORD_VERSION          0
#define PMT_RECORD_VERSION          0
#define TRIG_RECORD_VERSION         0
#define PTK_RECORD_VERSIONS			0x50544B30 // 'PTK0'
#define DAQ_RECORD_VERSIONS  		0x44415130 // 'DAQ0' 

#define WORD_SIZE(x)				(sizeof(x)/sizeof(uint32_t))

// -------------------------------------------------------------------------------------
//   generic information for every header
//   - preceeds ALL dispatcher records, and all records sent by SHaRC
//   - not written to ZDAB files
//
typedef struct GenericRecordHeader {
  uint32_t RecordID;
  uint32_t RecordLength;   //  length of record to follow, 
                                // NOT INCLUDING generic record header! 
  uint32_t RecordVersion;
} aGenericRecordHeader;

#define HEADERSIZE  sizeof( aGenericRecordHeader ) // 12 uint8_ts generic header.

// -------------------------------------------------------------------------------------
// SHaRC header for all manipulator records
//

// local time structure
typedef struct {
	uint8_t	year;				// last 2 digits of year
	uint8_t	month;				// month of year (1->12)
	uint8_t	day;				// day of month (1->31)
	uint8_t	daylightSavings;	// 0 = standard time, 1 = daylight savings time
    char    timeZone;           // hours from GMT (-11->11) (signed uint8_t!)
	uint8_t	hour;				// hour of day (0->23)
	uint8_t	minute;				// minute of hour (0->59)
	uint8_t	second;				// second of minute (0->59)
	uint32_t	microsecond;		// millionths of a second (0->999999)	
} SDateTime;

// header for all manipulator banks
typedef struct {
	uint32_t		instanceNumber;
	uint32_t		gtid_ref;			// reference GTID for this event
	SDateTime	time;
} SManipHeader;

// -------------------------------------------------------------------------------------
//   MAST record
//
#define MAST_RECORD    0x4d415354 // 'MAST'

// MAST bank structure
typedef struct MastRecord {
	float	currentVersion;		// current SNOMAN version
	float	originalVersion;	// original SNOMAN version
} SBankMAST;

// -------------------------------------------------------------------------------------
//   RHDR record
//
#define RHDR_RECORD    0x52484452 // 'RHDR'  (as written to ZDAB file)
#define RUN_RECORD     0x52554E20 // 'RUN '  (as dispatched and sent by SHaRC)

// RHDR bank structure
typedef struct RunRecord {
  uint32_t Date;
  uint32_t Time;
  uint32_t DAQCodeVersion;
  uint32_t RunNumber;
  uint32_t CalibrationTrialNumber;
  uint32_t SourceMask;	//   which sources in? 
  uint32_t RunMask;		//   run conditions 
#ifdef __MWERKS__
  uint32_t GTCrateMask;  // this run's GT crate mask
#else
  uint32_t GTCrateMsk;   // accomodate misspelling of this for backward compatibility
#endif
  uint32_t FirstGTID;   	// first GTID of this run
  uint32_t ValidGTID;	// first valid GTID (after hardware changes have been made)
  uint32_t Spares[8];	// spares as per nick's suggestion (Thanks Nick!)
} SBankRHDR, aRunRecord;

//   SourceMask... 
#define NO_SRC					0x00000UL
#define ROTATING_SRC            0x00001UL
#define LASER_SRC               0x00002UL
#define SONO_SRC                0x00004UL
#define N16_SRC                 0x00008UL
#define N17_SRC                 0x00010UL
#define NAI_SRC                 0x00020UL
#define LI8_SRC                 0x00040UL
#define PT_SRC                  0x00080UL
#define CF_HI_SRC               0x00100UL
#define CF_LO_SRC               0x00200UL
#define U_SRC                   0x00400UL
#define TH_SRC                  0x00800UL
#define P_LI7_SRC               0x01000UL
#define WATER_SAMPLER           0x02000UL
#define PROP_COUNTER_SRC        0x04000UL
#define SINGLE_NCD_SRC          0x08000UL
#define SELF_CALIB_SRC          0x10000UL
#define Y88_SRC                 0x20000UL

//   RunMask... 
#define NEUTRINO_RUN            0x0001UL
#define SOURCE_RUN              0x0002UL
#define CALIB_RUN               0x0004UL
#define NCD_RUN                 0x0008UL
#define SALT_RUN                0x0010UL
#define POISON_RUN              0x0020UL
#define PARTIAL_FILL_RUN        0x0040UL
#define AIR_FILL_RUN            0x0080UL
#define D2O_RUN                 0x0100UL
#define H2O_RUN                 0x0200UL
#define DCR_ACTIVITY_RUN        0x0400UL
#define TRANSITION_RUN          0x0800UL
#define SOURCE_MOVING_RUN       0x1000UL
#define COMP_COILS_RUN          0x2000UL
#define ECA_RUN                 0x4000UL
#define DIAGNOSTIC_RUN          0x8000UL
#define SUPERNOVA_RUN           0x10000UL
#define MAINTENANCE_RUN         0x20000UL
#define PCA_RUN                 0x40000UL
#define EXPERIMENTAL_RUN        0x80000UL
#define D2O_CIRC_RUN            0x100000UL
#define BUBBLERS_RUN            0x200000UL
#define PMT_OFF_RUN             0x01000000UL
#define NCD_OFF_RUN             0x02000000UL
#define NCD_ECA_RUN             0x04000000UL

// -------------------------------------------------------------------------------------
//   TRIG record 
//
#define TRIG_RECORD    0x54524947 // 'TRIG'

// TRIG bank structure
typedef struct TriggerInfo {
  uint32_t       TriggerMask;    //  which triggers were set? 
  uint32_t       n100lo;            // trigger Threshold settings
  uint32_t       n100med;           // these are longs cuz Josh is a weenie.
  uint32_t       n100hi;
  uint32_t       n20;
  uint32_t       n20lb;
  uint32_t       esumlo;
  uint32_t       esumhi;
  uint32_t       owln;
  uint32_t       owlelo;
  uint32_t       owlehi;
  uint32_t       n100lo_zero;    // trigger Threshold zeroes
  uint32_t       n100med_zero;
  uint32_t       n100hi_zero;
  uint32_t       n20_zero;
  uint32_t       n20lb_zero;
  uint32_t       esumlo_zero;
  uint32_t       esumhi_zero;
  uint32_t       owln_zero;
  uint32_t       owlelo_zero;
  uint32_t       owlehi_zero;
  uint32_t       PulserRate;        //  MTC local pulser 
  uint32_t       ControlRegister;  //  MTC control register status 
  uint32_t       reg_LockoutWidth; //  min. time btwn global triggers 
  uint32_t       reg_Prescale;     //  how many nhit_100_lo triggers to take 
  uint32_t       GTID;             //  to keep track of where I am in the world
} SBankTRIG, aTriggerInfo, *aTriggerInfoPtr;

// TriggerMask...
#define TRIG_NHIT_100_LO        0x00000001
#define TRIG_NHIT_100_MED       0x00000002
#define TRIG_NHIT_100_HI        0x00000004
#define TRIG_NHIT_20            0x00000008
#define TRIG_NHIT_20_LB         0x00000010
#define TRIG_ESUM_LO            0x00000020
#define TRIG_ESUM_HI            0x00000040
#define TRIG_OWLN               0x00000080
#define TRIG_OWLE_LO            0x00000100
#define TRIG_OWLE_HI            0x00000200
#define TRIG_PULSE_GT           0x00000400
#define TRIG_PRESCALE           0x00000800
#define TRIG_PEDESTAL           0x00001000
#define TRIG_PONG               0x00002000
#define TRIG_SYNC               0x00004000
#define TRIG_EXT_ASYNC          0x00008000
#define TRIG_HYDROPHONE         0x00010000
#define TRIG_EXT3               0x00020000
#define TRIG_EXT4               0x00040000
#define TRIG_EXT5               0x00080000
#define TRIG_EXT6               0x00100000
#define TRIG_NCD_SHAPER         0x00200000
#define TRIG_EXT8               0x00400000
#define TRIG_SPECIAL_RAW        0x00800000
#define TRIG_NCD_MUX            0x01000000
#define TRIG_SOFT_GT            0x02000000

// -------------------------------------------------------------------------------------
//   EPED record
//
#define EPED_RECORD    0x45504544 // 'EPED'

// EPED bank structure (as seen by SNOMAN)
typedef struct EpedRecord {
  uint32_t ped_width;
  uint32_t ped_delay_coarse;
  uint32_t ped_delay_fine;
  uint32_t qinj_dacsetting;
  uint32_t halfCrateID;
  uint32_t CalibrationType;
  uint32_t GTID;
  uint32_t Flag;
} SBankEPED;

// EPED record as sent by SHaRC
// (be very careful here!  for backward compatibility this is also
//  defined as EPEDRecord as used by SHaRC.  Not to be confused with EpedRecord.)
typedef struct EPEDRecord {
  uint32_t	Time;					// ptk used instead of aDate_Time
  uint16_t	CalibrationType;
  uint8_t		halfCrateID; 			// which 1/2 crate is enabled for pedestals
  uint8_t		reg_PedestalWidth;		// width of pedestal pulse for Qinj 
  uint8_t		reg_Ped_GTDel_Coarse;
  uint8_t		reg_Ped_GTDel_Fine;		// pedestal delay for T slopes 
  int16_t		Qinj_dacsetting;		// DAC setting for Qinj 
  uint32_t	MTCD_csr;
  uint32_t	GTID;					// GT Id validity range
  uint32_t	Flag;					// start/stop flag
  uint32_t	RunNumber;				// current run number
  uint32_t	Spares[5];
} SHaRC_BankEPED;

// CalibrationType...
#define EPED_Q_SLOPE_RUN        1
#define EPED_T_SLOPE_RUN        2
#define EPED_PED_RUN            3
#define EPED_SLOWPED_RUN        4

// Bit mask in halfCrateID...
#define EPED_FIRST_HALF         (uint8_t) 0
#define EPED_SECOND_HALF        (uint8_t) 0x80U

// Flag...
#define EPED_START_CAL          1ul       // start of cali run
#define EPED_CHANGE_CAL         2ul       // change of same
#define EPED_STOP_CAL           3ul       // stop of same, crate
#define EPED_END_CAL            4ul       // end of run, all crates

// -------------------------------------------------------------------------------------
//   VTHR record 
//
#define VTHR_RECORD	   0x56544852 // 'VTHR'

// VTHR bank structure
typedef struct VThresholdRecord {
  uint32_t        GTID;	  //  GTID 
  uint32_t        ncrates; // number of crates = 19 for SNODAQ
  uint32_t        null2;   // these are longs cuz Josh is a weenie.
  uint32_t        null3;
  uint32_t        null4;
  uint32_t        null5;
  uint32_t        null6;
  uint32_t        null7;
  uint32_t        null8;
  uint32_t        null9;
  unsigned char	 theData[19*16*32];
} SBankVTHR;

// -------------------------------------------------------------------------------------
//   CAST record
//
#define CAST_RECORD		0x43415354	// 'CAST'
#define CLST_RECORD		0x434C5354	// 'CLST' - obsolete

#define	kMaxManipulatorRopes	6

// manipulator rope structure
typedef struct ManipRopeStatus {
	uint32_t		ropeID;				// EManipulatorRopeID
	float		length;
	float		targetLength;
	float		velocity;
	float		tension;
	float		encoderError;
	float		spares[2];
} aManipRopeStatus;

//  CAST bank structure
typedef struct ManipStatus {
	uint32_t			sourceID;
	uint32_t			status;
	uint32_t			numRopes;
	float			position[3];	// x, y, z position
	float			destination[3];
	float			positionError;
	float			sourceOffset[3];// PH 2018-06-20 (was obsoletePosErr)
    float			orientation;	// source orientation (0=unknown, 1=north...)
    float           spare;
	uint32_t			tail;
	aManipRopeStatus	ropeStatus[kMaxManipulatorRopes];
	float			spare2[23];
} SBankCAST;

//  SHaRC CAST bank structure
typedef struct {
	uint32_t			sourceID;
	uint32_t			status;
	uint32_t			numRopes;
	float			position[3];	// x, y, z position
	float			destination[3];
	float			positionError;
	float			obsoletePosErr[3];
    float			orientation;	// source orientation (0=unknown, 1=north...)
    float           spare;
	uint32_t			tail;
	aManipRopeStatus	ropeStatus[4];
} SHaRC_BankCAST;

// status...
enum EManipulatorStatus {
	kStoppedNone = 1,
	kStoppedLowTension,
	kStoppedHighTension,
	kStoppedEndpoint,
	kStoppedStuck,
	kStoppedNetForce,
	kStoppedAxisError,
	kMovingDirect,
	kMovingIdle,
	kMovingAbort
};
	
// ropeID...
enum EManipulatorRopeID {
	NorthRope = 1,
	SouthRope,
	EastRope,
	WestRope,
	CentralRope1,
	CentralRope2,
	CentralRope3,
	GuideTubeRope1,
	GuideTubeRope2,
	GuideTubeRope3,
	GuideTubeRope4,
	GuideTubeRope5,
	GuideTubeRope6,
	GasUmbilical,
	LaserUmbilical,
	RotatingUmbilical
};

// -------------------------------------------------------------------------------------
//   CAAC record
//
#define CAAC_RECORD		0x43414143	// 'CAAC'
#define CLAC_RECORD		0x434C4143	// 'CLAC' - obsolete

// CAAC bank structure
typedef struct AVStatus {
	float		position[3];		// x, y, z
	float		rotation[3];		// roll, pitch, yaw
	float		ropeLength[7];		// seven sense rope lengths
	float		spare[3];
} SBankCAAC;

// -------------------------------------------------------------------------------------
//   SOSA record
//
#define SOSA_RECORD		0x534F5341	// 'SOSA'

// SOSA bank structure
typedef struct AcceleratorStatus {
	uint32_t		sourceID;
	uint32_t		acceleratorOn;
	float		targetVoltage;
	float		targetCurrent;
	float		anodeVoltage;
	float		anodeCurrent;
	float		getterVoltage;
	float		getterCurrent;
	float		direction[3];
	float		spare;
} SBankSOSA;

// -------------------------------------------------------------------------------------
//   SOSG record
//
#define SOSG_RECORD		0x534F5347	// 'SOSG'

// SOSG bank structure
typedef struct GasStatus {
	uint32_t		sourceID;
	uint32_t		dtGeneratorOn;
	float		targetVerticalPosition;
	float		pressure1;
	float		pressure2;
	float		pressure3;
	float		pressure4;
	float		pressure5;
	float		pressure6;
	float		vacuum1;
	float		vacuum2;
	float		carbonDioxideFlowRate;
	float		heliumFlowRate;
	float		oxygenFlowRate;
	float		fastNeutronFlux;
	float		neutronRadiationMonitor;
	float		pmtVoltage;
	float		wireVoltage;
	float		direction[3];
	float		spare[2];
} SBankSOSG;

// -------------------------------------------------------------------------------------
//   SOSL record
//
#define SOSL_RECORD		0x534F534C	// 'SOSL'

// SOSL bank structure
typedef struct LaserStatus {
	uint32_t		sourceID;
	uint32_t		status;
	uint32_t		dyeCellNumber;
	uint32_t		filterWheel1Position;
	uint32_t		filterWheel2Position;
	uint32_t		pmtVoltage;
	float		pressure1;
	float		pressure2;
	float		nitrogenFlowRate;
	float		direction[3];
	float		spare;
} SBankSOSL;

// -------------------------------------------------------------------------------------
//   SOSR record
//
#define SOSR_RECORD		0x534F5352	// 'SOSR'

// SOSR bank structure
typedef struct RotatingStatus {
	uint32_t		sourceID;
	float		direction[3];
} SBankSOSR;


// -------------------------------------------------------------------------------------
//   TASK record
//
#define TASK_RECORD     0x5441534B // 'TASK'

// task record  -- Task running record
typedef struct TASKRecord {
    uint32_t Time;           // ptk used instead of aDate_Time
    uint32_t TasksRunning;   //true if any task is running, false if none running.
    uint32_t GTID;
    uint32_t RunNumber;
    uint32_t Spares[5];
} SBankTASK;

// -------------------------------------------------------------------------------------
//   ZDAB record (event record)
//
#define ZDAB_RECORD    0x5a444142 // 'ZDAB'  (as written to ZDAB file)
#define PMT_RECORD     0x504d5420 // 'PMT '  (as dispatched and sent by SHaRC)

/*
 * pmt record -- variable-length record containing npmt*3 LW of FEC data.
 * note: Generic Header length indicates only length of the pmt header, and
 * DOES NOT include the hits! 
 *
 * Also includes the following info, from ref_packer_zdab_pmt.f
 *  
 *      -> BEWARE:  HERE, MSB is 32 ! ! ! ! <-
 *  Also, word number below is SNOMAN WORD NUMBER ONLY!
 *
 *  o  Event Header Record (one per event trigger):
 *                                 Number
 *     Name        WORD  LSB Pos.  of Bits       Description
 *     RECORD_TYPE   1      26       7       Record type (e.g. PMT, NCD, etc).
 *     MC_FLAG       1      25       1       0=Real event, 1= MC event.
 *     VER_NUM       1      17       8       ZDAB_PMT format number number.
 *     DATA_TYPE     1       1      16       Run Type (see id_run_types.doc).
 *     RUN_NUMBER    2       1      32       Run number.
 *     EV_NUMBER     3       1      32       Event number in this Run.
 *     DAQ_STATUS    4      17      16       DAQ status flags.
 *     NHITS         4       1      16       Number of fired PMT's.
 *     PCK_TYPE      5      29       4       MC packing type:
 *                                           0= PMT info only
 *                                           1= 0 plus source bank info
 *                                           2= 1 plus jitter/cerenkov history
 *     CAL_TYPE      5      25       4       MC Calibration type:
 *                                           0= simple calibration constants
 *                                           1= full calibration constants
 *
 *     ---- extended PmtEventRecord format (PH 02/25/99) ----
 *
 *     SUB_FIELD     5      24       1       0= No sub fields
 *                                           1= Sub field to follow
 *     FIELD_LEN     5       1      23       Offset in 4-uint8_t words to first sub field
 *                                           (May be zero if no sub fields)
 *                                           
 */

// put flags here, shifted to correct position.  See above.
#define PMT_EVR_RECTYPE         ( 0xA << 9 ) // see SNOMAN Docs for these
#define PMT_EVR_NOT_MC          ( 0x0UL << 8 )
#define PMT_EVR_ZDAB_VER        ( 23UL << 0  )
#define PMT_EVR_DATA_TYPE       0xB 
#define PMT_EVR_DAQ_STAT        0xA 
#define PMT_EVR_PCK_TYPE        ( 0x00UL << 28 )
#define PMT_MC_PCK_TYPE         ( 0x01UL << 28 )
#define PMT_JITTER_PCK_TYPE     ( 0x02UL << 28 )
#define PMT_EVR_CAL_TYPE        ( 0x01UL << 24 )

#define PCK_TYPE_MASK           ( 0x0fUL << 28 )
#define CAL_TYPE_MASK           ( 0x0fUL << 24 )

#ifdef SWAP_BYTES

//   FEC data as read out in 96-bit structure 
typedef struct FECReadoutData {
  //   word 1 (starts from LSB): 
  unsigned GTID1                :16; //   lower 16 bits 
  unsigned ChannelID            :5;
  unsigned CrateID              :5;
  unsigned BoardID              :4;
  unsigned CGT_ES16             :1;
  unsigned CGT_ES24             :1;

  //   word 2: 
  unsigned Qlx                  :11;
  unsigned SignQlx              :1;
  unsigned CellID               :4;
  unsigned Qhs                  :11;
  unsigned SignQhs              :1;
  unsigned MissedCount          :1;
  unsigned NC_CC                :1;
  unsigned LGI_Select           :1;
  unsigned Cmos_ES16            :1;

  //   word 3            : 
  unsigned Qhl                  :11;
  unsigned SignQhl              :1;
  unsigned GTID2                :4;        //   bits 17-20 
  unsigned TAC                  :11;
  unsigned SignTAC              :1;
  unsigned GTID3                :4;        //   bits 21-24 

} aFECReadoutData, *aFECReadoutDataPtr;

//   Master Trigger Card data 
typedef struct MTCReadoutData {
  //   word 0 
  uint32_t Bc10_1          :32;
  //   word 1 
  uint32_t Bc10_2          :21;
  uint32_t Bc50_1          :11;
  //   word 2 
  uint32_t Bc50_2          :32;
  //   word 3 
  uint32_t BcGT            :24; //   LSB 
  unsigned Nhit_100_Lo          :1;
  unsigned Nhit_100_Med         :1;
  unsigned Nhit_100_Hi          :1;
  unsigned Nhit_20              :1;
  unsigned Nhit_20_LB           :1;
  unsigned ESum_Lo              :1;
  unsigned ESum_Hi              :1;
  unsigned Owln                 :1; //   MSB 

  //   word 4 
  unsigned Owle_Lo              :1;
  unsigned Owle_Hi              :1;
  unsigned Pulse_GT             :1;
  unsigned Prescale             :1;
  unsigned Pedestal             :1;
  unsigned Pong                 :1;
  unsigned Sync                 :1;
  unsigned Ext_Async            :1;
  unsigned Hydrophone           :1;
  unsigned Ext_3                :1;
  unsigned Ext_4                :1;
  unsigned Ext_5                :1;
  unsigned Ext_6                :1;
  unsigned NCD_Shaper           :1;
  unsigned Ext_8                :1;
  unsigned Special_Raw          :1;
  unsigned NCD_Mux              :1;
  unsigned Soft_GT              :1;
  unsigned Miss_Trig            :1;
  unsigned Peak                 :10;
  unsigned Diff_1               :3;

  //  word 5 
  unsigned Diff_2               :7;
  unsigned Int                  :10;
  unsigned TestGT               :1;
  unsigned Test50               :1;
  unsigned Test10               :1;
  unsigned TestMem1             :1;
  unsigned TestMem2             :1;
  unsigned SynClr16             :1;
  unsigned SynClr16_wo_TC16     :1;
  unsigned SynClr24             :1;
  unsigned SynClr24_wo_TC24     :1;
  unsigned FIFOsNotAllEmpty     :1;
  unsigned FIFOsNotAllFull      :1;
  unsigned FIFOsAllFull         :1;
  unsigned Unused1              :1;
  unsigned Unused2              :1;
  unsigned Unused3              :1;

} aMTCReadoutData, *aMTCReadoutDataPtr;

#else // SWAP_BYTES

//   FEC data as read out in 96-bit structure 
typedef struct FECReadoutData {
  //   word 1 (starts from MSB): 
  unsigned CGT_ES24             :1;
  unsigned CGT_ES16             :1;
  unsigned BoardID              :4;
  unsigned CrateID              :5;
  unsigned ChannelID            :5;
  unsigned GTID1                :16; //   lower 16 bits 
  //   word 2: 
  unsigned Cmos_ES16            :1;
  unsigned LGI_Select           :1;
  unsigned NC_CC                :1;
  unsigned MissedCount          :1;
  unsigned SignQhs              :1;
  unsigned Qhs                  :11;
  unsigned CellID               :4;
  unsigned SignQlx              :1;
  unsigned Qlx                  :11;
  //   word 3            : 
  unsigned GTID3                :4;        //   bits 21-24 
  unsigned SignTAC              :1;
  unsigned TAC                  :11;
  unsigned GTID2                :4;        //   bits 17-20 
  unsigned SignQhl              :1;
  unsigned Qhl                  :11;
} aFECReadoutData, *aFECReadoutDataPtr;

//   Master Trigger Card data 
typedef struct MTCReadoutData {
  //   word 0 
  uint32_t Bc10_1          :32;
  //   word 1 
  uint32_t Bc50_1          :11;
  uint32_t Bc10_2          :21;
  //   word 2 
  uint32_t Bc50_2          :32;
  //   word 3 
  unsigned Owln                 :1; //   MSB 
  unsigned ESum_Hi              :1;
  unsigned ESum_Lo              :1;
  unsigned Nhit_20_LB           :1;
  unsigned Nhit_20              :1;
  unsigned Nhit_100_Hi          :1;
  unsigned Nhit_100_Med         :1;
  unsigned Nhit_100_Lo          :1;
  uint32_t BcGT            :24; //   LSB 
  //   word 4 
  unsigned Diff_1               :3;
  unsigned Peak                 :10;
  unsigned Miss_Trig            :1;
  unsigned Soft_GT              :1;
  unsigned NCD_Mux              :1;
  unsigned Special_Raw          :1;
  unsigned Ext_8                :1;
  unsigned NCD_Shaper           :1;
  unsigned Ext_6                :1;
  unsigned Ext_5                :1;
  unsigned Ext_4                :1;
  unsigned Ext_3                :1;
  unsigned Hydrophone           :1;
  unsigned Ext_Async            :1;
  unsigned Sync                 :1;
  unsigned Pong                 :1;
  unsigned Pedestal             :1;
  unsigned Prescale             :1;
  unsigned Pulse_GT             :1;
  unsigned Owle_Hi              :1;
  unsigned Owle_Lo              :1;
  //  word 5 
  unsigned Unused3              :1;
  unsigned Unused2              :1;
  unsigned Unused1              :1;
  unsigned FIFOsAllFull         :1;
  unsigned FIFOsNotAllFull      :1;
  unsigned FIFOsNotAllEmpty     :1;
  unsigned SynClr24_wo_TC24     :1;
  unsigned SynClr24             :1;
  unsigned SynClr16_wo_TC16     :1;
  unsigned SynClr16             :1;
  unsigned TestMem2             :1;
  unsigned TestMem1             :1;
  unsigned Test10               :1;
  unsigned Test50               :1;
  unsigned TestGT               :1;
  unsigned Int                  :10;
  unsigned Diff_2               :7;
} aMTCReadoutData, *aMTCReadoutDataPtr;

#endif // SWAP_BYTES

// ZDAB bank structure
typedef struct PmtEventRecord {
#ifdef SWAP_BYTES
  uint16_t DataType;
  uint16_t PmtEventRecordInfo;
#else
  uint16_t PmtEventRecordInfo;
  uint16_t DataType;
#endif
  uint32_t RunNumber;
  uint32_t EvNumber;
#ifdef SWAP_BYTES
  uint16_t NPmtHit;
  uint16_t DaqStatus; // Now used to store sub-run number - PH
#else
  uint16_t DaqStatus; // Now used to store sub-run number - PH
  uint16_t NPmtHit;
#endif
  uint32_t CalPckType;  // lower 24 bits are now used for extended PmtEventRecord flags
  aMTCReadoutData TriggerCardData;    //   6 LW of MTC data 
                                      //   FECReadoutData follows directly. 
} SBankZDAB, aPmtEventRecord;

// MCHeader - monte carlo event data if CAL_TYPE = 1 or 2 (one per event)
typedef struct MCHeader {
  uint16_t mcVersion;    // SNOMAN MC version number
  uint16_t nVertices;    // number of source vertices
  uint32_t mcEvNumber;   // MC event number
  uint32_t julianDate;   // Julian Date (01/01/75 = 1)
  uint32_t sec;          // universal time seconds
  uint32_t nsec;         // universal time nanoseconds
  uint16_t Seed1;        // first random number seed
  uint16_t Seed2;        // second random number seed
  uint32_t randNum;      // random number used
} aMCHeader;

// MCData - monte carlo data (one per source vertex + hanging tracks)
typedef struct MCData {
  uint32_t intType;      // interaction type
  uint16_t xPos;         // vertex X position (32768 = 1000.0 cm)
  uint16_t yPos;         // vertex Y position
  uint16_t zPos;         // vertex X position
  uint8_t    nTracks;      // number of tracks for this vertex
  uint8_t    vClass;       // vertex class, (Pre)Source=(1)0
  uint32_t time0;        // first word of double prec time
  uint32_t time1;        // second word of double prec time
  uint16_t pType;        // particle type
  uint16_t pEnergy0;     // first word of 32-bit energy (note: NOT on 32-bit boundary!)
  uint16_t pEnergy1;     // second word of 32-bit energy
  uint16_t xDir;         // track direction x-cosine (32768 = 1.0)
  uint16_t yDir;         // track direction x-cosine
  uint16_t zDir;         // track direction x-cosine
} aMCData;

// MCJitter - jitter/cherenkov history if CAL_TYPE = 2 (one per PMT)
typedef struct MCJitter {
  uint32_t jitterData;
} aMCJitter;
 
/* ................... extended PmtEventRecord format (PH 02/25/99) ................... */

#define SUB_NOT_LAST            ( 0x00800000UL )  // bit set indicates another sub-field follows
#define SUB_LENGTH_MASK         ( 0x007fffffUL )  // mask for offset to next sub-field
#define SUB_TYPE_BITNUM         24                // bit position of sub-field type

/* sub-field ID numbers */
#define SUB_TYPE_CALIBRATED     8UL     // calibrated sub-field type
#define SUB_TYPE_MONTE_CARLO    9UL     // monte carlo sub-field
#define SUB_TYPE_FIT           10UL     // fitted event sub-field
#define SUB_TYPE_HIT_DATA	   11UL		// extra floating-point hit data
#define SUB_TYPE_EVENT_DATA    12UL		// extra floating-point event data
#define SUB_TYPE_PACKED_TSLH   13UL     // packed time since last hit
#define SUB_TYPE_CAL_FLAGS     14UL     // calibrated data flags (one 32 bit word)
#define SUB_TYPE_HCA_QUEENS    15UL     // Queen's HCA calibration
#define SUB_TYPE_NCD		   16UL     // NCD data record
#define SUB_TYPE_CAEN          32UL     // SNO+ CAEN data
#define SUB_TYPE_TUBII         33UL     // SNO+ TUBII trigger word

/* sub-field header */
typedef struct SubFieldHeader {
  uint32_t flags;        // bits  0-22 = size of this sub-field in 4-uint8_t words (including header)
                        // bit  23    = 0-no more sub-fields, 1=another sub-field follows
                        // bits 24-31 = ID number for this sub-field
} aSubFieldHeader;

/* sub-field ID number 8 - calibrated PMT data */
/* -9999 in any of these fields means that no calibration was available. */
/* Order and number of these correspond exactly to hits in original PmtEventRecord. */
typedef struct CalibratedPMT {
    float    tac;
    float    qhs;
    float    qhl;
    float    qlx;
} aCalibratedPMT;

/* sub-field ID number 9 - monte carlo header */
typedef struct MCHeader   MonteCarloHeader;

/* vertex structure for monte-carlo data */
typedef struct MonteCarloVertex {
    float    x,y,z;     // vertex location in cm
    float    u,v,w;     // track direction cosines
    double   time;      // event time
    float    energy;    // energy of particle for outgoing track
    uint32_t  int_code;  // vertex interaction code
    uint16_t  flags;     // special vertex flags (upper 8 bits reserved for display)
    uint16_t  particle;  // outgoing track particle ID
    int32_t    parent;    // index of parent vertex in list (-ve if no parent)
} aMonteCarloVertex;

/* sub-field ID number 10 - fitted event data */
typedef struct FittedEvent {
    float    x,y,z;     // fitted event position in cm
    float    u,v,w;     // fitted event direction cosines
    float    time;      // fitted event time
    float    quality;   // quality of fit
    uint16_t  npmts;     // number of PMT's fit
    uint16_t  spare;     // extra fit-dependent data
    char     name[32];  // fitter identification string (NULL terminated)
} aFittedEvent;

/* sub-field ID number 11 - extra floating point hit data */
#define DATA_NAME_LEN	24

typedef struct ExtraHitData {
	char	 name[DATA_NAME_LEN];	// null-terminated data name (see Note 1)
	// -- followed by NPmtHit float values in the same order as PMT hits
} aExtraHitData;

// Note 1) The extra hit/event data name may have an optional format specifier.
// The format specifier is standard C printf style for a floating point number
// (ie. "%.2f"), and must immediately follow the null terminator of the name.

/* sub-field ID number 12 - extra floating point event data */
typedef struct ExtraEventData {
	char	name[DATA_NAME_LEN];	// null-terminated data name (see Note 1)
	float	value;	// data value
} aExtraEventData;

/* sub-field ID number 13 - packed TSLH */

/* sub-field ID number 14 - calibration flags (one 32-bit word) */
#define CAL_FLAG_QSLOPE		0x0001	// flag for charge slopes applied

/* sub-field ID number 15 - Queen's HCA calibration */

/* sub-field ID number 16 - NCD data record (see NcdDataTypes.h) */

/* sub-field ID nubmer 32 - CAEN trigger sum data */
#define UNPK_CAEN_MAGIC(a)          ( (*(a) >> 28) & 0x0000000f )
#define UNPK_CAEN_WORD_COUNT(a)     ( *(a) & 0x0fffffff )
#define UNPK_CAEN_CHANNEL_MASK(a)   ( *((a)+1) & 0x000000ff )
#define UNPK_CAEN_PATTERN(a)        ( (*((a)+1) >> 8) & 0x0000ffff )
#define UNPK_CAEN_PACK_FLAG(a)      ( (*((a)+1) >> 24) & 0x00000001 )
#define UNPK_CAEN_BOARD_ID(a)       ( *((a)+1) >> 25 )
#define UNPK_CAEN_SYNC16(a)         ( (*((a)+1) >> 15) & 0x00000001 )
#define UNPK_CAEN_SYNC24(a)         ( (*((a)+1) >> 16) & 0x00000001 )
#define UNPK_CAEN_EVENT_COUNT(a)    ( *((a)+2) & 0x00ffffff )
#define UNPK_CAEN_TRIGGER_TIME(a)   ( *((a)+3) )

/* sub-field ID number 33 - TUBii record */
typedef struct TubiiRecord {
    uint32_t     TrigWord;
    uint32_t     GTID;
} aTubiiRecord;

/* .............................. End extended format .............................. */

/*
 *  unpacking "routines" by definition! Looks awful, but is fast...
 *  "a" in the following is a pointer to 3 longwords as read out from
 *  the FEC32.  from SNODAQ distribution
 */
#define UNPK_MISSED_COUNT(a)    ( (*((a)+1) >> 28) & 0x1 )
#define UNPK_NC_CC(a)           ( (*((a)+1) >> 29) & 0x1 )
#define UNPK_LGI_SELECT(a)      ( (*((a)+1) >> 30) & 0x1 )
#define UNPK_CMOS_ES_16(a)      ( (*((a)+1) >> 31) & 0x1 )
#define UNPK_CGT_ES_16(a)       ( (*(a) >> 30) & 0x1 )
#define UNPK_CGT_ES_24(a)       ( (*(a) >> 31) & 0x1 )
#define UNPK_QLX(a)             ( (  *((a)+1)        & 0x00000fff) ^ 0x00000800 )
#define UNPK_QHS(a)             ( ( (*((a)+1) >> 16) & 0x00000fff) ^ 0x00000800 )
#define UNPK_QHL(a)             ( (  *((a)+2)        & 0x00000fff) ^ 0x00000800 )
#define UNPK_TAC(a)             ( ( (*((a)+2) >> 16) & 0x00000fff) ^ 0x00000800 )

#define UNPK_CELL_ID(a)         ( (*((a)+1) >> 12) & 0x0000000F )
#define UNPK_CHANNEL_ID(a)      ( (*(a) >> 16) & 0x0000001F )
#define UNPK_BOARD_ID(a)        ( (*(a) >> 26) & 0x0000000F )
#define UNPK_CRATE_ID(a)        ( (*(a) >> 21) & 0x0000001F )
#define UNPK_FEC_GT24_ID(a)     ( (  * (a)          & 0x0000FFFF ) | \
                                  ( (*((a)+2) << 4) & 0x000F0000 ) | \
                                  ( (*((a)+2) >> 8) & 0x00F00000 ) )
#define UNPK_FEC_GT16_ID(a)     ( *(a) & 0x0000FFFF ) 
#define UNPK_FEC_GT8_ID(a)      ( ( (*((a)+2) >> 24) & 0x000000F0 ) | \
                                  ( (*((a)+2) >> 12) & 0x0000000F )

#define UNPK_FEC_GT_ID(a)       ( (  * (a)          & 0x0000FFFF ) | \
                                  ( (*((a)+2) << 4) & 0x000F0000 ) | \
                                  ( (*((a)+2) >> 8) & 0x00F00000 ) )

// unpacking trigger words by definitions...
// unpacking "routines" by definition! Looks awful, but is fast...
// "a" in the following is a pointer to 6 longwords as read out from the MTC.
#define UNPK_MTC_BC50_1(a)		( (*((a)+1) >> 21) & 0x7FF )
#define UNPK_MTC_BC50_2(a)		( *((a)+2) )
#define UNPK_MTC_BC10_1(a)		( *( a ) )
#define UNPK_MTC_BC10_2(a)		( *((a)+1) & 0x1FFFFF )
#define UNPK_MTC_GT_ID(a)		( *((a)+3) & 0x00FFFFFF )
#define UNPK_MTC_DIFF(a)		(((*((a)+4) >> 29) & 0x007) | ((*((a)+5) << 3) & 0x3F8))
#define UNPK_MTC_INT(a)			( (*((a)+5) >>  7) & 0x3FF )
#define UNPK_MTC_PEAK(a)		( (*((a)+4) >> 19) & 0x3FF )
#define UNPK_NHIT_100_LO(a)		( *((a)+3) & 0x01000000 )
#define UNPK_NHIT_100_MED(a)	( *((a)+3) & 0x02000000 )
#define UNPK_NHIT_100_HI(a)		( *((a)+3) & 0x04000000 )
#define UNPK_NHIT_20(a)			( *((a)+3) & 0x08000000 )
#define UNPK_NHIT_20_LB(a)		( *((a)+3) & 0x10000000 )
#define UNPK_ESUM_LO(a)			( *((a)+3) & 0x20000000 )
#define UNPK_ESUM_HI(a)			( *((a)+3) & 0x40000000 )
#define UNPK_OWLN(a)			( *((a)+3) & 0x80000000 )
#define UNPK_OWLE_LO(a)			( *((a)+4) & 0x00000001 )
#define UNPK_OWLE_HI(a)			( *((a)+4) & 0x00000002 )
#define UNPK_PULSE_GT(a)		( *((a)+4) & 0x00000004 )
#define UNPK_PRESCALE(a)		( *((a)+4) & 0x00000008 )
#define UNPK_PONG(a)			( *((a)+4) & 0x00000010 )
#define UNPK_SYNC(a)			( *((a)+4) & 0x00000020 )
#define UNPK_EXT_ASYNC(a)		( *((a)+4) & 0x00000040 )
#define UNPK_EXT_2(a)			( *((a)+4) & 0x00000080 )
#define UNPK_EXT_3(a)			( *((a)+4) & 0x00000100 )
#define UNPK_EXT_4(a)			( *((a)+4) & 0x00000200 )
#define UNPK_EXT_5(a)			( *((a)+4) & 0x00000400 )
#define UNPK_EXT_6(a)			( *((a)+4) & 0x00000800 )
#define UNPK_EXT_7(a)			( *((a)+4) & 0x00001000 )
#define UNPK_EXT_8(a)			( *((a)+4) & 0x00002000 )
#define UNPK_SPECIAL_RAW(a)		( *((a)+4) & 0x00004000 )
#define UNPK_NCD(a)				( *((a)+4) & 0x00008000 )
#define UNPK_SOFT_GT(a)			( *((a)+4) & 0x00010000 )
#define UNPK_MISS_TRIG(a)		( *((a)+4) & 0x00020000 )

// unpack Trigger Error Mask...
#define UNPK_TESTGT(a)				( *((a)+5) & 0x00020000 )
#define UNPK_TEST50(a)				( *((a)+5) & 0x00040000 )
#define UNPK_TEST10(a)				( *((a)+5) & 0x00080000 )
#define	UNPK_TESTMEM1(a)			( *((a)+5) & 0x00100000 )
#define	UNPK_TESTMEM2(a)			( *((a)+5) & 0x00200000 )
#define	UNPK_SYNCLR16(a)			( *((a)+5) & 0x00400000 )
#define	UNPK_SYNCLR16_WO_TC16(a)	( *((a)+5) & 0x00800000 )
#define	UNPK_SYNCLR24(a)			( *((a)+5) & 0x01000000 )
#define	UNPK_SYNCLR24_WO_TC24(a)	( *((a)+5) & 0x02000000 )
#define	UNPK_SOME_FIFOS_EMPTY(a)	( *((a)+5) & 0x04000000 )
#define	UNPK_SOME_FIFOS_FULL(a)		( *((a)+5) & 0x08000000 )
#define	UNPK_ALL_FIFOS_FULL(a)		( *((a)+5) & 0x10000000 )

// unpack the Trigger Word (lowest 27 bits are significant)
#define UNPK_MTC_TRIGGER(a)     ( ( ( *((a)+3) >> 5 ) & 0x7F80000 ) | ( *((a)+4) & 0x7FFFF ) )
// unpack the Trigger Error bits (lowest 14 bits signficant)
#define UNPK_MTC_ERROR(a) 		( ( *((a)+5) >> 17 ) & 0x3FFF )


// -------------------------------------------------------------------------------------

// other records, not defined in this file
#define CRON_RECORD	   0x43524F4E // 'CRON'
#define VRLY_RECORD	   0x56524C59 // 'VRLY'
#define MSEI_RECORD    0x4D534549 // 'MSEI'

#ifdef __MWERKS__
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// extra code for SHaRC version only

typedef struct MTCReadoutWords {
    uint32_t Word[6];
} aMTCReadoutWords, *aMTCReadoutWordsPtr;

typedef struct FECReadoutWords {
    uint32_t Word[3];
} aFECReadoutWords, *aFECReadoutWordsPtr;

// (be very careful here!  for backward compatibility this is also
//  defined as EpedRecord as used by everyone else.  Not to be confused with EPEDRecord.)
typedef struct EPEDRecord  SHaRC_BankEPED;

// SHaRC uses short forms of trigger bit definitions
#define NHIT_100_LO        TRIG_NHIT_100_LO
#define NHIT_100_MED       TRIG_NHIT_100_MED
#define NHIT_100_HI        TRIG_NHIT_100_HI
#define NHIT_20            TRIG_NHIT_20
#define NHIT_20_LB         TRIG_NHIT_20_LB
#define ESUM_LO            TRIG_ESUM_LO
#define ESUM_HI            TRIG_ESUM_HI
#define OWLN               TRIG_OWLN
#define OWLE_LO            TRIG_OWLE_LO
#define OWLE_HI            TRIG_OWLE_HI
#define PULSE_GT           TRIG_PULSE_GT
#define PRESCALE           TRIG_PRESCALE
#define PEDESTAL           TRIG_PEDESTAL
#define PONG               TRIG_PONG
#define SYNC               TRIG_SYNC
#define EXT_ASYNC          TRIG_EXT_ASYNC
#define HYDROPHONE         TRIG_HYDROPHONE
#define EXT3               TRIG_EXT3
#define EXT4               TRIG_EXT4
#define EXT5               TRIG_EXT5
#define EXT6               TRIG_EXT6
#define EXT7               TRIG_EXT7
#define EXT8               TRIG_EXT8
#define SPECIAL_RAW        TRIG_SPECIAL_RAW
#define NCD                TRIG_NCD_MUX
#define SOFT_GT            TRIG_SOFT_GT

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#endif // __MWERKS__


#endif //  __RECORD_INFO_H__