Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:21:51

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /*
0027  * =============================================================================
0028  *
0029  *       Filename:  CexmcCustomFilterEval.cc
0030  *
0031  *    Description:  custom filter eval
0032  *
0033  *        Version:  1.0
0034  *        Created:  17.07.2010 15:46:01
0035  *       Revision:  none
0036  *       Compiler:  gcc
0037  *
0038  *         Author:  Alexey Radkov (), 
0039  *        Company:  PNPI
0040  *
0041  * =============================================================================
0042  */
0043 
0044 #ifdef CEXMC_USE_CUSTOM_FILTER
0045 
0046 #include <fstream>
0047 #include <string>
0048 #include "CexmcCustomFilterEval.hh"
0049 #include "CexmcException.hh"
0050 
0051 
0052 CexmcCustomFilterEval::CexmcCustomFilterEval( const G4String &  sourceFileName,
0053                                   const CexmcEventFastSObject *  evFastSObject,
0054                                   const CexmcEventSObject *  evSObject ) :
0055     astEval( evFastSObject, evSObject )
0056 {
0057     std::string     command;
0058     std::ifstream   sourceFile( sourceFileName );
0059 
0060     if ( ! sourceFile )
0061         throw CexmcException( CexmcCFBadSource );
0062 
0063     bool            commandIsPending( false );
0064 
0065     while ( ! sourceFile.eof() )
0066     {
0067         std::string  line;
0068         std::getline( sourceFile, line );
0069 
0070         size_t  commentStartPos( line.find_first_of( '#' ) );
0071         if ( commentStartPos != std::string::npos )
0072             line.erase( commentStartPos );
0073 
0074         if ( line.empty() ||
0075              line.find_first_not_of( " \t" ) == std::string::npos )
0076         {
0077             if ( commandIsPending )
0078             {
0079                 sourceFile.close();
0080                 throw CexmcException( CexmcCFParseError );
0081             }
0082             continue;
0083         }
0084 
0085         command += line;
0086 
0087         size_t  length( command.length() );
0088 
0089         if ( command[ length - 1 ] == '\\' )
0090         {
0091             command.erase( length - 1 );
0092             commandIsPending = true;
0093             continue;
0094         }
0095 
0096         CexmcCustomFilter::ParseResult  curParseResult;
0097 
0098         std::string::const_iterator   begin( command.begin() );
0099         std::string::const_iterator   end( command.end() );
0100 
0101         try
0102         {
0103             if ( ! CexmcCustomFilter::phrase_parse( begin, end, grammar,
0104                                 CexmcCustomFilter::space, curParseResult ) ||
0105                  begin != end )
0106             {
0107                 throw CexmcException( CexmcCFParseError );
0108             }
0109         }
0110         catch ( ... )
0111         {
0112             sourceFile.close();
0113             throw;
0114         }
0115 
0116 #ifdef CEXMC_DEBUG_CF
0117         G4cout << "Parsed expression AST:" << G4endl;
0118         curParseResult.expression.Print();
0119 #endif
0120 
0121         switch ( curParseResult.action )
0122         {
0123         case CexmcCustomFilter::KeepTPT :
0124         case CexmcCustomFilter::DeleteTPT :
0125             parseResultTPT.push_back( curParseResult );
0126             break;
0127         case CexmcCustomFilter::KeepEDT :
0128         case CexmcCustomFilter::DeleteEDT :
0129             parseResultEDT.push_back( curParseResult );
0130             break;
0131         default :
0132             break;
0133         }
0134 
0135         command = "";
0136         commandIsPending = false;
0137     }
0138 
0139     sourceFile.close();
0140 
0141     if ( commandIsPending )
0142         throw CexmcException( CexmcCFParseError );
0143 }
0144 
0145 
0146 void  CexmcCustomFilterEval::SetAddressedData(
0147                                 const CexmcEventFastSObject *  evFastSObject,
0148                                 const CexmcEventSObject *  evSObject )
0149 {
0150     astEval.SetAddressedData( evFastSObject, evSObject );
0151 
0152     for ( ParseResultVector::iterator  k( parseResultTPT.begin() );
0153           k != parseResultTPT.end(); ++k )
0154     {
0155         if ( evFastSObject == NULL || evSObject == NULL )
0156             astEval.ResetAddressBinding( k->expression );
0157         else
0158             astEval.BindAddresses( k->expression );
0159     }
0160     for ( ParseResultVector::iterator  k( parseResultEDT.begin() );
0161           k != parseResultEDT.end(); ++k )
0162     {
0163         if ( evFastSObject == NULL || evSObject == NULL )
0164             astEval.ResetAddressBinding( k->expression );
0165         else
0166             astEval.BindAddresses( k->expression );
0167     }
0168 }
0169 
0170 
0171 bool  CexmcCustomFilterEval::EvalTPT( void ) const
0172 {
0173     for ( ParseResultVector::const_iterator  k( parseResultTPT.begin() );
0174           k != parseResultTPT.end(); ++k )
0175     {
0176         if ( astEval( k->expression ) )
0177             return k->action == CexmcCustomFilter::KeepTPT;
0178     }
0179 
0180     return true;
0181 }
0182 
0183 
0184 bool  CexmcCustomFilterEval::EvalEDT( void ) const
0185 {
0186     for ( ParseResultVector::const_iterator  k( parseResultEDT.begin() );
0187           k != parseResultEDT.end(); ++k )
0188     {
0189         if ( astEval( k->expression ) )
0190             return k->action == CexmcCustomFilter::KeepEDT;
0191     }
0192 
0193     return true;
0194 }
0195 
0196 
0197 #endif
0198