# The goal of this macro is to produce a root-file from data that can be used
# for the PMT calibration rattest. You will need to get the zdab file for
# run 8880 (December 2014 data) or update the rattest with a new run when
# needed.
#
# Remember:
# 1) Make sure to keep the file-size small. We do not need a large number of
#    events for this test to work. To create the input file, the zdabconverter
#    was changed to skip over any hit that was not on PMT 3635, cell 1 and also not
#    to record any events that did not contain a hit on PMT 3635, cell 1.
#    Here is what I did in zdab_convert.cc where the pmt info is read in:
#    // pmt hit data
#    uint32_t* hits = reinterpret_cast<uint32_t*>(pev + 1);
#    bool skipev = 1;
#    RAT::DS::UncalPMTs& uncalPMTs = ev.GetUncalPMTs();
#    double totalQ = 0.0;
#    RAT::DS::CalPMTs calPMTs;
#    for (unsigned i=0; i<nhit; i++, hits += 3) {
#        const unsigned crate_id = UNPK_CRATE_ID(hits);
#        const unsigned board_id = UNPK_BOARD_ID(hits);
#        const unsigned channel_id = UNPK_CHANNEL_ID(hits);
#        const unsigned cell_id = UNPK_CELL_ID(hits);
#        const int lcn = 16 * 32 * crate_id + 32 * board_id + channel_id;
#        if (lcn != 3635) continue;
#        if (cell_id != 1) continue;
#        skipev = 0;
#
#   Then, add the end of that function:
#   if (!skipev) ds->AddEV( ev );
#
# 2) Update the rattest when you have re-generated the input file.
#
# F. Descamps Sept 2015 <fbdescamps@lbl.gov>
# N Barros (Nov 2016) : Changed the logic of InZDAB

/rat/inzdab/load SNOP_0000008880_000.zdab

/run/initialize

# BEGIN EVENT LOOP
# Select only events with EXTA trigger
/rat/proc/if trigTypeSelector
/rat/procset trigType 15

# BEGIN EVENT LOOP
/rat/proc count
/rat/procset update 1000

/rat/proclast outroot
/rat/procset file "pmtcal-input.root"
/rat/proc/endif

# END EVENTLOOP
# Fiber 020 December 2014 data
/rat/inzdab/read