Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OlapEventAction.cc
Go to the documentation of this file.
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 //
28 //
29 //
30 // $Id$
31 //
32 //
33 // --------------------------------------------------------------
34 // OlapEventAction
35 //
36 // Author: Martin Liendl - Martin.Liendl@cern.ch
37 //
38 // --------------------------------------------------------------
39 //
40 #include "globals.hh"
41 #include <vector>
42 #include <sstream>
43 
44 #include "G4VVisManager.hh"
45 #include "G4Trajectory.hh"
46 #include "G4TrajectoryContainer.hh"
47 #include "G4UImanager.hh"
48 #include "G4Event.hh"
49 #include "G4RunManager.hh"
50 #include "G4Run.hh"
51 
52 #include "OlapManager.hh"
53 #include "OlapEventAction.hh"
54 #include "OlapRunAction.hh"
55 #include "OlapDetConstr.hh"
56 #include "OlapGenerator.hh"
57 
58 //#define OLAP_DEBUG
59 
60 std::ostream &
61  operator<<(std::ostream& flux, OlapInfo & oi)
62 {
63  OlapStepInfo si;
64  si.thePoint = oi.v1;
65  si.theHist = oi.hist1;
66  flux << "vol 1: point=" << oi.v1 << " vol 2: point=" << oi.v2
67  << " axis=" << oi.axis << " [" << oi.probNot << "] "
68  << oi.info << G4endl;
69  if (oi.stAB.size())
70  {
71  flux << oi.stAB << G4endl << G4endl;
72  flux << oi.stBA << G4endl;
73  }
74  flux << "A -> B:" << G4endl;
75  flux << si << G4endl;
76  flux << "B -> A:" << G4endl;
77  si.thePoint=oi.v2;
78  si.theHist=oi.hist2;
79  flux << si << G4endl;
80  return flux;
81 }
82 
83 
85 {
86  while( !stAB.empty() )
87  {
88  delete stAB.back();
89  stAB.pop_back();
90  }
91 
92  while( !stBA.empty() )
93  {
94  delete stBA.back();
95  stBA.pop_back();
96  }
97 
98 }
99 
101 {
102  G4bool result = false;
103  if (
104  (
105  ( hist1.GetTopVolume() == rh.hist1.GetTopVolume() ) &&
106  ( hist2.GetTopVolume() == rh.hist2.GetTopVolume() )
107  )
108  ||
109  (
110  ( hist1.GetTopVolume() == rh.hist2.GetTopVolume() ) &&
111  ( hist2.GetTopVolume() == rh.hist1.GetTopVolume() )
112  )
113  )
114  result = true;
115  return result;
116 }
117 
118 
120  : theRunAction(aRunAction), dontDelete(false)
121 {
122  const G4VUserDetectorConstruction * aDet =
124  const OlapDetConstr * aConstDet =
125  dynamic_cast<const OlapDetConstr *>(aDet);
126  if (!aDet)
127  {
128  G4Exception("OlapEventAction::OlapEventAction()",
129  "InvalidSetup", FatalException,
130  "Can't be used together with an OlapDetConstr! Exiting...");
131  }
132  theDet = const_cast<OlapDetConstr *>(aConstDet);
133 }
134 
135 
137 {
138 }
139 
140 
142 {
143  while (!ABSteps.empty())
144  {
145  if (! dontDelete )
146  delete ABSteps.back();
147  ABSteps.pop_back();
148  }
149 
150  while (!BASteps.empty())
151  {
152  if (! dontDelete )
153  delete BASteps.back();
154  BASteps.pop_back();
155  }
156 
157  dontDelete=false;
158 }
159 
160 
162 {
163 
164  const OlapGenerator * aGen = dynamic_cast<const OlapGenerator *>
166  OlapGenerator * aGenerator=0;
167  if ( aGen )
168  {
169  aGenerator = const_cast<OlapGenerator*>(aGen);
170  }
171  else
172  {
173  G4cout << "Warning: Primary generator is not OlapGenerator!" << G4endl
174  << " Overlap Detection will not work!" << G4endl;
175  return;
176  }
177 
178  if (! (anEvent->GetEventID() % 1000))
179  {
180  G4cout << "Event=" << anEvent->GetEventID() << " : "
182  << G4endl;
183  }
184 
185  G4int axx = -1;
186  if (aGenerator)
187  axx = aGenerator->GetAxis();
188 
189  G4double tolerance = OlapManager::GetOlapManager()->Delta();
190 
191  if ( (ABSteps[ABSteps.size()-1])->theHist.GetTopVolume()->GetLogicalVolume() !=
193  (BASteps[BASteps.size()-1])->theHist.GetTopVolume()->GetLogicalVolume() !=
195  )
196  {
197  OlapInfo * oi =
198  new OlapInfo( ABSteps[ABSteps.size()-1]->theHist,
199  BASteps[BASteps.size()-1]->theHist,
200  ABSteps[ABSteps.size()-1]->thePoint,
201  BASteps[BASteps.size()-1]->thePoint,
202  axx,
204  oi->info = "daughter protrudes mother";
205  oi->probNot = false; // it's an overlap
206  oi->stAB = ABSteps;
207  oi->stBA = BASteps;
208  dontDelete = true;
209 
210  if ( !InsertOI(oi) )
211  { // already detected!
212  delete oi;
213  }
214  return;
215  }
216 
217  // just in case, not the OlapGenerator is used but
218  // another particle generator
219  if (!BASteps.size())
220  return;
221 
222  // do we have as many steps from AB as from BA?
223  if (ABSteps.size() != BASteps.size())
224  {
225  OlapInfo * oi =
226  new OlapInfo( ABSteps[1]->theHist,
227  BASteps[1]->theHist,
228  ABSteps[1]->thePoint,
229  BASteps[1]->thePoint,
230  axx,
231  OlapManager::GetOlapManager()->GetOriginalWorld() );
232  // copy all steps and prohibit deletion of their pointers ...
233  oi->stAB = ABSteps;
234  oi->stBA = BASteps;
235  oi->probNot = true; // probably not an overlap, just numerics ....
236  dontDelete = true;
237 
238  std::ostringstream os;
239  os << "A,B diff. steps AB:" << ABSteps.size()
240  << " BA:" << BASteps.size() << '\0';
241 
242  oi->info = G4String(os.str());
243  if ( !InsertOI(oi) )
244  {
245  delete oi;
246  }
247  return;
248  }
249 
250  // now forget about the first and the last points of AB and BA &
251  // compare the remaining points of AB with the remaining points
252  // of BA where the points of BA are in descending order while
253  // the points of BA are in ascending order
254  G4int siz = ABSteps.size();
255  G4int j = siz-3;
256  for (G4int i = 1; i<(siz-1); i++)
257  {
258  G4double delta =
259  (ABSteps[i]->thePoint - BASteps[j]->thePoint).mag() ;
260  if (delta>tolerance)
261  {
262  OlapInfo * oi =
263  new OlapInfo( ABSteps[i]->theHist,
264  BASteps[j]->theHist,
265  ABSteps[i]->thePoint,
266  BASteps[j]->thePoint,
267  axx,
268  OlapManager::GetOlapManager()->GetOriginalWorld() );
269  if ( !InsertOI(oi) )
270  { // already detected!
271  delete oi;
272  }
273  }
274  j--;
275  }
276 }
277 
279 {
280  std::vector<OlapInfo*>::iterator it = theRunAction->theOlaps.begin();
281  G4bool flag(false);
282  while (it != theRunAction->theOlaps.end())
283  {
284  if (*oi== *(*it)) { // already detected overlap
285  flag=true;
286  if ((*it)->probNot != oi->probNot )
287  { // probably a real overlap, not numerics
288  flag=false;
289  (*it)->probNot = false;
290  oi->probNot = false;
291  }
292  break;
293  }
294  it++;
295  }
296 
297  if (flag)
298  {
299  return false; // already detected overlap
300  }
301 
302  theRunAction->theOlaps.push_back(oi);
303 
304  #ifdef OLAP_DEBUG
305  G4cout << "===================================" << G4endl;
306  G4cout << "No of detected Overlaps: " << theRunAction->theOlaps.size()
307  << G4endl;
308  G4cout << "===================================" << G4endl;
309  G4cout << oi->hist1 << G4endl;
310  G4cout << "CpNo: " << oi->hist1.GetTopVolume()->GetCopyNo() << G4endl;
311  G4cout << "Pos: " << oi->hist1.GetTopTransform().NetTranslation()
312  << G4endl;
313  G4cout << "Rot: " << oi->hist1.GetTopTransform().NetRotation().phiX()
314  << " " << oi->hist1.GetTopTransform().NetRotation().phiY() << " "
315  << oi->hist1.GetTopTransform().NetRotation().phiZ() << " "
316  << oi->hist1.GetTopTransform().NetRotation().thetaX() << " "
317  << oi->hist1.GetTopTransform().NetRotation().thetaY() << " "
319  << G4endl ;
320  G4cout << oi->v1 << G4endl << "----" << G4endl;
321 
322  G4cout << oi->hist2 << G4endl;
323  G4cout << "CpNo: " << oi->hist2.GetTopVolume()->GetCopyNo() << G4endl;
324  G4cout << "Pos: " << oi->hist2.GetTopTransform().NetTranslation()
325  << G4endl;
326  G4cout << "Rot: " << oi->hist2.GetTopTransform().NetRotation().phiX()
327  << " " << oi->hist2.GetTopTransform().NetRotation().phiY() << " "
328  << oi->hist2.GetTopTransform().NetRotation().phiZ() << " "
329  << oi->hist2.GetTopTransform().NetRotation().thetaX() << " "
330  << oi->hist2.GetTopTransform().NetRotation().thetaY() << " "
332  << G4endl ;
333  G4cout << oi->v2 << G4endl << "----" << G4endl;
334 
335  #endif
336 
337  static char * c = getenv("OLAP_LOG_EVENT");
338  if (c)
339  G4cerr << *oi << G4endl;
340 
341  return true; // new overlap detected!
342 }