00001 // BLFOR009.hh 00002 /* 00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com 00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 http://www.gnu.org/copyleft/gpl.html 00017 */ 00018 #ifndef BLFOR009_HH 00019 #define BLFOR009_HH 00020 00021 #include <stdio.h> 00022 #include <map> 00023 00024 #include "globals.hh" 00025 #include "G4ThreeVector.hh" 00026 00027 #include "BLTrackFile.hh" // BLTrackFileStatus only 00028 00029 /* A Region is placed in Z at the first track written to it. All tracks 00030 aving Z within REGION_SIZE of that first track are put into the same Region. 00031 If z is monotonically increasing during tracking, this handles a ring 00032 correctly (for N orbits around the ring, a single virtualdetector will 00033 generate N regions). 00034 */ 00035 const double REGION_SIZE = 1.0*cm; 00036 struct Region; // in BLFOR009.cc 00037 struct RegionCompare { 00038 bool operator()(const double a, const double b) const 00039 { return a < b-REGION_SIZE; } 00040 }; 00041 typedef std::map<double,Region*,RegionCompare> RegionMap; 00042 00043 /** class BLFOR009 creates an ASCII file containing tracks in ICOOL 00044 * FOR009.DAT format. 00045 * 00046 * NOTE: Because the FOR009.DAT format requires the particles be sorted 00047 * by region (JSRG), all tracks are stored in memory until close() is 00048 * called; the file is written at that time. The ICOOL region is 00049 * determined from the Z position of the track, and is simply 00050 * sequentially assigned as tracks at different Z positions are written. 00051 * The region is +-REGION_SIZE in Z from the first track of each region. 00052 * 00053 * XP[], PP[], BFLD[], and EFLD[] should all be converted to 00054 * Centerline coordinates before calling write(). 00055 * 00056 * This data format comes from ICOOL v 2.77, User's Guide section 4.2.3. 00057 * The first line of the file is the title. 00058 * The second and third lines are comments, supposedly units and column 00059 * labels. 00060 * There follow the tracks, one per line, sorted by "region". The first 00061 * track of each region should be the "reference" particle in ICOOL 00062 * parlance; in g4beamline it is the reference particle -- if multiple 00063 * reference particles intersect the region, the first will be 00064 * "reference", and the following ones will be considered "beam". 00065 * The variables for each track are: 00066 * IEVT (I) event # 00067 * IPNUM (I) track # for this event 00068 * IPTYP (I) particle type: >0 for positive charge, 00069 * <0 for negative 00070 * 1=e, 2=mu, 3=pi, 4=K, 5=proton 00071 * IPFLG (I) "flag", always 0 00072 * JSRG (I) region number (see above) 00073 * TP (F) time (sec) 00074 * XP[3] (F) position (meters) 00075 * PP[3] (F) momentum (GeV/c) 00076 * BFLD[3] (F) Magnetic field (Tesla) 00077 * EVTWT (F) weight 00078 * EFLD[3] (F) Electric field (V/meter) 00079 * SARC (F) arclength (meter) -- set to 0.0 00080 * POL[3] (F) spin -- set to 0.0 00081 * 00082 * Note that event 0 will be ignored, as that value of IEVT is 00083 * reserved for the reference particle. 00084 **/ 00085 class BLFOR009 { 00086 G4String filename; 00087 BLTrackFileStatus status; 00088 FILE *file; 00089 G4String mode; 00090 RegionMap *map; 00091 public: 00092 /// Constructor. Opens the file; mode="r" or "w". 00093 /// title is an identifying title in the first line of the file 00094 /// (ignored for mode=r). 00095 /// READ MODE IS UNIMPLEMENTED. 00096 BLFOR009(G4String _filename, G4String title, G4String mode); 00097 00098 /// Destructor. Writes any data and closes the file. 00099 ~BLFOR009(); 00100 00101 /// close() writes the data to the file and closes it. 00102 void close(); 00103 00104 /// write() will write one track to the file (mode must have been "w"). 00105 /// Units are standard geant4 internal units. 00106 /// Note that weight is optional. 00107 BLTrackFileStatus write(G4ThreeVector& pos, G4double time, 00108 G4ThreeVector& momentum, G4ThreeVector& Bfield, 00109 G4ThreeVector& Efield, int PDGid, int eventId, 00110 int trackId, int parentId, G4double weight=1.0); 00111 00112 /// read() will read one track from the file (mode must have been "r"). 00113 /// Units are standard geant4 internal units. 00114 /// Use the other version of read() if you want to use the weight. 00115 /// UNIMPLEMENTED. 00116 BLTrackFileStatus read(G4ThreeVector& pos, G4double& time, 00117 G4ThreeVector& momentum, G4ThreeVector& Bfield, 00118 G4ThreeVector& Efield, int& PDGid, int& eventId, 00119 int& trackId, int& parentId); 00120 /// read() will read one track from the file (mode must have been "r"). 00121 /// Units are standard geant4 internal units. 00122 /// UNIMPLEMENTED. 00123 BLTrackFileStatus read(G4ThreeVector& pos, G4double& time, 00124 G4ThreeVector& momentum, G4ThreeVector& Bfield, 00125 G4ThreeVector& Efield, int& PDGid, int& eventId, 00126 int& trackId, int& parentId, G4double &weight); 00127 }; 00128 00129 #endif // BLFOR009_HH