////////////////////////////////////////////////////////////////////////
//
// Manage the input of a ZDAB file coming from data acquisition. Takes
// a ZDAB file and provides the information to the analysis part of
// Rat. This is useful for doing fast processing of data online. Users
// are intended to include the Level 2 filter, supernova trigger and
// monitoring system.
//
// Author: Matthew Strait <strait@hep.uchicago.edu> - contact person
//
// REVISION HISTORY:
//   2015-05-25 : D. Auty - Added code to allow part runs
//   2016-11-22 : N. Barros - Changed the code to allow for multiple files and not call
//                            BeginOfRun for each individual file
//   2017-04-25 : N. Barros - Fixed a problem when loading files from different runs
//
////////////////////////////////////////////////////////////////////////

#ifndef __RAT_InZDABProducer__
#define __RAT_InZDABProducer__

#include <string>
#include <RAT/Producer.hh>
#include <globals.hh>

class G4UIcmdWithAString;
class G4UIcommand;

namespace RAT {


class InZDABProducer : public Producer {
public:
  // Default Constructor
  //
  // Read in parameters from macro, but can't run until SetMainBlock()
  // is called.
  InZDABProducer();

  virtual ~InZDABProducer();

  // Normal constructor
  //
  // Read in parameters from macro and set ProcBlock so we know what
  // to do with the data we read.
  //
  // block: The block of processors to run on each event.
  InZDABProducer(ProcBlock *block);

  // Reads and processes all of the events from the input file.
  //
  // Runs RAT's main loop by reading ZDAB records and, if
  // they represent triggers, calling ProcBlock::DSEvent() to
  // process them. Or, if they represent run headers, it calls
  // ProcBlock::BeginOfRun() and/or ProcBlock::EndofRun().  Other
  // records may update the DS without triggering a loop through
  // the processors, such as TRIG records, whereas others may be
  // ignored.
  //
  // If the file can't be read, zdabfile, used by ReadEvents(), throws
  // zdab_file_read_error.
  //
  // filename: The input ZDAB file name.
  virtual void ReadEvents(const G4String & filename);

  // Fails. Don't call this.
  //
  // Override G4UImessenger (from Producer) method to Log::Die()
  // instead of returning an empty string.
  virtual G4String GetCurrentValue(G4UIcommand * );

  // Sneakily runs RAT's main loop.  Not called directly.
  //
  // Gets a Geant4 /rat/inzdab/read or /rat/inzdab/read_default command
  // and runs ReadEvents() on the file. On any other command, aborts. I
  // think this is probably an abuse of G4UImessenger::SetNewValue().
  //
  // command: The macro command, like /rat/inzdab/read
  // newValue: The filename, since command has to be /read
  virtual void SetNewValue(G4UIcommand * command,G4String newValue);

  // Called whenever the run changes
  //
  // run: run to begin/initialise
  virtual void BeginOfRun( DS::Run& run );

  // Called whenever the run changes
  //
  // run: run to begin/initialise
  virtual void EndOfRun( DS::Run& run );


protected:
  // Initialises
  //
  // Learns what the valid commands are, sets help text.
  void Init();

  // Information for /rat/inzdab/load
  G4UIcmdWithAString *fLoadCmd;

  // Information for /rat/inzdab/load_default
  G4UIcommand *loadDefaultCmd;

  // Information for /rat/inzdab/read_default
  G4UIcommand *readDefaultCmd;

  // Information for /rat/inzdab/read
  G4UIcommand *fReadCmd;


  bool fRunStarted;
  DS::Run *fRun;
  long long fCount;
  long long fDefaultNumEvents;

  std::vector<std::string> fInputFiles;
};

} // namespace RAT

#endif