Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

EusoMainFrame - source file

// class EusoMainFrame
// GUI interface for ESAF : Euso Simulation and Analysis Framework
// M. Pallavicini created       28-12-2001
//                new structure 09-02-2002
//                

#include "EusoMainFrame.hh"
#include "SimuApplication.hh"
#include "DetectorConfigDlg.hh"
#include "ConfigEditorDlg.hh"
#include "OutputRenameDlg.hh"
#include "ShowHistoDlg.hh"
#include "Config.hh"
#include "TGFileDialog.h"
#include "TROOT.h"
#include "TGMsgBox.h"
#include "TKey.h"
#include "TTree.h"

#include <stdlib.h>

ClassImp(EusoMainFrame)

#define  NOT_IMPLEMENTED_YET() { new TGMsgBox(fClient->GetRoot(),\
		                   this,\
			           "ESAF Message",\
				   "This function is not implemented yet. Sorry!",\
				   kMBIconExclamation);}

// file extension and description for Configuration files
static const char *gConfigFileTypes[] = { "Euso Config Files", "*.cfg" , 
                                          "All files", "*.*", 
                                          0,0 };

// file extensions and descriptions for input files
static const char *gInputFileTypes[] = { "Shower Tracks", "*.stk" ,
                                         "Light in Atmosphere", "*.lgt",
					 "Photons at Euso", "*.pht",
                                         "All files", "*.*", 
                                          0,0 };

// file extensions and descriptions for root files
static const char *gRootFileTypes[] = { "Euso root files", "*.root" ,
                                        "All files", "*.*", 
                                        0,0 };

// build the main window with its menu
 EusoMainFrame::EusoMainFrame( SimuApplication* app, const TGWindow *p , 
  UInt_t w, UInt_t h ) : TGMainFrame( p, w, h ), theApp( app ) {

   if ( !theApp ) {
     fprintf(stdout,"Error: SimuApplication not validn");
     exit(2);
   }

   // layout hints for menu items and menu bar
   fMenuLayout = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandX,0,0,1,1);
   fItemLayout = new TGLayoutHints(kLHintsTop|kLHintsLeft,0,4,0,0);
   fHelpLayout = new TGLayoutHints(kLHintsTop|kLHintsRight);

   // create main menu
   // file
   fMenuFile = new TGPopupMenu( fClient->GetRoot() );
   fMenuFile->AddLabel("Input file");
   fMenuFile->AddEntry("&Open",E_FILE_OPEN_INPUT);
   fMenuFile->AddEntry("&Close",E_FILE_CLOSE_INPUT);
   fMenuFile->AddSeparator();
   fMenuFile->AddLabel("Output file");
   fMenuFile->AddEntry("&Rename",E_FILE_RENAME_OUTPUT);
   fMenuFile->AddSeparator();
   fMenuFile->AddLabel("Root file");
   fMenuFile->AddEntry("O&pen input",E_FILE_OPEN_ROOT_INP);
   fMenuFile->AddEntry("C&lose input",E_FILE_CLOSE_ROOT_INP);
   fMenuFile->AddEntry("Clo&se output",E_FILE_CLOSE_ROOT_OUT);
   fMenuFile->AddSeparator();
   fMenuFile->AddEntry("E&xit",E_FILE_EXIT_PROGRAM);
   fMenuFile->Associate( this );
   fMenuFile->DisableEntry(E_FILE_CLOSE_INPUT);
   fMenuFile->DisableEntry(E_FILE_CLOSE_ROOT_INP);
   fMenuFile->DisableEntry(E_FILE_CLOSE_ROOT_OUT);

   // config
   fMenuConfig = new TGPopupMenu( fClient->GetRoot() );
   fMenuConfig->AddLabel("Files");
   fMenuConfig->AddEntry("&Edit files",E_CONFIG_SELECT_FILES);
   fMenuConfig->AddSeparator();
   fMenuConfig->AddLabel("Parameters");
   
   fCascadeShowerMenu = new TGPopupMenu(fClient->GetRoot());
   fCascadeShowerMenu->AddEntry("Slast",E_CONFIG_SHOWER_SLAST);
   fCascadeShowerMenu->AddEntry("Corsika",E_CONFIG_SHOWER_CORSIKA);
   fCascadeShowerMenu->AddEntry("AP Gil",E_CONFIG_SHOWER_APGIL);
   
   fCascadeLightMenu = new TGPopupMenu(fClient->GetRoot());
   fCascadeLightMenu->AddEntry("TestLightToEuso",E_CONFIG_TESTLIGHTTOEUSO);
   fCascadeLightMenu->AddEntry("2",E_NOT_IMPLEMENTED);
   
   fCascadeAtmosphereMenu = new TGPopupMenu(fClient->GetRoot());
   fCascadeAtmosphereMenu->AddEntry("1",E_NOT_IMPLEMENTED);
   fCascadeAtmosphereMenu->AddEntry("2",E_NOT_IMPLEMENTED);
   
   fCascadeDetectorMenu = new TGPopupMenu(fClient->GetRoot());
   fCascadeDetectorMenu->AddEntry("Euso",E_CONFIG_EUSO);
   
   fMenuConfig->AddPopup("Shower Generators",fCascadeShowerMenu);
   fMenuConfig->AddPopup("Light Generators",fCascadeLightMenu);
   fMenuConfig->AddPopup("Atmosphere",fCascadeAtmosphereMenu);
   fMenuConfig->AddPopup("Euso Detector",fCascadeDetectorMenu); 
    
   fMenuConfig->Associate( this );

   // run
   fMenuRun = new TGPopupMenu( fClient->GetRoot() );
   fMenuRun->AddEntry("&Single",E_RUN_SINGLE);
   fMenuRun->AddEntry("&All",E_RUN_ALL);
   fMenuRun->AddSeparator();
   fMenuRun->AddEntry("&Rerun last",E_RUN_REDO);
   fMenuRun->AddEntry("&Rerun all",E_RUN_REDOALL);
   fMenuRun->AddSeparator();
   fMenuRun->AddEntry("&Parameters",E_RUN_SET);
   fMenuRun->Associate( this );

   // event display
   fMenuDisplay = new TGPopupMenu( fClient->GetRoot() );
   fMenuDisplay->AddEntry("&Show",E_DISPLAY_SHOW_DATA);    // single event histograms
   fMenuDisplay->AddEntry("&Dump",E_DISPLAY_DUMP_DATA);    // dump some useful numbers
   fMenuDisplay->AddEntry("&Print",E_DISPLAY_PRINT_DATA);  // print ( same as dump )
   fMenuDisplay->AddSeparator();
   fMenuDisplay->AddEntry("&Toggle 3D Display",E_DISPLAY_3D_EVENT);  // enable/dis 3D viewer
   fMenuDisplay->Associate( this );
   EnableDisplay(kFALSE);

   // root objects: histograms and tree
   fMenuRoot =  new TGPopupMenu( fClient->GetRoot() );
   fMenuRoot->AddEntry("&Histograms",E_ROOT_HIST);  // set of histogram with all events in root file
   fMenuRoot->AddEntry("&Tree View",E_ROOT_TREE);   // open tree viewer
   fMenuRoot->AddEntry("&Browser",E_ROOT_BROWSER);  // open TBrowser
   fMenuRoot->Associate( this );

   // help
   fMenuHelp = new TGPopupMenu( fClient->GetRoot() );
   fMenuHelp->AddEntry("&Contents",E_HELP_CONTENTS);
   fMenuHelp->AddEntry("&About",E_HELP_ABOUT);
   fMenuHelp->Associate( this );

   fMenu = new TGMenuBar( this, 1, 1, kHorizontalFrame );
   fMenu->AddPopup("&File",fMenuFile,fItemLayout);
   fMenu->AddPopup("&Config",fMenuConfig,fItemLayout);
   fMenu->AddPopup("&Run",fMenuRun,fItemLayout);
   fMenu->AddPopup("Event &Display",fMenuDisplay,fItemLayout);
   fMenu->AddPopup("Roo&t",fMenuRoot,fItemLayout);
   fMenu->AddPopup("&Help",fMenuHelp,fHelpLayout);
   AddFrame( fMenu, fMenuLayout );

   // crate main window
   fWindow = new TGCanvas( this, 300, 100 );
   fWindowContainer = new TGCompositeFrame( fWindow->GetViewPort(), 300, 100);
   fWindow->SetContainer( fWindowContainer );
   AddFrame( fWindow, new TGLayoutHints(kLHintsTop|kLHintsLeft));

   SetWindowName("ESAF Main");
   MapSubwindows();
   Resize( GetDefaultSize() );
   MapWindow();

   fRootInputFile = NULL;
}

// dtor
 EusoMainFrame::~EusoMainFrame() {
}

// close the main Window and exit the program
 void EusoMainFrame::CloseWindow() {
   theApp->EndJob();
   TGMainFrame::CloseWindow();
   gApplication->Terminate(0);
}

// handle menu commands and main window commands
Bool_t 
 EusoMainFrame::ProcessMessage( Long_t msg, Long_t p1, Long_t p2) {
  switch ( GET_MSG( msg ) ) {
    case kC_COMMAND:
      switch ( GET_SUBMSG( msg ) ) {
        case kCM_BUTTON:
	  break;
	case kCM_MENUSELECT:
	  break;
	case kCM_MENU:                       // message from the main menu
	  HandleMenuCommand( p1 );
	  break;
	default:
	  break;
      }
      break;
    default:
      break;
  }
  return kTRUE;
}

// enable or disable Event Display menu commands
 void EusoMainFrame::EnableDisplay( Bool_t enable ) {
    if ( enable ) {
      fMenuDisplay->EnableEntry(E_DISPLAY_SHOW_DATA);
      fMenuDisplay->EnableEntry(E_DISPLAY_DUMP_DATA);
      fMenuDisplay->EnableEntry(E_DISPLAY_PRINT_DATA);
    } else {
      fMenuDisplay->DisableEntry(E_DISPLAY_SHOW_DATA);
      fMenuDisplay->DisableEntry(E_DISPLAY_DUMP_DATA);
      fMenuDisplay->DisableEntry(E_DISPLAY_PRINT_DATA);
    }
    fMenuDisplay->UnCheckEntry(E_DISPLAY_3D_EVENT);
}

// enable or disable Compare
 void EusoMainFrame::EnableCompare( Bool_t enable ) {
    if ( enable ) {
    } else {
    }
}

// handle a gui command
#define EusoCommandAssociate(s1,s2) {if(p==s1) { s2; return; }}
 void EusoMainFrame::HandleMenuCommand(Int_t p ) {
    EusoCommandAssociate(E_FILE_OPEN_INPUT, GuiFileOpenInput());
    EusoCommandAssociate(E_FILE_CLOSE_INPUT, GuiFileCloseInput());
    EusoCommandAssociate(E_FILE_RENAME_OUTPUT,GuiFileRenameOutput());
    EusoCommandAssociate(E_FILE_OPEN_ROOT_INP,GuiFileOpenRootInput());
    EusoCommandAssociate(E_FILE_CLOSE_ROOT_INP,GuiFileCloseRootInput());
    EusoCommandAssociate(E_FILE_CLOSE_ROOT_OUT,GuiFileCloseRootOutput());
    EusoCommandAssociate(E_FILE_EXIT_PROGRAM, CloseWindow());
    EusoCommandAssociate(E_CONFIG_SELECT_FILES, GuiConfigSelectFiles());
    EusoCommandAssociate(E_CONFIG_SHOWER_SLAST, GuiConfigShowerBySlast());
    EusoCommandAssociate(E_CONFIG_TESTLIGHTTOEUSO, GuiConfigTestLightToEuso());
    EusoCommandAssociate(E_CONFIG_EUSO, GuiConfigEuso());
    EusoCommandAssociate(E_RUN_SINGLE, GuiRunSingle() );
    EusoCommandAssociate(E_RUN_ALL,GuiRunAll());
    EusoCommandAssociate(E_RUN_REDO,GuiRunRedo());
    EusoCommandAssociate(E_RUN_REDOALL, GuiRunRedoAll());
    EusoCommandAssociate(E_RUN_SET, GuiRunSet());
    EusoCommandAssociate(E_DISPLAY_SHOW_DATA,GuiDisplayShowData());
    EusoCommandAssociate(E_DISPLAY_DUMP_DATA,GuiDisplayDumpData());
    EusoCommandAssociate(E_DISPLAY_3D_EVENT,GuiDisplayToggle3DViewer());
    EusoCommandAssociate(E_ROOT_HIST,GuiRootHistos());
    EusoCommandAssociate(E_ROOT_TREE,GuiRootTree());
    EusoCommandAssociate(E_ROOT_BROWSER,GuiRootBrowser());
}

// individual command functions

// open input file
// FIXME: just demo right now....
 void EusoMainFrame::GuiFileOpenInput() {
    TGFileInfo *info = new TGFileInfo();
    info->fIniDir = new char[500];
    sprintf(info->fIniDir,"./");
    info->fFilename = 0;
    info->fFileTypes = gInputFileTypes;
    new TGFileDialog(fClient->GetRoot(),this,kFDOpen,info);
    if ( info->fFilename ) {
	ConfigFileParser* runpar = Config::Get()->GetCF("General","Run");
	runpar->ReplaceStr("InputFileName",string(info->fFilename) );
	fMenuFile->DisableEntry(E_FILE_OPEN_INPUT);
        fMenuFile->EnableEntry(E_FILE_CLOSE_INPUT);
    }
    delete info;
    EnableDisplay( kFALSE );
}

// close input file
 void EusoMainFrame::GuiFileCloseInput() {
    fMenuFile->DisableEntry(E_FILE_CLOSE_INPUT);	      
    fMenuFile->EnableEntry(E_FILE_OPEN_INPUT);
    EnableDisplay( kFALSE );	   
}

// rename output file name (root file)
 void EusoMainFrame::GuiFileRenameOutput() {
//    fClient->WaitFor( new OutputRenameDlg(fClient->GetRoot(),this)); 
    NOT_IMPLEMENTED_YET(); 
}

// open existing root file for reading
 void EusoMainFrame::GuiFileOpenRootInput() {
    if ( theApp->Manager()->GetFile() ) {
        char s[300];
        sprintf(s,"Simulation of event %d completed",theApp->EvCounter());
        new TGMsgBox(fClient->GetRoot(), this, 
                "ESAF Message", 
                "You must close the root output file first!!nGo to "File" --> Close output",
                kMBIconExclamation);
        return;
    }
    TGFileInfo *info = new TGFileInfo();
    info->fIniDir = new char[500];
    sprintf(info->fIniDir,"./");
    info->fFilename = 0;
    info->fFileTypes = gRootFileTypes;
    new TGFileDialog(fClient->GetRoot(),this,kFDOpen,info);
    if ( info->fFilename ) {
        OpenRootInputFile( info->fFilename );
    }
    delete info;
    fMenuFile->EnableEntry(E_FILE_CLOSE_ROOT_INP);
    fMenuFile->DisableEntry(E_FILE_OPEN_ROOT_INP);
    EnableDisplay( kTRUE);
}

// helper functions for opening and closing root file
 void EusoMainFrame::CloseRootInputFile() {
    if ( RootInputFile())
        RootInputFile()->Close();
    fRootInputFile = NULL;
}

 void EusoMainFrame::OpenRootInputFile( const char* fname) {
    CloseRootInputFile();
    fRootInputFile = new TFile( fname );
    if ( fRootInputFile->IsZombie() ) {
	char msg[300];
	sprintf(msg,"File %s not found",fname);
        new TGMsgBox(fClient->GetRoot(),this,"ESAF Message",msg,kMBIconExclamation);
	fRootInputFile = NULL;
    }
}

// close root file for reading
 void EusoMainFrame::GuiFileCloseRootInput() {
    if ( RootInputFile() )
        CloseRootInputFile();
    fMenuFile->EnableEntry(E_FILE_OPEN_ROOT_INP);
    fMenuFile->DisableEntry(E_FILE_CLOSE_ROOT_INP);
}

// close root file for writing
 void EusoMainFrame::GuiFileCloseRootOutput() {
    if ( theApp->Manager()->GetFile() )
        theApp->Manager()->Close();
    fMenuFile->EnableEntry(E_FILE_OPEN_ROOT_INP);
    fMenuFile->DisableEntry(E_FILE_CLOSE_ROOT_INP);
}

// edit config file
 void EusoMainFrame::GuiConfigSelectFiles() {
    theApp->SetLock();
    TGFileInfo *info = new TGFileInfo();
    info->fIniDir = new char[500];
    sprintf(info->fIniDir,"./config");
    info->fFilename=0;
    info->fFileTypes = gConfigFileTypes;
    new TGFileDialog(fClient->GetRoot(),this,kFDOpen,info);
    if ( info->fFilename ) {
	new ConfigEditorDlg(theApp,info->fFilename,this);
    }
    delete info;
}

// configure ShowerBySlast parameters
 void EusoMainFrame::GuiConfigShowerBySlast() {
    theApp->SetLock();
    fClient->WaitFor( new ConfigEditorDlg(theApp,
		      "config/LightToEuso/SlastLightToEuso.cfg",this ) );
    theApp->SetLock( false );
    theApp->SetChanged(kESAFLight);
    theApp->Build();
}

// configure TestLightToEuso parameters
 void EusoMainFrame::GuiConfigTestLightToEuso(){
    theApp->SetLock();
    fClient->WaitFor(new ConfigEditorDlg(theApp, 
	             "./config/LightToEuso/TestLightToEuso.cfg",this));
    theApp->SetLock( false );
    theApp->SetChanged(kESAFLight);
    theApp->Build();
}

// configure euso detector parameters
 void EusoMainFrame::GuiConfigEuso() {
    theApp->SetLock();
    fClient->WaitFor( new DetectorConfigDlg(theApp,fClient->GetRoot(),this ) );
    theApp->SetLock( false );
}

// execute one single event
// run program on a single event
 void EusoMainFrame::DoEvent() {
    if ( ! (theApp->DoEvent(false,false)) ) {
	char s[300];
	sprintf(s,"An error occurred during simulation of event %d",
	    theApp->EvCounter() );
	new TGMsgBox(fClient->GetRoot(), this,
	    "Euso Application Error",s,kMBIconExclamation);
    }
    else {
	char s[300];
	sprintf(s,"Simulation of event %d completed",theApp->EvCounter());
	new TGMsgBox(fClient->GetRoot(), this, 
	    "Euso Application Message", s,kMBIconExclamation);
    }
}


// run program on a single event
// in this case the root file is not written and the event just kept in memory
//
 void EusoMainFrame::GuiRunSingle() {
    theApp->ResetCopies();
    EnableCompare( false );
    if ( theApp->Manager()->GetFile() ) {
        theApp->Manager()->Close();
        fMenuFile->DisableEntry(E_FILE_CLOSE_ROOT_OUT);
    }

    //    theApp->CreateRootEvent();
    DoEvent();
    EnableDisplay();  
}

// redo last event
 void EusoMainFrame::GuiRunRedo() {
    // keep a copy of last event
    theApp->CopyEvent();
    // do event
    DoEvent();
    EnableCompare( true );
}

// run all events 
 void EusoMainFrame::GuiRunAll() {
    NOT_IMPLEMENTED_YET();
}

// re-run from beginning
 void EusoMainFrame::GuiRunRedoAll() {
    NOT_IMPLEMENTED_YET();
}

// set run parameters
 void EusoMainFrame::GuiRunSet() {
    NOT_IMPLEMENTED_YET();
}

// show data (histograms and numbers ) for last simulated event
// just open a dedicated dialog and returns
 void EusoMainFrame::GuiDisplayShowData() {
    new ShowHistoDlg(theApp,fClient->GetRoot(),this);
}

// dump data for last simulated event
 void EusoMainFrame::GuiDisplayDumpData() {
    NOT_IMPLEMENTED_YET();
}

// enable/disable 3D viewer
 void EusoMainFrame::GuiDisplayToggle3DViewer() {
}

// histograms from one root file
 void EusoMainFrame::GuiRootHistos() {
    TTree *tree = GetTree();
    if ( tree ) {
        new ShowHistoDlg(theApp,fClient->GetRoot(),this,tree);
    }
}

 TTree* EusoMainFrame::GetTree() {
    if ( RootInputFile() ) {
        RootInputFile()->cd();
	TTree* tree = (TTree*)gDirectory->FindObject( "etree" );
	if ( !tree ) {
	    tree = (TTree*)gDirectory->GetKey("etree")->ReadObj();
	    if (!tree) {
	        new TGMsgBox(fClient->GetRoot(),this,"ESAF Message",
				   "Cannot find a valid Euso Tree",
				   kMBIconExclamation);
	    }
	}
	return tree;
    }
    new TGMsgBox(fClient->GetRoot(), this, "ESAF Message",
		"File not opened", kMBIconExclamation );
    return 0;
}

// open tree viewer
 void EusoMainFrame::GuiRootTree() {
}

// display root browser
 void EusoMainFrame::GuiRootBrowser() {
    TBrowser *Browser = new TBrowser("EusoBrowser","Euso root objects");
    Browser->Show();
}


About Us | EUSO Official Website | Web pages created by Roberto Pesce and Alessandro Thea - Last Update Wed Nov 16 16:57:39 2005 Wed Nov 16 16:29:22 2005