source: MEMPHYS/HEAD/applications/MEMPHYS_session.cxx @ 108

Last change on this file since 108 was 108, checked in by barrand, 18 years ago
File size: 6.1 KB
Line 
1
2// Geant4:
3#include <G4ios.hh>
4#include <G4RunManager.hh>
5#include <G4UImanager.hh>
6#include <G4SDManager.hh>
7
8//AIDA :
9#include <AIDA/IAnalysisFactory.h>
10
11// std::
12#include <iostream>
13
14// Lib :
15#include <Lib/Interfaces/IManager.h>
16#include <Lib/Interfaces/ISession.h>
17#include <Lib/Interfaces/ITypeManager.h>
18#include <Lib/Manager.h>
19#include <Lib/System.h>
20#include <Lib/Out.h>
21#include <Lib/Cast.h>
22
23// G4Lab :
24#include <G4Lab/UIOnX.h>
25#include <G4Lab/TrackingAction.h>
26#include <G4Lab/Interfaces/IGeant4Manager.h>
27//#include <G4Lab/DigitsCollectionType.h>
28
29//MEMPHYS:
30#include "MEMPHYS/Analysis.hh"
31#include "MEMPHYS/DetectorConstruction.hh"
32#include "MEMPHYS/PhysicsList.hh"
33#include "MEMPHYS/PhysicsMessenger.hh"
34#include "MEMPHYS/PrimaryGeneratorAction.hh"
35#include "MEMPHYS/EventAction.hh"
36#include "MEMPHYS/RunAction.hh"
37#include "MEMPHYS/StackingAction.hh"
38#include "MEMPHYS/TrackingAction.hh"
39#include "MEMPHYS/SteppingAction.hh"
40#include "MEMPHYS/WCDigi.hh"
41#include <MEMPHYS/HitsCollectionType.h>
42#include <MEMPHYS/DigitsCollectionType.h>
43
44// Create a manager to access the MEMPHYSAnalysis from the callbacks :
45
46//JEC 10/1/06 introduce MEMPHYS namespace
47namespace MEMPHYS {
48
49class AppManager 
50  :public virtual IManager
51  ,public Analysis  {
52 public: //IManager
53  virtual const std::string& name() const { return fName;}
54  virtual void* cast(const std::string& aTo) const {
55    if(aTo=="MEMPHYS::AppManager") {
56      return Lib_SCast(MEMPHYS::AppManager);
57    } else if(aTo=="MEMPHYS::IAppManager") {
58      return Lib_SCast(MEMPHYS::IAppManager);
59    } else if(aTo=="MEMPHYS::Analysis") {
60      return Lib_SCast(MEMPHYS::Analysis);
61    } else if(aTo=="IManager") {
62      return Lib_SCast(IManager);
63    } else {
64      return 0;
65    }
66  }
67 public://IAppManager
68  virtual bool initialize() {
69    //GB :
70    // Used in the MEMPHYS_Initialize callback.
71    // Do all the below here because of platforms having
72    // not Geant4 shared libs.
73    IGeant4Manager* g4Manager = 
74      Lib_findManager(fSession,"Geant4Manager",IGeant4Manager);
75    if(!g4Manager) {
76      Lib::Out out(fSession.printer());
77      out << "MEMPHYS_Initialize :"
78          << " Geant4Manager not found."
79          << Lib::endl;
80      return false;
81    }
82
83    // To initialize G4Lab Types (PV, Trajectory, HitsCollection, etc.. types).
84    // To initialize G4Lab SoNodes.
85    g4Manager->initialize();
86
87    // Overload the HitsCollectionType of G4Lab :
88    Lib::ITypeManager* typeManager = 
89      Lib_findManager(fSession,"TypeManager",Lib::ITypeManager);
90    if(typeManager) {
91      G4SDManager* sdManager = g4Manager->sdManager();
92      if(sdManager) {
93        G4HCtable* hcTable  = sdManager->GetHCtable();
94        if(hcTable) {
95          int number = hcTable->entries();
96          for(int index=0;index<number;index++) {
97            G4String hcName = hcTable->GetHCname(index);
98            typeManager->removeType(hcName);
99            typeManager->addType
100              (new MEMPHYS::HitsCollectionType(fSession,hcName));
101          }
102        }
103      }
104
105      /*
106      typeManager->addType
107        (new G4Lab::DigitsCollectionType<MEMPHYS::WCDigi>
108          (fSession,"WCDigitizedCollection"));
109      */
110      typeManager->addType
111        (new MEMPHYS::DigitsCollectionType<MEMPHYS::WCDigi>
112          (fSession,"WCDigitizedCollection"));
113    }
114    return true;
115  }
116 public:
117  AppManager(ISession& aSession,AIDA::IAnalysisFactory& aAIDA,bool aBatch)
118    :Analysis(aAIDA,aBatch),fSession(aSession),fName("MEMPHYS::AppManager") {}
119  virtual ~AppManager(){}
120 private:
121  ISession& fSession;
122  std::string fName;
123};
124}
125
126//////////////////////////////////////////////////////////////////////////////
127int main(
128         int aArgc
129         ,char** aArgv
130         ) 
131//////////////////////////////////////////////////////////////////////////////
132//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
133{
134  //Lib::Debug::checkByClass(true);
135
136  G4RunManager* runManager = new G4RunManager;
137
138  // Need to pass the G4RunManager at UIOnX creation
139  std::string gui = "$MEMPHYSROOT/scripts/OnX/MEMPHYS_session.onx";
140  G4Lab::UIOnX* session = new G4Lab::UIOnX(*runManager,gui,aArgc,aArgv);
141  if(!session->isValid()) {
142    std::cout << "MEMPHYS_session : problem starting OnX." << std::endl;
143    return EXIT_FAILURE;
144  }
145
146  MEMPHYS::AppManager* appManager = 0;
147
148  AIDA::IAnalysisFactory* aida = session->findAIDA();
149  if(!aida) {
150    std::cout << "MEMPHYS_session : AIDA not found." << std::endl;
151  } else {
152    appManager = new MEMPHYS::AppManager(*session->session(),*aida,false);
153    session->addManager(appManager);
154  }
155
156  G4UImanager* UI = G4UImanager::GetUIpointer();
157  if (UI) {
158    std::string file = 
159      Lib::System::getenv("MEMPHYSROOT")+"/scripts/Geant4/jobOptions.mac";
160    UI->ApplyCommand("/control/execute "+file); 
161  }
162
163 
164  //JEC 18/11/05 give the appManager to fill geometry ITuple
165  MEMPHYS::DetectorConstruction* MEMPHYSdetector = 
166    new MEMPHYS::DetectorConstruction(*appManager); 
167
168  runManager->SetUserInitialization(MEMPHYSdetector);
169
170  runManager->SetUserInitialization(new MEMPHYS::PhysicsList);
171
172  // Set user action classes
173  MEMPHYS::PrimaryGeneratorAction* myGeneratorAction = 
174    new MEMPHYS::PrimaryGeneratorAction(MEMPHYSdetector);
175  runManager->SetUserAction(myGeneratorAction);
176
177  MEMPHYS::RunAction* myRunAction = new MEMPHYS::RunAction();
178
179  runManager->SetUserAction(myRunAction);
180
181
182  runManager->SetUserAction(new MEMPHYS::EventAction(*appManager,
183                                                   myRunAction, 
184                                                   MEMPHYSdetector,
185                                                   myGeneratorAction));
186  runManager->SetUserAction(new MEMPHYS::TrackingAction);
187
188  runManager->SetUserAction(new MEMPHYS::StackingAction);
189
190  runManager->SetUserAction(new MEMPHYS::SteppingAction); //JEC 15/12/05 Add user SteppingAction
191
192  // Initialize G4 kernel
193  runManager->Initialize();
194
195  if (UI) {
196    std::string file = 
197      Lib::System::getenv("MEMPHYSROOT")+"/scripts/Geant4/init.g4m";
198    UI->ApplyCommand("/control/execute "+file); 
199  }
200
201
202  // Main UI loop :
203  // GUI is really constructed here.
204  // Create callbacks are executed here too.
205  session->SessionStart();
206
207  delete session; //It will delete the appManager.
208
209  delete runManager;
210
211  return EXIT_SUCCESS;
212}
213
Note: See TracBrowser for help on using the repository browser.