/*******************************************************************************
* Instrument: ESS_mcpl2hist
*
* %I
* Written by: Peter Willendrup <pkwi@fysik.dtu.dk>
* Date: 2016-12-02
* Origin: ESS
* %INSTRUMENT_SITE: ESS
*
* Utility instrument that generates a set of histograms from an MCPL input file
*
* %D
* Utility instrument that generates a set of histograms from an MCPL input file.
* 
* Generates energy, wavelength, position, veloicty and time histograms via Monitor_nD.
*
* Assumes access to binary MCPL datasets in . named [sector][beamline].mcpl.gz, i.e. W8.mcpl.gz.
*
* %P
* sector:        [str]  Defines the 'sector' of your instrument position. Valid values are "N","S","E" and "W"
* beamline:      [1]    Defines the 'beamline number' of your instrument position. Valid values are 1..10 or 1..11 depending on sector
* filter:        [1]    Flag to define if the filtered or nonfiltered version of the MCPL file should be used.
* thres:         [1]    Weight threshold above which neutrons are absorbed
*
* %L
* <reference/HTML link>
* Benchmarking website available at <a href="http://ess_butterfly.mcstas.org">http://ess_butterfly.mcstas.org</a>
* %E
*******************************************************************************/
DEFINE INSTRUMENT ESS_mcpl2hist(string sector="N",int beamline=1,int filter=0,thres=0.003)

DECLARE
%{
  double WL;
  char MCPLfile[128];
%}

INITIALIZE
%{
  if (filter==0)
    sprintf(MCPLfile,"%s%i.mcpl.gz",sector,beamline);
  else
    sprintf(MCPLfile,"%s%i_filtered.mcpl.gz",sector,beamline);
  printf("MCPLfile is %s\n",MCPLfile);
%}

TRACE

COMPONENT Origin = Progress_bar()
AT (0, 0, 0) ABSOLUTE
  

COMPONENT vin = MCPL_input(filename=MCPLfile,verbose=1)
  AT(0,0,0) RELATIVE PREVIOUS
EXTEND %{
  SCATTER;
  p*=1.56e16;
  p/=1e5;
%}

  COMPONENT Histograms = Monitor_nD(xwidth=1,yheight=1,options="previous, multiple, omega bins=100, lambda bins=100, x bins=100, y bins=100, z bins=100, vx bins=100, vy bins=100, vz bins=100, t bins=100 auto")
  AT (0,0,0.01) RELATIVE PREVIOUS



END
