// =====================================================================
//  $Id: OFFspfind.cc,v 1.6 2003/10/06 16:28:18 goiwai Exp $
//  $Name: CLDAQ-1-14-03 $
//  $Log: OFFspfind.cc,v $
//  Revision 1.6  2003/10/06 16:28:18  goiwai
//  *** empty log message ***
//
//  Revision 1.5  2003/07/30 16:16:10  goiwai
//  ファイルにコミットログをつけることにしました.
//
// =====================================================================
#include "TDataRecord.hh"
#include "TDataSection.hh"
#include "TDataSegment.hh"
#include "TDataElement.hh"
#include "TEnvironmentVariableTable.hh"
#include "TInputObjectFile.hh"
#include "Trootinit.h"

#include "St01OfflineDarkAverageMap.hh"
#include "St01OfflineDarkRmsMap.hh"
#include "St01SinglePixelInterface.hh"
#include "St01SinglePixelList.hh"
#include "St01DataRecordConversion.hh"
#include "St01RunInformationExtractor.hh"
#include "St01BadChannelMap.hh"

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

  TApplication* theApplication = rootinit();
  TCanvas CVspfind( "CVspfind", "Findout SINGLE PIXEL EVENTs", 720, 810 );
  CVspfind.ToggleEventStatus();
  CVspfind.Divide( 2, 3 );
  //シングルピクセルイベントのシグナルに相当するADCカウント
  TH1D H1Dsignal( "H1Dsignal", "Single Pixel Event subtracted D.C.", 100, 0.0, 500.0 );
  //そのピクセルに於けるダークカレントに相当するADCカウントの平均値
  //同ラインのH.O.C.の平均は引かれている
  TH1D H1Ddark( "H1Ddark", "Dark Current subtracted H.O.C.", 100, -50.0, 50.0 );
  //そのピクセルに於けるRMSに相当するADCカウントの平均値
  TH1D H1Drms( "H1Drms", "RMS of D.C.", 50, 0.0, 50.0 );
  //解析情報テキスト
  TPaveText PTdinfo( 0.0, 0.0, 1.0, 1.0 );
  TPaveText PTsinfo( 0.0, 0.0, 1.0, 1.0 );
  TPaveText PTainfo( 0.0, 0.0, 1.0, 1.0 );

  CVspfind.cd( 1 );
  H1Ddark.Draw();
  CVspfind.cd( 2 );
  H1Drms.Draw();
  CVspfind.cd( 3 );
  H1Dsignal.Draw();
  CVspfind.cd( 4 );
  PTdinfo.Draw();
  CVspfind.cd( 5 );
  PTsinfo.Draw();
  CVspfind.cd( 6 );
  PTainfo.Draw();



  // file名の決定
  TEnvironmentVariableTable envtable( "st01setup" );
  Tint drkid = strtol( argv[ 1 ], 0, 0 );
  Tint srcid = strtol( argv[ 2 ], 0, 0 );
  Tstring drkf = envtable.GetEnvironmentValue( "ST01_RECORD_DIR" ) + "/run";
  Tstring srcf = envtable.GetEnvironmentValue( "ST01_RECORD_DIR" ) + "/run";
  drkf += itostr( drkid ) + ".drec";
  srcf += itostr( srcid ) + ".drec";


  // シングルピクセルイベントのファイル名を決める
  Tstring outpath = envtable.GetEnvironmentValue( "ST01_SPDATASET_DIR" );
  Tstring darkrunid = itostr( drkid );
  Tstring sourcerunid = itostr( srcid );

  Tstring th=envtable.GetEnvironmentValue( "ST01_SPIXEL_THRESHOLD_SIGMA" );
  Tstring ar=envtable.GetEnvironmentValue( "ST01_SPIXEL_NO_AROUND_SIGMA" );
  outpath += "/" + th + "sigma" + ar + "sigma";
  if ( !isexist( outpath ) ) {
    Tcerr << "No such file or directory: " << outpath << Tendl;
    return -1;
  }
  outpath += "/drk" + darkrunid + "src" + sourcerunid;


  Tstring outdat = outpath + ".dat";
  Tstring outps = outpath + ".ps";
  Tstring outgif = outpath + ".gif";
  Tstring outC = outpath + ".C";
  Tstring outroot = outpath + ".root";
  if ( envtable.GetIntegerValue( "ST01_OVERWRITE_ENABLE" ) == 0 ) {
    if ( isexist( outdat ) ) {
      Tcerr << "already exist " << outdat << Tendl;
      return -1;
    }
  }

  Tcout << "DARK RUN:   " << drkf << Tendl;
  Tcout << "SOURCE RUN: " << srcf << Tendl;
  Tcout << "OUTPUT:     " << outdat << Tendl;


  St01SinglePixelInterface spif( drkf, srcf );

  
  //const TDataMultiplicity& bad = spif.GetBadChannelMap()->GetMap();
  const TDataMultiplicity& rms = spif.GetBadChannelMap()->GetOfflineDarkRmsMap().GetMap();
  const TDataMultiplicity& ave = spif.GetBadChannelMap()->GetOfflineDarkRmsMap().GetOfflineDarkAverageMap().GetMap();

  for ( Tint row = 0; row < ave.num_row(); row ++ ) {
    for ( Tint col = 0; col < ave.num_col(); col ++ ) {
      //if ( badmap.IsGoodChannel( row, col ) ) {
      H1Ddark.Fill( ave[ row ][ col ] );
      H1Drms.Fill( rms[ row ][ col ] );
      //}
    }
  }
  TGaxis::SetMaxDigits( 2 );
  H1Ddark.GetXaxis()->SetTitle("Pulseheight (ADU)");
  H1Ddark.GetXaxis()->CenterTitle();
  H1Ddark.GetXaxis()->SetNoExponent();
  H1Ddark.GetYaxis()->SetTitle("Counts");
  H1Ddark.GetYaxis()->CenterTitle();

  H1Drms.GetXaxis()->SetTitle("Pulseheight (ADU)");
  H1Drms.GetXaxis()->CenterTitle();
  H1Drms.GetXaxis()->SetNoExponent();
  H1Drms.GetYaxis()->SetTitle("Counts");
  H1Drms.GetYaxis()->CenterTitle();

  CVspfind.GetPad(1)->Modified();
  CVspfind.GetPad(1)->Update();
  CVspfind.GetPad(2)->Modified();
  CVspfind.GetPad(2)->Update();



  const St01SinglePixelList& splist = spif.GetSinglePixelList();
  Tofstream ofs;
  ofs.open( outdat.c_str() );
  for ( Tsize_t i = 0; i < splist.size(); i ++ ) {
    H1Dsignal.Fill( splist[ i ].GetSignal() );
    ofs << splist[ i ] << Tendl;
  }
  ofs.close();

  H1Dsignal.GetXaxis()->SetTitle("Pulseheight (ADU)");
  H1Dsignal.GetXaxis()->CenterTitle();
  H1Dsignal.GetXaxis()->SetNoExponent();
  H1Dsignal.GetYaxis()->SetTitle("Counts");
  H1Dsignal.GetYaxis()->CenterTitle();

  CVspfind.GetPad(3)->Modified();
  CVspfind.GetPad(3)->Update();


  
  const char* tmppath = "ST01TMP";
  Tofstream tofs;

  PTdinfo.SetFillColor( 10 );
  PTdinfo.SetFillStyle( 0 );
  PTdinfo.SetBorderSize( 0 );
  tofs.open( tmppath );
  St01RunInformationExtractor dext( drkf );
  tofs << dext << Tendl;
  PTdinfo.ReadFile( tmppath );
  tofs.close();
  remove( tmppath );


  PTsinfo.SetFillColor( 10 );
  PTsinfo.SetFillStyle( 0 );
  PTsinfo.SetBorderSize( 0 );
  tofs.open( tmppath );
  St01RunInformationExtractor sext( srcf );
  tofs << sext << Tendl;
  PTsinfo.ReadFile( tmppath );
  tofs.close();
  remove( tmppath );


  PTainfo.SetFillColor( 10 );
  PTainfo.SetFillStyle( 0 );
  PTainfo.SetBorderSize( 0 );
  PTainfo.SetTextSize( 0.06 );
  tofs.open( tmppath );
  TstringList items;
  items.push_back( "ST01_VSHIFT_CYCLE" );
  items.push_back( "ST01_HPIXEL" );
  items.push_back( "ST01_VPIXEL" );
  items.push_back( "ST01_HOFFSET" );
  items.push_back( "ST01_VOFFSET" );
  items.push_back( "ST01_HOC_PIXEL" );
  items.push_back( "ST01_BAD_THRESHOLD_RMS" );
  items.push_back( "ST01_BAD_OVERFLOW" );
  items.push_back( "ST01_SPIXEL_THRESHOLD_SIGMA" );
  items.push_back( "ST01_SPIXEL_NO_AROUND_SIGMA" );
  for ( Tsize_t i = 0; i < items.size(); i ++ ) {
    tofs << items[i] << ": " << envtable.GetEnvironmentValue( items[i] ) << Tendl;
  }
   PTainfo.ReadFile( tmppath );
   tofs.close();
   remove( tmppath );




  CVspfind.GetPad(4)->Modified();
  CVspfind.GetPad(4)->Update();
  CVspfind.GetPad(5)->Modified();
  CVspfind.GetPad(5)->Update();
  CVspfind.GetPad(6)->Modified();
  CVspfind.GetPad(6)->Update();

  CVspfind.cd();

  CVspfind.Print( outps.c_str() );
  CVspfind.Print( outgif.c_str() );
  CVspfind.Print( outC.c_str() );
  CVspfind.Print( outroot.c_str() );

  theApplication -> Run();
  //theApplication -> Terminate();

  return 0;
}
