G4TrajectoryChargeFilter.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Filter trajectories according to charge. Only registered 
00029 // charges will pass the filter.
00030 //
00031 // Jane Tinslay May 2006
00032 //
00033 #include "G4TrajectoryChargeFilter.hh"
00034 #include <sstream>
00035 
00036 G4TrajectoryChargeFilter::G4TrajectoryChargeFilter(const G4String& name)
00037   :G4SmartFilter<G4VTrajectory>(name)
00038 {}
00039 
00040 G4TrajectoryChargeFilter::~G4TrajectoryChargeFilter() {}
00041 
00042 bool
00043 G4TrajectoryChargeFilter::Evaluate(const G4VTrajectory& traj) const
00044 {
00045   G4double charge = traj.GetCharge();
00046 
00047   if (GetVerbose()) G4cout<<"G4TrajectoryChargeFilter processing trajectory with charge: "<<charge<<G4endl;
00048 
00049   MyCharge myCharge;
00050 
00051   if(charge>0.)      myCharge = Positive; 
00052   else if(charge<0.) myCharge = Negative; 
00053   else               myCharge = Neutral; 
00054 
00055   std::vector<MyCharge>::const_iterator iter = std::find(fCharges.begin(), fCharges.end(), myCharge);
00056 
00057   // Fail if charge not registered 
00058   if (iter == fCharges.end()) return false;
00059 
00060   return true;
00061 }
00062 
00063 void
00064 G4TrajectoryChargeFilter::Add(const G4String& charge) 
00065 {
00066   MyCharge myCharge;
00067   
00068   if (!ConvertToCharge(charge, myCharge)) {
00069     G4ExceptionDescription ed;
00070     ed << "Invalid charge "<<charge;
00071     G4Exception   
00072       ("G4TrajectoryChargeFilter::Add(const G4String& charge)",
00073        "modeling0115", JustWarning, ed);
00074     return;
00075   }
00076   
00077   return Add(myCharge);
00078 }
00079 
00080 void
00081 G4TrajectoryChargeFilter::Add(const MyCharge& charge)
00082 {
00083   fCharges.push_back(charge);
00084 }
00085 
00086 void
00087 G4TrajectoryChargeFilter::Print(std::ostream& ostr) const
00088 {
00089   ostr<<"Charges registered: "<<G4endl;
00090   std::vector<MyCharge>::const_iterator iter = fCharges.begin();
00091   
00092   while (iter != fCharges.end()) {
00093     ostr<<*iter<<G4endl;    
00094     iter++;
00095   }
00096 }
00097 
00098 void 
00099 G4TrajectoryChargeFilter::Clear()
00100 {
00101   // Clear registered charge vector
00102   fCharges.clear();
00103 }
00104 
00105 G4bool
00106 G4TrajectoryChargeFilter::ConvertToCharge(const G4String& string, MyCharge& myCharge)
00107 {
00108   bool result(true);
00109  
00110   G4int charge;
00111   std::istringstream is(string.c_str());
00112   is >> charge;
00113 
00114   switch (charge) {
00115   case 1:
00116     myCharge = G4TrajectoryChargeFilter::Positive;
00117     break;
00118   case 0:
00119     myCharge = G4TrajectoryChargeFilter::Neutral;  
00120     break;
00121   case -1:
00122     myCharge = G4TrajectoryChargeFilter::Negative;   
00123     break;
00124   default:
00125     result = false;
00126   }
00127   
00128   return result;
00129 }

Generated on Mon May 27 17:50:01 2013 for Geant4 by  doxygen 1.4.7