Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TrajectoryChargeFilter.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 //
26 // $Id: G4TrajectoryChargeFilter.cc 66373 2012-12-18 09:41:34Z gcosmo $
27 //
28 // Filter trajectories according to charge. Only registered
29 // charges will pass the filter.
30 //
31 // Jane Tinslay May 2006
32 //
34 #include <sstream>
35 
38 {}
39 
41 
42 bool
44 {
45  G4double charge = traj.GetCharge();
46 
47  if (GetVerbose()) G4cout<<"G4TrajectoryChargeFilter processing trajectory with charge: "<<charge<<G4endl;
48 
49  MyCharge myCharge;
50 
51  if(charge>0.) myCharge = Positive;
52  else if(charge<0.) myCharge = Negative;
53  else myCharge = Neutral;
54 
55  std::vector<MyCharge>::const_iterator iter = std::find(fCharges.begin(), fCharges.end(), myCharge);
56 
57  // Fail if charge not registered
58  if (iter == fCharges.end()) return false;
59 
60  return true;
61 }
62 
63 void
65 {
66  MyCharge myCharge;
67 
68  if (!ConvertToCharge(charge, myCharge)) {
70  ed << "Invalid charge "<<charge;
72  ("G4TrajectoryChargeFilter::Add(const G4String& charge)",
73  "modeling0115", JustWarning, ed);
74  return;
75  }
76 
77  return Add(myCharge);
78 }
79 
80 void
81 G4TrajectoryChargeFilter::Add(const MyCharge& charge)
82 {
83  fCharges.push_back(charge);
84 }
85 
86 void
87 G4TrajectoryChargeFilter::Print(std::ostream& ostr) const
88 {
89  ostr<<"Charges registered: "<<G4endl;
90  std::vector<MyCharge>::const_iterator iter = fCharges.begin();
91 
92  while (iter != fCharges.end()) {
93  ostr<<*iter<<G4endl;
94  iter++;
95  }
96 }
97 
98 void
100 {
101  // Clear registered charge vector
102  fCharges.clear();
103 }
104 
105 G4bool
106 G4TrajectoryChargeFilter::ConvertToCharge(const G4String& string, MyCharge& myCharge)
107 {
108  bool result(true);
109 
110  G4int charge;
111  std::istringstream is(string.c_str());
112  is >> charge;
113 
114  switch (charge) {
115  case 1:
116  myCharge = G4TrajectoryChargeFilter::Positive;
117  break;
118  case 0:
119  myCharge = G4TrajectoryChargeFilter::Neutral;
120  break;
121  case -1:
122  myCharge = G4TrajectoryChargeFilter::Negative;
123  break;
124  default:
125  result = false;
126  }
127 
128  return result;
129 }
virtual void Print(std::ostream &ostr) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4TrajectoryChargeFilter(const G4String &name="Unspecified")
const XML_Char * name
int G4int
Definition: G4Types.hh:78
virtual bool Evaluate(const G4VTrajectory &) const
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
virtual G4double GetCharge() const =0
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void Add(const G4String &particle)
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76