G4SmartFilter.hh

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 with additional funcionality such as active and inverted states, 
00029 // and filtering statistics
00030 //
00031 // Jane Tinslay, March 2006
00032 //
00033 #ifndef G4SMARTFILTER_HH
00034 #define G4SMARTFILTER_HH
00035 
00036 #include "G4VFilter.hh"
00037 
00038 template <typename T>
00039 class G4SmartFilter : public G4VFilter<T> {
00040 
00041 public: // With description
00042 
00043   // Construct with filter name
00044   G4SmartFilter(const G4String& name);
00045 
00046   virtual ~G4SmartFilter();
00047 
00048   // Evaluate method implemented in subclass
00049   virtual G4bool Evaluate(const T&) const = 0;
00050 
00051   // Print subclass configuration
00052   virtual void Print(std::ostream& ostr) const = 0;
00053 
00054   // Clear filter 
00055   virtual void Clear() = 0;
00056 
00057   // Filter method
00058   G4bool Accept(const T&) const;
00059   
00060   // Print G4SmartFilter configuration
00061   virtual void PrintAll(std::ostream& ostr) const;
00062 
00063   //Reset
00064   virtual void Reset();
00065 
00066   // Activate/deactivate filter
00067   void SetActive(const G4bool&);
00068   G4bool GetActive() const;
00069 
00070   // Invert filter
00071   void SetInvert(const G4bool&);
00072   G4bool GetInvert() const;
00073 
00074   // Set verbosity
00075   void SetVerbose(const G4bool&);
00076   G4bool GetVerbose() const;
00077   
00078 
00079 private:
00080 
00081   // Data members
00082   G4bool fActive;
00083   G4bool fInvert;
00084   G4bool fVerbose;
00085   mutable size_t fNPassed;
00086   mutable size_t fNProcessed;
00087 
00088 };
00089 
00090 template <typename T>
00091 G4SmartFilter<T>::G4SmartFilter(const G4String& name)
00092   :G4VFilter<T>(name)
00093   ,fActive(true)
00094   ,fInvert(false)
00095   ,fVerbose(false)
00096   ,fNPassed(0)
00097   ,fNProcessed(0)
00098 {}
00099 
00100 template <typename T>
00101 G4SmartFilter<T>::~G4SmartFilter() {}
00102 
00103 template <typename T>
00104 G4bool 
00105 G4SmartFilter<T>::Accept(const T& object) const
00106 {
00107   if (fVerbose) {
00108     G4cout<<"Begin verbose printout for filter "<<G4VFilter<T>::Name()<<G4endl;
00109     G4cout<<"Active ? :   "<<fActive<<G4endl;
00110   }
00111   
00112   fNProcessed++;
00113   
00114   // Pass everything if filter is not active
00115   if (!fActive) {
00116     fNPassed++;
00117     return true;
00118   }
00119   
00120   // Do filtering
00121   G4bool passed = Evaluate(object);
00122   
00123   // Apply inversion if applicable
00124   if (fInvert) passed = !passed;
00125   
00126   if (passed) fNPassed++;
00127   
00128   if (fVerbose) {
00129     G4cout<<"Inverted ? : "<<fInvert<<G4endl;
00130     G4cout<<"Passed ?   : "<<passed<<G4endl;
00131     G4cout<<"End verbose printout for filter "<<G4VFilter<T>::Name()<<G4endl;
00132   }
00133   
00134   return passed;
00135 }
00136 
00137 template <typename T>
00138 void 
00139 G4SmartFilter<T>::PrintAll(std::ostream& ostr) const 
00140 {
00141   ostr<<"Printing data for filter: "<<G4VFilter<T>::Name()<<G4endl;
00142 
00143   Print(ostr);
00144 
00145   ostr<<"Active ?   : " <<fActive<<G4endl;
00146   ostr<<"Inverted ? : " <<fInvert<<G4endl;
00147   ostr<<"#Processed : " <<fNProcessed<<G4endl;
00148   ostr<<"#Passed    : " <<fNPassed<<G4endl;
00149 }
00150 
00151 template <typename T>
00152 void
00153 G4SmartFilter<T>::Reset()
00154 {
00155   fActive = true;
00156   fInvert = false;
00157   fNProcessed = 0;
00158   fNPassed = 0;
00159   
00160   // Clear subclass data
00161   Clear();
00162 }
00163 
00164 template <typename T>
00165 void 
00166 G4SmartFilter<T>::SetActive(const G4bool& active) 
00167 {
00168   fActive = active;
00169 }
00170 
00171 template <typename T>
00172 G4bool 
00173 G4SmartFilter<T>::GetActive() const 
00174 {
00175   return fActive;
00176 }
00177 
00178 template <typename T>
00179 void 
00180 G4SmartFilter<T>::SetInvert(const G4bool& invert) 
00181 {
00182   fInvert = invert;
00183 }
00184 
00185 template <typename T>
00186 G4bool
00187 G4SmartFilter<T>::GetInvert() const
00188 {
00189   return fInvert;
00190 }
00191 
00192 template <typename T>
00193 void 
00194 G4SmartFilter<T>::SetVerbose(const G4bool& verbose) 
00195 {
00196   fVerbose = verbose;
00197 }
00198 
00199 template <typename T>
00200 G4bool
00201 G4SmartFilter<T>::GetVerbose() const
00202 {
00203   return fVerbose;
00204 }
00205 
00206 #endif
00207 

Generated on Mon May 27 17:49:51 2013 for Geant4 by  doxygen 1.4.7