Skip to content
Snippets Groups Projects
LoadEventPreNexus.h 6.4 KiB
Newer Older
#ifndef LOADEVENTPRENEXUS_H_
#define LOADEVENTPRENEXUS_H_

Nick Draper's avatar
Nick Draper committed
#include <fstream>
#include <string>
#include <vector>
#include "MantidAPI/IFileLoader.h"
#include "MantidAPI/DeprecatedAlgorithm.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidDataObjects/EventWorkspace.h"
Nick Draper's avatar
Nick Draper committed

namespace Mantid {
namespace DataHandling {
/** @class Mantid::DataHandling::LoadEventPreNexus
Nick Draper's avatar
Nick Draper committed

    A data loading routine for SNS pre-nexus event files

    Copyright &copy; 2010-11 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
   National Laboratory & European Spallation Source
Nick Draper's avatar
Nick Draper committed

    This file is part of Mantid.

    Mantid is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Mantid is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    File change history is stored at: <https://github.com/mantidproject/mantid>
Nick Draper's avatar
Nick Draper committed
*/
Nick Draper's avatar
Nick Draper committed

/// This define is used to quickly turn parallel code on or off.
#undef LOADEVENTPRENEXUS_ALLOW_PARALLEL

Nick Draper's avatar
Nick Draper committed
/// Make the code clearer by having this an explicit type
Doucet, Mathieu's avatar
Doucet, Mathieu committed
typedef int PixelType;
Nick Draper's avatar
Nick Draper committed

/// Type for the DAS time of flight (data file)
Doucet, Mathieu's avatar
Doucet, Mathieu committed
typedef int DasTofType;
Nick Draper's avatar
Nick Draper committed
/// Structure that matches the form in the binary event list.
#pragma pack(push, 4) // Make sure the structure is 8 bytes.
struct DasEvent {
Nick Draper's avatar
Nick Draper committed
  /// Time of flight.
Nick Draper's avatar
Nick Draper committed
  /// Pixel identifier as published by the DAS/DAE/DAQ.
  PixelType pid;
};
/// Structure used as an intermediate for parallel processing of events
#pragma pack(push, 4) // Make sure the structure is 8 bytes.
struct IntermediateEvent {
  /// Time of flight.
  DasTofType tof;
  /// Pixel identifier as published by the DAS/DAE/DAQ.
  PixelType pid;
  /// Frame index (pulse # of this event)
  size_t frame_index;
  /// Period of the event (not really used at this time)
  uint32_t period;
};
#pragma pack(pop)

/// Structure that matches the form in the new pulseid files.
#pragma pack(push, 4) // Make sure the structure is 16 bytes.
struct Pulse {
  /// The number of nanoseconds since the seconds field. This is not necessarily
  /// less than one second.
  uint32_t nanoseconds;

  /// The number of seconds since January 1, 1990.
  uint32_t seconds;

  /// The index of the first event for this pulse.
  uint64_t event_index;

  /// The proton charge for the pulse.
  double pCurrent;
};
class DLLExport LoadEventPreNexus
    : public API::IFileLoader<Kernel::FileDescriptor>,
      public API::DeprecatedAlgorithm  {
Nick Draper's avatar
Nick Draper committed
  /// Constructor
  virtual ~LoadEventPreNexus();
  virtual const std::string name() const { return "LoadEventPreNexus"; }
  /// Summary of algorithms purpose
  virtual const std::string summary() const {
    return "Loads SNS raw neutron event data format and stores it in a "
           "workspace (EventWorkspace class).";
  }
  virtual int version() const { return (1); }
  virtual const std::string category() const {
    return "DataHandling\\PreNexus";
  }
  /// Algorithm's aliases
  virtual const std::string alias() const { return "LoadEventPreNeXus"; }

  /// Returns a confidence value that this algorithm can load a file
  virtual int confidence(Kernel::FileDescriptor &descriptor) const;
  /// Execution code
  Mantid::API::Progress *prog;
Peterson, Peter's avatar
Peterson, Peter committed
  std::vector<int64_t> spectra_list; ///<the list of Spectra
  std::vector<Kernel::DateAndTime> pulsetimes;
  /// The index of the first event in each pulse.
  std::vector<uint64_t> event_indices;
  /// The proton charge on a pulse by pulse basis.
  std::vector<double> proton_charge;
  /// The total proton charge for the run.
  double proton_charge_tot;
  /// The value of the vector is the workspace index. The index into it is the
  /// pixel ID from DAS
  std::vector<std::size_t> pixel_to_wkspindex;
  /// Map between the DAS pixel IDs and our pixel IDs, used while loading.
  std::vector<PixelType> pixelmap;
Nick Draper's avatar
Nick Draper committed

  /// The maximum detector ID possible
  Mantid::detid_t detid_max;

  /// Handles loading from the event file
  Mantid::Kernel::BinaryFile<DasEvent> *eventfile;
  std::size_t num_events; ///< The number of events in the file
Nick Draper's avatar
Nick Draper committed
  std::size_t num_pulses; ///<the number of pulses
  uint32_t numpixel;      ///<the number of pixels
Nick Draper's avatar
Nick Draper committed

  std::size_t num_good_events;  ///< The number of good events loaded
  std::size_t num_error_events; ///< The number of error events encountered
  /// the number of events that were ignored (not loaded) because, e.g. of only
  /// loading some spectra.
  std::size_t num_ignored_events;
  std::size_t first_event; ///< The first event to load (count from zero)
  std::size_t max_events;  ///< Number of events to load
Nick Draper's avatar
Nick Draper committed

  /// Set to true if a valid Mapping file was provided.
  /// For loading only some spectra
  /// Handle to the loaded spectra map
Peterson, Peter's avatar
Peterson, Peter committed
  std::map<int64_t, bool> spectraLoadMap;
  /// Longest TOF limit
  double longest_tof;
  /// Shortest TOF limit
  double shortest_tof;
  /// Flag to allow for parallel loading
  void loadPixelMap(const std::string &filename);

  void openEventFile(const std::string &filename);

  void readPulseidFile(const std::string &filename, const bool throwError);
  void runLoadInstrument(const std::string &eventfilename,
                         API::MatrixWorkspace_sptr localWorkspace);
  inline void fixPixelId(PixelType &pixel, uint32_t &period) const;
  void procEvents(DataObjects::EventWorkspace_sptr &workspace);
  void procEventsLinear(DataObjects::EventWorkspace_sptr &workspace,
                        std::vector<DataObjects::TofEvent> **arrayOfVectors,
                        DasEvent *event_buffer,
                        size_t current_event_buffer_size, size_t fileOffset);
  void setProtonCharge(DataObjects::EventWorkspace_sptr &workspace);
Nick Draper's avatar
Nick Draper committed
}