// =====================================================================
//  $Id: sw00g4view.cc,v 1.4 2003/10/12 13:08:47 goiwai Exp $
//  $Name: CLDAQ-1-11-00 $
//  $Log: sw00g4view.cc,v $
//  Revision 1.4  2003/10/12 13:08:47  goiwai
//  オンラインのほうは TInputObjectFileOnline を使うようにしました.
//
//  Revision 1.3  2003/10/06 16:28:16  goiwai
//  *** empty log message ***
//
//  Revision 1.2  2003/07/30 16:15:16  goiwai
//  ファイルにコミットログをつけることにしました.
//
// =====================================================================
#include "TDataRecord.hh"
#include "TDataSection.hh"
#include "TDataSegment.hh"
#include "TDataElement.hh"
#include "TInputObjectFileOnline.hh"
#include "Trootinit.h"

int main( int argc, char** argv, char** envv )
{
  if ( argc != 2 ) {
    Tcerr << "usage: " << argv[ 0 ] << Tspace << "<filename>" << Tendl;
    return -1;
  }

  TApplication* theApplication = rootinit();


  Tstring filename = argv[ 1 ];
  TInputObjectFileOnline ifile( filename );


  TCanvas* canvas = new TCanvas( "simmodule", "Geant4 View", 640, 480 );
  TH1D* g4hist = new TH1D( "g4hist", "Beam energy spectrum", 6, 0.0, 6.0 );
  g4hist -> Draw();

  Tstring idset[ 2 ] = { "Event Data", "Geant4" };
  TDataRecord record;

  while ( ifile.Read( record ) ) {

    Tstring recid = record.GetID();


    if ( recid == "RUN BEGIN" ) {
      continue;
    } else if ( recid == "RUN END" ) {
      break;
    } else if ( recid == "EVENT" ) {

      TDataSegment segment;
      record.FindDataSegment( idset, segment );

      TDataElement ccdid = segment[ 0 ];
      TDataElement edep = segment[ 1 ];

      Tdouble id = *( ccdid.GetDoubleData() );
      Tdouble e = *( edep.GetDoubleData() );

      g4hist -> Fill( id, e );
      canvas -> Modified();
      canvas -> Update();

    } else {
      break;
    }

    gSystem -> ProcessEvents();

  }

  theApplication -> Run();

  return 0;
}
