//-----------------------------------------------------------------------------
// $Id: pandpyth_EventDisp.C,v 1.10 2002/01/15 19:35:11 masako Exp $
//-----------------------------------------------------------------------------
//
//  Example file to run Event Display interactively with Root.
//  
//  In this file, 1) event generation with pandora_pythia
//                2) through Fast MC
//                3) make event display
//   
//        
//                               Apr. 26 2001  Toshi
//
//   :: How to run ::
// 
//   1) type root
//   2)
//      root [0]  .x pandpyth_EventDisp.C
//
// For example, if you like to run 20 events, type like
//     root [0]  .x pandpyth_EventDisp.C(20)
//    

//__________________________________________________________________________

Int_t pandpyth_EventDisp(Int_t nEvent=10){

  gROOT->Reset();

  TString parfile_dir;
  parfile_dir += gSystem->Getenv("LCDROOT")   ;parfile_dir += "/";
  parfile_dir += "ParFiles/";

  // In order to refer to shareable libraries in this simple form,
  // include their paths in a .rootrc file.
  gSystem->Load("libLCDEvent");
  gSystem->Load("libPandora");
  gSystem->Load("libPandoraSMProc");
  if (strcmp(gSystem->GetName(),"WinNT")) {
    gSystem->Load("libPythia6");
    gSystem->Load("libTauola");
  }
  gSystem->Load("libEG");
  gSystem->Load("libEGPythia6");
  gSystem->Load("libPandPyth");
  gSystem->Load("libLCDRootAppsUtil");
  gSystem->Load("libLCDFastMC");
  gSystem->Load("libLCDPhUtil");
  gSystem->Load("libLCDGenUtil");

  //------------< Setup Generator (Pandora-Pythia) >------------------------
  printf("Start Generator Setup!\n");

#include "pandpythconsts.h"

  Char_t* outfile = 0;

  Int_t iseed_pan = 7;  // default pandora initial seed

  /*    define the pandora event selection in this space     */
  
  double ECM  = 500.;
  double PolE = -0.8; // Polarization Electron
  double PolP =  0.0; // Polarization Positron

  ebeam b1(ECM/2.0, PolE, electron, electron);
  b1.setup(NLC500);
  b1.ISRoff();            // Turn off initial-state radiation
  b1.beamstrahlungoff();  // Turn off beamstrahlung

  ebeam b2(ECM/2.0, PolP, positron, positron);
  b2.setup(NLC500);
  b2.ISRoff();
  b2.beamstrahlungoff();
  
  double Hmass = 120.; // Higgs Mass
  //double Hmass = 140.; // Higgs Mass
  eetoZHiggs pr(Hmass);

  //For example:: Z0 decays to anything. Z0 decays to nu nu-bar only.
  pr.onlyDecay(allStates,bOnly); 
  //pr.onlyDecay(invisibleOnly,allStates); 

  pandora P(b1,b2,pr);

  pandorarun  PR(&P, nEvent, iseed_pan);  

  //----- For PYTHIA ------
  //Or you can also change PYTHIA seed here.
  int iseed_pyth = 22846; PR.newseed(iseed_pyth); // Change Seed

  //PR.partonshoweroff();   // Turn off parton shower
  //PR.fragmentationoff();  // Turn off fragmentation
  //PR.FSRoff();            // Turn off final-state QCD QED radiation

  //       end of definition

  PR.initialize(outfile);
  
  // If you want/don't want to decay some specific particle(s),
  // you can call PartDecayOn/PartDecayOff here.   
  // For example: Do not allow to decay K0S and Lambda
  //int ipart1 = 310;  // K0S
  //int ipart2 = 3122; // Lambda
  //PR.PartDecayOff(ipart1);
  //PR.PartDecayOff(ipart2);

  //Pyjet -> McPart converter
  //LCDPyj2McPart      py2mc;
  LCDPandPyth2McPart py2mc;

  printf("End of Generator Setup!\n");

  //------------< End Generator (Pandora-Pythia) setup>-------------------

  LCDEvent event;

  //------------< Setup Fast MC >------------------------
  printf("Start FastMC Setup!\n");

  // Call detector parameter file
  char* detectorGeomFile = "SDMar01.par"; // Silicon 
  TString s_geomFile(parfile_dir);
  s_geomFile += detectorGeomFile;
  FILE* m_geomfile =  fopen(s_geomFile.Data(),"r");
  LCDGetParameters gp(m_geomfile);
  fclose(m_geomfile);

  // If you want to change some detector parameter(s) for FastMC 
  // do here
  // For example..
  //gp.SetMagneticField(2.);  // Change Magnetif field

  // Call Tracker smearing parameter
  char* trkSmearFile = "lookup_silicon.nobmcon";
  TString s_smearFile(parfile_dir);
  s_smearFile += trkSmearFile;

  // Initialize FastMC
  LCDFastMC fmc(&gp, s_smearFile.Data(), &event);

  printf("End of FastMC Setup!\n");

  //------------< End Fast MC setup >--------------------

  //------------< Setup Event Display >------------------
  printf("Start EventDisplay Setup!\n");

  LCDEventDisplay evdisp(&gp);

  //Select Black-White option, if you like
  //evdisp->SetBlackWhite(); 

  evdisp.SetNoMUCAL();  // Do not display MUCAL part
  evdisp.SetNoCOIL();   // Do not display COIL part
//evdisp.SetNoHADCAL(); // Do not display HADCAL part
  evdisp.SetTheta(90.);
  evdisp.SetPhi(30.);

  printf("End of EventDisplay Setup!\n");

  //------------< End Event Display setup >--------------

  Int_t iEvent;
  for (Int_t iEvent=0; iEvent < nEvent ; iEvent++) {
    PR.getevent();      //Event Generation with pandora_pythia
    event.Clean();
    //py2mc.MakeMcPart(event.MCparticles()); //Make LCDMcPart 
    py2mc.MakeMcPart(&PR, event.MCparticles()); //Make LCDMcPart

    fmc.Doit();          //Through FastMC

    evdisp.Draw(&event); //Event Display

    Int_t a;
    printf("To get the next event, enter any number (<0 Exit Event loop)\n");
    scanf("%d",&a);
    if (a < 0) break;

  }
  
  PR.terminate();

  return iEvent;
}
