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 // Please cite the following paper if you use << 26 // ------------------------------------------------------------------- 27 // Nucl.Instrum.Meth.B260:20-27, 2007 << 27 // $Id: RunAction.cc,v 1.3 2008/04/10 12:02:57 sincerti Exp $ 28 << 28 // ------------------------------------------------------------------- 29 // #define MATRIX_BOUND_CHECK << 29 >> 30 #include "G4SteppingManager.hh" >> 31 #include "G4Run.hh" >> 32 #include "G4Material.hh" >> 33 #include "G4UImanager.hh" >> 34 #include "G4ios.hh" >> 35 #include "G4UnitsTable.hh" >> 36 >> 37 #include "Randomize.hh" >> 38 #include <iomanip> >> 39 #include <assert.h> 30 40 31 #include "RunAction.hh" 41 #include "RunAction.hh" 32 #include "G4AnalysisManager.hh" << 33 #include "G4AutoLock.hh" << 34 << 35 namespace << 36 { << 37 G4Mutex aMutex = G4MUTEX_INITIALIZER; << 38 } << 39 42 >> 43 // MATRIX >> 44 #define MATRIX_BOUND_CHECK >> 45 #include "globals.hh" >> 46 #include <iostream> >> 47 #include "CLHEP/Matrix/Matrix.h" >> 48 #include "CLHEP/Matrix/Vector.h" >> 49 #include <fstream> >> 50 #include "G4ios.hh" >> 51 #include <fstream> >> 52 #include <vector> >> 53 #include <cmath> 40 using namespace std; 54 using namespace std; 41 55 42 //....oooOO0OOooo........oooOO0OOooo........oo 56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 43 57 44 RunAction::RunAction(DetectorConstruction* det << 58 RunAction::RunAction(DetectorConstruction* det) 45 :fDetector(det),fPrimary(pri) << 59 :detector(det) 46 {} << 60 { >> 61 } 47 62 48 //....oooOO0OOooo........oooOO0OOooo........oo 63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 49 64 50 RunAction::~RunAction() 65 RunAction::~RunAction() 51 {} 66 {} 52 67 53 //....oooOO0OOooo........oooOO0OOooo........oo 68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 54 69 55 void RunAction::BeginOfRunAction(const G4Run* 70 void RunAction::BeginOfRunAction(const G4Run* /*aRun*/) 56 { 71 { 57 // Vector initialization << 72 // Cleaning result files 58 << 73 system ("rm -rf ./results/x.txt"); 59 fRow=0; << 74 system ("rm -rf ./results/y.txt"); 60 << 75 system ("rm -rf ./results/theta.txt"); 61 fXVector = CLHEP::HepVector(32); << 76 system ("rm -rf ./results/phi.txt"); 62 fYVector = CLHEP::HepVector(32); << 77 system ("rm -rf ./results/image.txt"); 63 fThetaVector = CLHEP::HepVector(32); << 78 system ("rm -rf ./results/matrix.txt"); 64 fPhiVector = CLHEP::HepVector(32); << 79 system ("rm -rf ./results/profile.txt"); 65 << 80 system ("rm -rf ./results/grid.txt"); 66 // Histograms << 67 << 68 // Get/create analysis manager << 69 G4cout << "##### Create analysis manager " << 70 << 71 G4AnalysisManager* man = G4AnalysisManager: << 72 man->SetDefaultFileType("root"); << 73 << 74 G4cout << "Using " << man->GetType() << " a << 75 << 76 // Open an output file << 77 man->OpenFile("nanobeam"); << 78 man->SetFirstHistoId(1); << 79 man->SetFirstNtupleId(1); << 80 << 81 // Create 1st ntuple (id = 1) << 82 man->CreateNtuple("ntuple0", "BeamProfile") << 83 man->CreateNtupleDColumn("xIn"); << 84 man->CreateNtupleDColumn("yIn"); << 85 man->CreateNtupleDColumn("zIn"); << 86 man->FinishNtuple(); << 87 G4cout << "Ntuple-1 created" << G4endl; << 88 << 89 // Create 2nd htuple (id = 2) << 90 man->CreateNtuple("ntuple1","Grid"); << 91 man->CreateNtupleDColumn("xIn"); << 92 man->CreateNtupleDColumn("yIn"); << 93 man->CreateNtupleDColumn("e"); << 94 man->FinishNtuple(); << 95 G4cout << "Ntuple-2 created" << G4endl; << 96 << 97 // Create 3rd ntuple (id = 3) << 98 man->CreateNtuple("ntuple2","Coef"); << 99 man->CreateNtupleDColumn("xIn"); << 100 man->CreateNtupleDColumn("yIn"); << 101 man->CreateNtupleDColumn("thetaIn"); << 102 man->CreateNtupleDColumn("phiIn"); << 103 man->FinishNtuple(); << 104 G4cout << "Ntuple-3 created" << G4endl; << 105 << 106 return; << 107 << 108 } 81 } 109 82 110 //....oooOO0OOooo........oooOO0OOooo........oo 83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 111 84 112 void RunAction::EndOfRunAction(const G4Run* /* 85 void RunAction::EndOfRunAction(const G4Run* /*aRun*/) 113 { 86 { 114 87 115 if (fDetector->GetCoef()==1) << 88 if (detector->GetCoef()==1) 116 { 89 { 117 // 17/12/2013 - thanks to A. Dotti << 90 118 // CLHEP Matrix inversion (as for CLHEP vers << 91 CLHEP::HepMatrix m,s; 119 // is not thread safe, we need to prot << 92 m = CLHEP::HepMatrix(32,32); 120 // Since this is not performance criti << 93 s = CLHEP::HepMatrix(32,32); 121 // all this part. << 94 CLHEP::HepVector v; 122 G4AutoLock l(&aMutex); << 95 v = CLHEP::HepVector(32); 123 // << 96 124 << 97 float aa,ab,ac,ad,ae,af,ag,ah,ai,aj; 125 CLHEP::HepMatrix m; << 98 float ba,bb,bc,bd,be,bf,bg,bh,bi,bj; >> 99 float ca,cb,cc,cd,ce,cf,cg,ch,ci,cj; >> 100 float da,db; >> 101 float vv; >> 102 >> 103 // MATRIX READING >> 104 >> 105 int ncols; >> 106 int nlines=1; >> 107 >> 108 FILE * fp1 = fopen("results/matrix.txt","r"); >> 109 while (1) >> 110 { >> 111 ncols = fscanf(fp1, >> 112 "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", >> 113 &aa,&ab,&ac,&ad,&ae,&af,&ag,&ah,&ai,&aj, >> 114 &ba,&bb,&bc,&bd,&be,&bf,&bg,&bh,&bi,&bj, >> 115 &ca,&cb,&cc,&cd,&ce,&cf,&cg,&ch,&ci,&cj, >> 116 &da,&db >> 117 ); >> 118 >> 119 if (ncols<0) break; >> 120 if (nlines>32) G4Exception("Try to read more than 32 lines in matrix file !"); >> 121 >> 122 m(nlines,1)=aa; >> 123 m(nlines,2)=ab; >> 124 m(nlines,3)=ac; >> 125 m(nlines,4)=ad; >> 126 m(nlines,5)=ae; >> 127 m(nlines,6)=af; >> 128 m(nlines,7)=ag; >> 129 m(nlines,8)=ah; >> 130 m(nlines,9)=ai; >> 131 m(nlines,10)=aj; >> 132 m(nlines,11)=ba; >> 133 m(nlines,12)=bb; >> 134 m(nlines,13)=bc; >> 135 m(nlines,14)=bd; >> 136 m(nlines,15)=be; >> 137 m(nlines,16)=bf; >> 138 m(nlines,17)=bg; >> 139 m(nlines,18)=bh; >> 140 m(nlines,19)=bi; >> 141 m(nlines,20)=bj; >> 142 m(nlines,21)=ca; >> 143 m(nlines,22)=cb; >> 144 m(nlines,23)=cc; >> 145 m(nlines,24)=cd; >> 146 m(nlines,25)=ce; >> 147 m(nlines,26)=cf; >> 148 m(nlines,27)=cg; >> 149 m(nlines,28)=ch; >> 150 m(nlines,29)=ci; >> 151 m(nlines,30)=cj; >> 152 m(nlines,31)=da; >> 153 m(nlines,32)=db; >> 154 >> 155 nlines++; 126 156 127 // VECTOR READING << 157 } >> 158 fclose(fp1); >> 159 128 // VECTOR READING 160 // VECTOR READING 129 161 130 m = CLHEP::HepMatrix(32,32); << 131 m = fPrimary->GetMatrix(); << 132 162 133 G4cout << G4endl; 163 G4cout << G4endl; 134 G4cout << "===> NANOBEAM LINE INTRINSIC ABER 164 G4cout << "===> NANOBEAM LINE INTRINSIC ABERRATION COEFFICIENTS (units of micrometer and mrad) :" << G4endl; 135 G4cout << G4endl; 165 G4cout << G4endl; 136 166 137 int inv; 167 int inv; >> 168 nlines = 1; >> 169 FILE * fp2 = fopen("results/x.txt","r"); >> 170 while (1) >> 171 { >> 172 ncols = fscanf(fp2,"%f", &vv); >> 173 if (ncols<0) break; >> 174 v(nlines)=vv; >> 175 nlines++; >> 176 } >> 177 fclose(fp2); 138 178 139 m.invert(inv); 179 m.invert(inv); 140 CLHEP::HepVector tmp(32,0); << 180 CLHEP::HepVector seb(32,0); 141 tmp=m*fXVector; << 181 seb=m*v; 142 CLHEP::HepVector b; 182 CLHEP::HepVector b; 143 b=tmp.sub(2,2); G4cout << "<x|theta>=" << << 183 b=seb.sub(2,2); G4cout << "<x|theta>=" << b << G4endl; 144 b=tmp.sub(8,8); G4cout << "<x|theta*delta> << 184 b=seb.sub(8,8); G4cout << "<x|theta*delta>=" << b << G4endl; 145 b=tmp.sub(10,10); G4cout << "<x|theta^3>=" < << 185 b=seb.sub(10,10); G4cout << "<x|theta^3>=" << b << G4endl; 146 b=tmp.sub(12,12); G4cout << "<x|theta*phi^2> << 186 b=seb.sub(12,12); G4cout << "<x|theta*phi^2>=" << b << G4endl; 147 m.invert(inv); << 187 m.invert(inv); 148 << 188 149 m.invert(inv); << 189 nlines = 1; 150 tmp = m*fThetaVector; << 190 FILE * fp3 = fopen("results/theta.txt","r"); 151 m.invert(inv); << 191 while (1) 152 b=tmp.sub(2,2); G4cout << "<x|x>=" << b << G << 192 { 153 << 193 ncols = fscanf(fp3,"%f", &vv); >> 194 if (ncols<0) break; >> 195 v(nlines)=vv; >> 196 nlines++; >> 197 } >> 198 fclose(fp3); >> 199 >> 200 m.invert(inv); >> 201 seb = m*v; >> 202 m.invert(inv); >> 203 b=seb.sub(2,2); G4cout << "<x|x>=" << b << G4endl; >> 204 >> 205 nlines = 1; >> 206 FILE * fp4 = fopen("results/y.txt","r"); >> 207 while (1) >> 208 { >> 209 ncols = fscanf(fp4,"%f", &vv); >> 210 if (ncols<0) break; >> 211 v(nlines)=vv; >> 212 nlines++; >> 213 } >> 214 fclose(fp4); >> 215 m.invert(inv); >> 216 seb=m*v; >> 217 b=seb.sub(3,3); G4cout << "<y|phi>=" << b << G4endl; >> 218 b=seb.sub(9,9); G4cout << "<y|phi*delta>=" << b << G4endl; >> 219 b=seb.sub(11,11); G4cout << "<y|theta^2*phi>=" << b << G4endl; >> 220 b=seb.sub(13,13); G4cout << "<y|phi^3>=" << b << G4endl; >> 221 m.invert(inv); >> 222 >> 223 nlines = 1; >> 224 FILE * fp5 = fopen("results/phi.txt","r"); >> 225 while (1) >> 226 { >> 227 ncols = fscanf(fp5,"%f", &vv); >> 228 if (ncols<0) break; >> 229 v(nlines)=vv; >> 230 nlines++; >> 231 } >> 232 fclose(fp5); 154 m.invert(inv); 233 m.invert(inv); 155 tmp=m*fYVector; << 234 seb = m*v; 156 b=tmp.sub(3,3); G4cout << "<y|phi>=" << b << 157 b=tmp.sub(9,9); G4cout << "<y|phi*delta>=" << 158 b=tmp.sub(11,11); G4cout << "<y|theta^2*phi> << 159 b=tmp.sub(13,13); G4cout << "<y|phi^3>=" << << 160 m.invert(inv); 235 m.invert(inv); 161 << 236 b=seb.sub(3,3); G4cout << "<y|y>=" << b << G4endl; 162 m.invert(inv); << 163 tmp = m*fPhiVector; << 164 m.invert(inv); << 165 b=tmp.sub(3,3); G4cout << "<y|y>=" << b << G << 166 237 167 } 238 } 168 239 169 // Save histograms << 170 << 171 G4AnalysisManager* man = G4AnalysisManager::I << 172 man->Write(); << 173 man->CloseFile(); << 174 << 175 // Complete clean-up << 176 man->Clear(); << 177 } 240 } >> 241 >> 242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 178 243