source: trunk/source/visualization/modeling/src/G4TrajectoryChargeFilter.cc @ 954

Last change on this file since 954 was 954, checked in by garnier, 15 years ago

remise a jour

File size: 3.9 KB
Line 
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,v 1.4 2006/08/25 19:44:14 tinslay Exp $
27// GEANT4 tag $Name:  $
28//
29// Filter trajectories according to charge. Only registered
30// charges will pass the filter.
31//
32// Jane Tinslay May 2006
33//
34#include "G4TrajectoryChargeFilter.hh"
35#include <sstream>
36
37G4TrajectoryChargeFilter::G4TrajectoryChargeFilter(const G4String& name)
38  :G4SmartFilter<G4VTrajectory>(name)
39{}
40
41G4TrajectoryChargeFilter::~G4TrajectoryChargeFilter() {}
42
43bool
44G4TrajectoryChargeFilter::Evaluate(const G4VTrajectory& traj) const
45{
46  G4double charge = traj.GetCharge();
47
48  if (GetVerbose()) G4cout<<"G4TrajectoryChargeFilter processing trajectory with charge: "<<charge<<G4endl;
49
50  MyCharge myCharge;
51
52  if(charge>0.)      myCharge = Positive; 
53  else if(charge<0.) myCharge = Negative; 
54  else               myCharge = Neutral; 
55
56  std::vector<MyCharge>::const_iterator iter = std::find(fCharges.begin(), fCharges.end(), myCharge);
57
58  // Fail if charge not registered
59  if (iter == fCharges.end()) return false;
60
61  return true;
62}
63
64void
65G4TrajectoryChargeFilter::Add(const G4String& charge) 
66{
67  MyCharge myCharge;
68 
69  if (!ConvertToCharge(charge, myCharge)) {
70    std::ostringstream o;
71    o << "Invalid charge "<<charge;
72    G4Exception   
73      ("G4TrajectoryChargeFilter::Add(const G4String& charge)", "InvalidCharge", JustWarning, o.str().c_str());
74    return;
75  }
76 
77  return Add(myCharge);
78}
79
80void
81G4TrajectoryChargeFilter::Add(const MyCharge& charge)
82{
83  fCharges.push_back(charge);
84}
85
86void
87G4TrajectoryChargeFilter::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
98void 
99G4TrajectoryChargeFilter::Clear()
100{
101  // Clear registered charge vector
102  fCharges.clear();
103}
104
105G4bool
106G4TrajectoryChargeFilter::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}
Note: See TracBrowser for help on using the repository browser.