ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DAWNFILESceneHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4DAWNFILESceneHandler.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 // Satoshi TANAKA
29 // DAWNFILE scene.
30 
31 
32 #define __G_ANSI_C__
33 
34 // #define DEBUG_FR_SCENE
35 
36  //----- header files
37 #include <fstream>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sstream>
41 #include <iomanip>
42 #include "globals.hh"
43 #include "G4VisManager.hh"
44 #include "G4FRConst.hh"
45 #include "G4DAWNFILE.hh"
47 #include "G4DAWNFILEViewer.hh"
48 #include "G4Point3D.hh"
49 #include "G4VisAttributes.hh"
50 #include "G4Scene.hh"
51 #include "G4Transform3D.hh"
52 #include "G4Polyhedron.hh"
53 #include "G4Box.hh"
54 #include "G4Cons.hh"
55 #include "G4Polyline.hh"
56 #include "G4Trd.hh"
57 #include "G4Tubs.hh"
58 #include "G4Trap.hh"
59 #include "G4Torus.hh"
60 #include "G4Sphere.hh"
61 #include "G4Para.hh"
62 #include "G4Text.hh"
63 #include "G4Circle.hh"
64 #include "G4Square.hh"
65 #include "G4VPhysicalVolume.hh"
66 
67 //----- constants
68 const char FR_ENV_CULL_INVISIBLE_OBJECTS [] = "G4DAWN_CULL_INVISIBLE_OBJECTS";
69 const char G4PRIM_FILE_HEADER [] = "g4_";
70 const char DEFAULT_G4PRIM_FILE_NAME[] = "g4_0000.prim";
71 
72 // const int FR_MAX_FILE_NUM = 1 ;
73 // const int FR_MAX_FILE_NUM = 5 ;
74 // const int FR_MAX_FILE_NUM = 10 ;
75 // const int FR_MAX_FILE_NUM = 15 ;
76 // const int FR_MAX_FILE_NUM = 20 ;
77  const int FR_MAX_FILE_NUM = 100 ;
78 
79 
81 // Driver-dependent part //
83 
84 
85  //----- G4DAWNFILESceneHandler, constructor
87 G4VSceneHandler (system, fSceneIdCount++, name) ,
88 fSystem (system) ,
89 fPrimDest () ,
90 FRflag_in_modeling (false) ,
91 flag_saving_g4_prim (false) ,
92 COMMAND_BUF_SIZE (G4FRofstream::SEND_BUFMAX),
93 fPrec (9), fPrec2 (16)
94 {
95  // g4.prim filename and its directory
96  if ( std::getenv( "G4DAWNFILE_DEST_DIR" ) == NULL ) {
97  strcpy( fG4PrimDestDir , "" ) ; // output dir
98  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
99  } else {
100  strcpy( fG4PrimDestDir , std::getenv( "G4DAWNFILE_DEST_DIR" ) ); // output dir
101  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
102  }
103 
104  // maximum number of g4.prim files in the dest directory
105  fMaxFileNum = FR_MAX_FILE_NUM ; // initialization
106  if ( std::getenv( "G4DAWNFILE_MAX_FILE_NUM" ) != NULL ) {
107 
108  sscanf( std::getenv("G4DAWNFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
109 
110  } else {
112  }
113  if( fMaxFileNum < 1 ) { fMaxFileNum = 1 ; }
114 
115 
116  //----- precision control
117  if( std::getenv( "G4DAWNFILE_PRECISION" ) != NULL ) {
118  sscanf( std::getenv("G4DAWNFILE_PRECISION"), "%d", &fPrec ) ;
119  } else {
120  fPrec = 9 ;
121  }
122  fPrec2 = fPrec + 7 ;
123 
124 }
125 
126 
127  //----- G4DAWNFILESceneHandler, destructor
129 {
130 #if defined DEBUG_FR_SCENE
132  G4cout << "***** ~G4DAWNFILESceneHandler" << G4endl;
133 #endif
134  if (fPrimDest.IsOpen())
135  {
136  //----- End of modeling
137  // !EndModeling, !DrawAll, !CloseDevice,
138  // close g4.prim
139  FREndModeling();
140  }
141 }
142 
143 //-----
145 {
146  // g4_0000.prim, g4_0001.prim, ..., g4_MAX_FILE_INDEX.prim
147  const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
148 
149  // dest directory (null if no environmental variables is set)
150  strcpy ( fG4PrimFileName, fG4PrimDestDir) ;
151 
152  // create full path name (default)
154 
155  // Automatic updation of file names
156  for( int i = 0 ; i < fMaxFileNum ; i++) {
157 
158  // Message in the final execution
159  if( i == MAX_FILE_INDEX )
160  {
162  G4cout << "===========================================" << G4endl;
163  G4cout << "WARNING MESSAGE from DAWNFILE driver: " << G4endl;
164  G4cout << " This file name is the final one in the " << G4endl;
165  G4cout << " automatic updation of the output file name." << G4endl;
166  G4cout << " You may overwrite existing files, i.e. " << G4endl;
167  G4cout << " g4_XXXX.prim and g4_XXXX.eps " << G4endl;
168  G4cout << "===========================================" << G4endl;
169  }
170  }
171 
172  // re-determine file name as G4DAWNFILE_DEST_DIR/g4_XXXX.prim
173  std::ostringstream filename; filename
175  << std::setw(4) << std::setfill('0') << i << ".prim";
176  strncpy(fG4PrimFileName,filename.str().c_str(),sizeof(fG4PrimFileName)-1);
177  fG4PrimFileName[sizeof(fG4PrimFileName)-1] = '\0';
178 
179  // check validity of the file name
180  std::ifstream fin ;
181  fin.open(fG4PrimFileName) ;
182  if(!fin) {
183  // new file
184  fin.close();
185  break;
186  } else {
187  // already exists (try next)
188  fin.close();
189  }
190 
191  } // for
192 
193  G4cout << "=========================================== " << G4endl;
194  G4cout << "Output file: " << fG4PrimFileName << G4endl;
195  G4cout << "Destination directory (current dir if NULL): " << fG4PrimDestDir << G4endl;
196  G4cout << "Maximal number of files in the destination directory: " << fMaxFileNum << G4endl;
197  G4cout << "Note: " << G4endl;
198  G4cout << " * The maximal number is customizable as: " << G4endl;
199  G4cout << " % setenv G4DAWNFILE_MAX_FILE_NUM number " << G4endl;
200  G4cout << " * The destination directory is customizable as:" << G4endl;
201  G4cout << " % setenv G4DAWNFILE_DEST_DIR dir_name/ " << G4endl;
202  G4cout << " ** Do not forget \"/\" at the end of the " << G4endl;
203  G4cout << " dir_name, e.g. \"./tmp/\". " << G4endl;
204  G4cout << "=========================================== " << G4endl;
205 
206 } // G4DAWNFILESceneHandler::SetG4PrimFileName()
207 
208 
209 //-----
211 {
212 #if defined DEBUG_FR_SCENE
214  G4cout << "***** BeginSavingG4Prim (called)\n";
215 #endif
216 
217  if( !IsSavingG4Prim() )
218  {
219 #if defined DEBUG_FR_SCENE
221  G4cout << "***** (started) " ;
222  G4cout << "(open g4.prim, ##)" << G4endl;
223  }
224 #endif
225  SetG4PrimFileName() ; // result set to fG4PrimFileName
227 
230  }
231 }
232 
234 {
235 #if defined DEBUG_FR_SCENE
237  G4cout << "***** EndSavingG4Prim (called)\n";
238 #endif
239 
240  if( IsSavingG4Prim() )
241  {
242 #if defined DEBUG_FR_SCENE
244  G4cout << "***** (started) (close g4.prim)" << G4endl;
245 #endif
246  fPrimDest.Close() ;
248  }
249 }
250 
251 
252 //-----
254 {
255  if( !FRIsInModeling() )
256  {
257 #if defined DEBUG_FR_SCENE
259  G4cout << "***** G4DAWNFILESceneHandler::FRBeginModeling (called & started)" << G4endl;
260 #endif
261 
262  //----- Send saving command and heading comment
264 
265  //----- Send bounding box command
266  SendBoundingBox();
267 
268  //----- send SET_CAMERA command
269 #if defined DEBUG_FR_SCENE
271  G4cout << "***** (!SetCamera in FRBeginModeling())" << G4endl;
272 #endif
274 
275  //----- open device
276 #if defined DEBUG_FR_SCENE
278  G4cout << "***** (!OpenDevice in FRBeginModeling())" << G4endl;
279 #endif
281 
282  //----- begin sending primitives
283 #if defined DEBUG_FR_SCENE
285  G4cout << "***** (!BeginModeling in FRBeginModeling())" << G4endl;
286 #endif
288 
289  } // if
290 
291 }
292 
293 
295 // Common to DAWN and DAWNFILE drivers //
297 
298 #define G4FRSCENEHANDLER G4DAWNFILESceneHandler
299 #include "G4FRSceneFunc.icc"
300 #undef G4FRSCENEHANDLER
301 
303 // static variables //
305 
306  //----- static variables