Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // 27 // 28 28 29 #include "G4TheMTRayTracer.hh" 29 #include "G4TheMTRayTracer.hh" 30 #include "G4SystemOfUnits.hh" 30 #include "G4SystemOfUnits.hh" 31 #include "G4RTMessenger.hh" 31 #include "G4RTMessenger.hh" 32 #include "G4VFigureFileMaker.hh" 32 #include "G4VFigureFileMaker.hh" 33 #include "G4RTJpegMaker.hh" 33 #include "G4RTJpegMaker.hh" 34 #include "G4RTRun.hh" 34 #include "G4RTRun.hh" 35 #include "G4RTRunAction.hh" 35 #include "G4RTRunAction.hh" 36 #include "G4RTWorkerInitialization.hh" 36 #include "G4RTWorkerInitialization.hh" 37 #include "G4VRTScanner.hh" 37 #include "G4VRTScanner.hh" 38 38 39 #include "G4MTRunManager.hh" 39 #include "G4MTRunManager.hh" 40 #include "G4SDManager.hh" 40 #include "G4SDManager.hh" 41 #include "G4StateManager.hh" 41 #include "G4StateManager.hh" 42 #include "G4Colour.hh" 42 #include "G4Colour.hh" 43 #include "G4VisAttributes.hh" 43 #include "G4VisAttributes.hh" 44 #include "G4UImanager.hh" 44 #include "G4UImanager.hh" 45 #include "G4UIcommand.hh" 45 #include "G4UIcommand.hh" 46 #include "G4VVisManager.hh" 46 #include "G4VVisManager.hh" 47 #include "G4RunManagerFactory.hh" 47 #include "G4RunManagerFactory.hh" 48 48 49 #define G4warn G4cout 49 #define G4warn G4cout 50 50 51 G4TheMTRayTracer* G4TheMTRayTracer::theInstanc 51 G4TheMTRayTracer* G4TheMTRayTracer::theInstance = nullptr; 52 52 53 G4TheMTRayTracer::G4TheMTRayTracer(G4VFigureFi 53 G4TheMTRayTracer::G4TheMTRayTracer(G4VFigureFileMaker* figMaker, 54 G4VRTScanner* scanner) 54 G4VRTScanner* scanner) 55 : G4TheRayTracer(figMaker,scanner) 55 : G4TheRayTracer(figMaker,scanner) 56 { 56 { 57 if(!theInstance) 57 if(!theInstance) 58 { theInstance = this; } 58 { theInstance = this; } 59 else 59 else 60 { G4Exception("G4TheMTRayTracer::G4TheMTRayT 60 { G4Exception("G4TheMTRayTracer::G4TheMTRayTracer","VisRayTracer00100", 61 FatalException,"G4TheMTRayTracer has 61 FatalException,"G4TheMTRayTracer has to be a singleton.");} 62 theUserWorkerInitialization = 0; 62 theUserWorkerInitialization = 0; 63 theRTWorkerInitialization = 0; 63 theRTWorkerInitialization = 0; 64 theUserRunAction = 0; 64 theUserRunAction = 0; 65 theRTRunAction = 0; 65 theRTRunAction = 0; 66 } 66 } 67 67 68 G4TheMTRayTracer* G4TheMTRayTracer::Instance() 68 G4TheMTRayTracer* G4TheMTRayTracer::Instance() 69 { 69 { 70 if (theInstance) return theInstance; 70 if (theInstance) return theInstance; 71 else return new G4TheMTRayTracer; 71 else return new G4TheMTRayTracer; 72 } 72 } 73 73 74 G4TheMTRayTracer* G4TheMTRayTracer::Instance 74 G4TheMTRayTracer* G4TheMTRayTracer::Instance 75 (G4VFigureFileMaker* figMaker,G4VRTScanner* sc 75 (G4VFigureFileMaker* figMaker,G4VRTScanner* scanner) 76 { 76 { 77 if (theInstance) { 77 if (theInstance) { 78 theFigMaker=figMaker; 78 theFigMaker=figMaker; 79 theScanner=scanner; 79 theScanner=scanner; 80 return theInstance; 80 return theInstance; 81 } 81 } 82 else return new G4TheMTRayTracer(figMaker,sc 82 else return new G4TheMTRayTracer(figMaker,scanner); 83 } 83 } 84 84 85 G4TheMTRayTracer::~G4TheMTRayTracer() 85 G4TheMTRayTracer::~G4TheMTRayTracer() 86 { 86 { 87 if(theRTWorkerInitialization) 87 if(theRTWorkerInitialization) 88 { 88 { 89 delete theRTWorkerInitialization; 89 delete theRTWorkerInitialization; 90 theRTWorkerInitialization = 0; 90 theRTWorkerInitialization = 0; 91 } 91 } 92 if(theRTRunAction) 92 if(theRTRunAction) 93 { 93 { 94 delete theRTRunAction; 94 delete theRTRunAction; 95 theRTRunAction = 0; 95 theRTRunAction = 0; 96 } 96 } 97 } 97 } 98 98 99 void G4TheMTRayTracer::Trace(const G4String& f 99 void G4TheMTRayTracer::Trace(const G4String& fileName) 100 { 100 { 101 G4StateManager* theStateMan = G4StateManager 101 G4StateManager* theStateMan = G4StateManager::GetStateManager(); 102 G4ApplicationState currentState = theStateMa 102 G4ApplicationState currentState = theStateMan->GetCurrentState(); 103 if(currentState!=G4State_Idle) 103 if(currentState!=G4State_Idle) 104 { 104 { 105 G4warn << "Illegal application state <" << 105 G4warn << "Illegal application state <" << theStateMan->GetStateString(currentState) 106 << "> - Trace() ignored. " << G4end 106 << "> - Trace() ignored. " << G4endl; 107 return; 107 return; 108 } 108 } 109 109 110 if(!theFigMaker) 110 if(!theFigMaker) 111 { 111 { 112 G4warn << "Figure file maker class is not 112 G4warn << "Figure file maker class is not specified - Trace() ignored." << G4endl; 113 return; 113 return; 114 } 114 } 115 115 116 G4UImanager* UI = G4UImanager::GetUIpointer( 116 G4UImanager* UI = G4UImanager::GetUIpointer(); 117 G4int storeTrajectory = UI->GetCurrentIntVal 117 G4int storeTrajectory = UI->GetCurrentIntValue("/tracking/storeTrajectory"); 118 UI->ApplyCommand("/tracking/storeTrajectory 118 UI->ApplyCommand("/tracking/storeTrajectory 1"); 119 119 120 G4ThreeVector tmpVec = targetPosition - eyeP 120 G4ThreeVector tmpVec = targetPosition - eyePosition; 121 eyeDirection = tmpVec.unit(); 121 eyeDirection = tmpVec.unit(); 122 G4int nPixel = nColumn*nRow; 122 G4int nPixel = nColumn*nRow; 123 colorR = new unsigned char[nPixel]; 123 colorR = new unsigned char[nPixel]; 124 colorG = new unsigned char[nPixel]; 124 colorG = new unsigned char[nPixel]; 125 colorB = new unsigned char[nPixel]; 125 colorB = new unsigned char[nPixel]; 126 unsigned char defR = (unsigned char)(G4int(2 126 unsigned char defR = (unsigned char)(G4int(255*backgroundColour.GetRed())); 127 unsigned char defG = (unsigned char)(G4int(2 127 unsigned char defG = (unsigned char)(G4int(255*backgroundColour.GetGreen())); 128 unsigned char defB = (unsigned char)(G4int(2 128 unsigned char defB = (unsigned char)(G4int(255*backgroundColour.GetBlue())); 129 for(G4int ii=0;ii<nPixel;++ii) 129 for(G4int ii=0;ii<nPixel;++ii) 130 { 130 { 131 colorR[ii] = defR; 131 colorR[ii] = defR; 132 colorG[ii] = defG; 132 colorG[ii] = defG; 133 colorB[ii] = defB; 133 colorB[ii] = defB; 134 } 134 } 135 135 136 G4bool succeeded = CreateBitMap(); 136 G4bool succeeded = CreateBitMap(); 137 if(succeeded) 137 if(succeeded) 138 { CreateFigureFile(fileName); } 138 { CreateFigureFile(fileName); } 139 else 139 else 140 { G4warn << "Could not create figure file" < 140 { G4warn << "Could not create figure file" << G4endl; 141 G4warn << "You might set the eye position 141 G4warn << "You might set the eye position outside of the world volume" << G4endl; } 142 142 143 G4String str = "/tracking/storeTrajectory " 143 G4String str = "/tracking/storeTrajectory " + G4UIcommand::ConvertToString(storeTrajectory); 144 UI->ApplyCommand(str); 144 UI->ApplyCommand(str); 145 145 146 delete [] colorR; 146 delete [] colorR; 147 delete [] colorG; 147 delete [] colorG; 148 delete [] colorB; 148 delete [] colorB; 149 } 149 } 150 150 151 void G4TheMTRayTracer::StoreUserActions() 151 void G4TheMTRayTracer::StoreUserActions() 152 { 152 { 153 G4MTRunManager* mrm = G4RunManagerFa 153 G4MTRunManager* mrm = G4RunManagerFactory::GetMTMasterRunManager(); 154 theUserWorkerInitialization = mrm->GetUserWo 154 theUserWorkerInitialization = mrm->GetUserWorkerInitialization(); 155 theUserRunAction = mrm->GetUserRunAction(); 155 theUserRunAction = mrm->GetUserRunAction(); 156 156 157 if(!theRTWorkerInitialization) theRTWorkerIn 157 if(!theRTWorkerInitialization) theRTWorkerInitialization = new G4RTWorkerInitialization(); 158 if(!theRTRunAction) theRTRunAction = new G4R 158 if(!theRTRunAction) theRTRunAction = new G4RTRunAction(); 159 159 160 mrm->SetUserInitialization(theRTWorkerInitia 160 mrm->SetUserInitialization(theRTWorkerInitialization); 161 mrm->SetUserAction(theRTRunAction); 161 mrm->SetUserAction(theRTRunAction); 162 } 162 } 163 163 164 void G4TheMTRayTracer::RestoreUserActions() 164 void G4TheMTRayTracer::RestoreUserActions() 165 { 165 { 166 G4MTRunManager* mrm = G4RunManagerFactory::G 166 G4MTRunManager* mrm = G4RunManagerFactory::GetMTMasterRunManager(); 167 mrm->SetUserInitialization( 167 mrm->SetUserInitialization( 168 const_cast<G4UserWorkerInitialization*>(th 168 const_cast<G4UserWorkerInitialization*>(theUserWorkerInitialization)); 169 mrm->SetUserAction(const_cast<G4UserRunActio 169 mrm->SetUserAction(const_cast<G4UserRunAction*>(theUserRunAction)); 170 } 170 } 171 171 172 G4bool G4TheMTRayTracer::CreateBitMap() 172 G4bool G4TheMTRayTracer::CreateBitMap() 173 { 173 { 174 G4VVisManager* visMan = G4VVisManager::GetCo 174 G4VVisManager* visMan = G4VVisManager::GetConcreteInstance(); 175 visMan->IgnoreStateChanges(true); 175 visMan->IgnoreStateChanges(true); 176 StoreUserActions(); 176 StoreUserActions(); 177 177 178 G4MTRunManager* mrm = G4RunManagerFactory::G 178 G4MTRunManager* mrm = G4RunManagerFactory::GetMTMasterRunManager(); 179 179 180 // Keep, then switch off any printing reques 180 // Keep, then switch off any printing requests 181 auto runVerbosity = mrm->GetVerboseLeve 181 auto runVerbosity = mrm->GetVerboseLevel(); 182 auto runPrintProgress = mrm->GetPrintProgre 182 auto runPrintProgress = mrm->GetPrintProgress(); 183 G4UImanager::GetUIpointer()->ApplyCommand("/ 183 G4UImanager::GetUIpointer()->ApplyCommand("/run/verbose 0"); 184 G4UImanager::GetUIpointer()->ApplyCommand("/ 184 G4UImanager::GetUIpointer()->ApplyCommand("/run/printProgress 0"); 185 185 186 // Event loop 186 // Event loop 187 G4int nEvent = nRow*nColumn; 187 G4int nEvent = nRow*nColumn; 188 //// mrm->BeamOn(nEvent); 188 //// mrm->BeamOn(nEvent); 189 //// Temporary work-around until direct invok 189 //// Temporary work-around until direct invokation of G4RunManager::BeamOn() works. 190 G4String str = "/run/beamOn " + G4UIcommand: 190 G4String str = "/run/beamOn " + G4UIcommand::ConvertToString(nEvent); 191 G4UImanager::GetUIpointer()->ApplyCommand(st 191 G4UImanager::GetUIpointer()->ApplyCommand(str); 192 192 193 // Restore printing requests 193 // Restore printing requests 194 str = "/run/verbose " + G4UIcommand::Convert 194 str = "/run/verbose " + G4UIcommand::ConvertToString(runVerbosity); 195 G4UImanager::GetUIpointer()->ApplyCommand(st 195 G4UImanager::GetUIpointer()->ApplyCommand(str); 196 str = "/run/printProgress " + G4UIcommand::C 196 str = "/run/printProgress " + G4UIcommand::ConvertToString(runPrintProgress); 197 G4UImanager::GetUIpointer()->ApplyCommand(st 197 G4UImanager::GetUIpointer()->ApplyCommand(str); 198 198 199 RestoreUserActions(); 199 RestoreUserActions(); 200 visMan->IgnoreStateChanges(false); 200 visMan->IgnoreStateChanges(false); 201 201 202 const G4RTRun* theRun = static_cast<const G4 202 const G4RTRun* theRun = static_cast<const G4RTRun*>(mrm->GetCurrentRun()); 203 if(!theRun) return false; 203 if(!theRun) return false; 204 204 205 G4THitsMap<G4Colour>* colMap = theRun->GetMa 205 G4THitsMap<G4Colour>* colMap = theRun->GetMap(); 206 auto itr = colMap->GetMap()->cbegin(); 206 auto itr = colMap->GetMap()->cbegin(); 207 for(;itr!=colMap->GetMap()->cend();++itr) 207 for(;itr!=colMap->GetMap()->cend();++itr) 208 { 208 { 209 G4int key = itr->first; 209 G4int key = itr->first; 210 G4Colour* col = itr->second; 210 G4Colour* col = itr->second; 211 colorR[key] = (unsigned char)(G4int(255*co 211 colorR[key] = (unsigned char)(G4int(255*col->GetRed())); 212 colorG[key] = (unsigned char)(G4int(255*co 212 colorG[key] = (unsigned char)(G4int(255*col->GetGreen())); 213 colorB[key] = (unsigned char)(G4int(255*co 213 colorB[key] = (unsigned char)(G4int(255*col->GetBlue())); 214 } 214 } 215 215 216 theScanner->Initialize(nRow,nColumn); 216 theScanner->Initialize(nRow,nColumn); 217 G4int iRow, iColumn; 217 G4int iRow, iColumn; 218 while (theScanner->Coords(iRow,iColumn)) 218 while (theScanner->Coords(iRow,iColumn)) 219 { 219 { 220 G4int iCoord = iRow * nColumn + iColumn; 220 G4int iCoord = iRow * nColumn + iColumn; 221 theScanner->Draw(colorR[iCoord],colorG[iCo 221 theScanner->Draw(colorR[iCoord],colorG[iCoord],colorB[iCoord]); 222 } 222 } 223 223 224 return true; 224 return true; 225 } 225 } 226 226 227 227