Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CexmcCustomFilterEval.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 /*
27  * =============================================================================
28  *
29  * Filename: CexmcCustomFilterEval.cc
30  *
31  * Description: custom filter eval
32  *
33  * Version: 1.0
34  * Created: 17.07.2010 15:46:01
35  * Revision: none
36  * Compiler: gcc
37  *
38  * Author: Alexey Radkov (),
39  * Company: PNPI
40  *
41  * =============================================================================
42  */
43 
44 #ifdef CEXMC_USE_CUSTOM_FILTER
45 
46 #include <fstream>
47 #include <string>
48 #include "CexmcCustomFilterEval.hh"
49 #include "CexmcException.hh"
50 
51 
52 CexmcCustomFilterEval::CexmcCustomFilterEval( const G4String & sourceFileName,
53  const CexmcEventFastSObject * evFastSObject,
54  const CexmcEventSObject * evSObject ) :
55  astEval( evFastSObject, evSObject )
56 {
57  std::string command;
58  std::ifstream sourceFile( sourceFileName );
59 
60  if ( ! sourceFile )
61  throw CexmcException( CexmcCFBadSource );
62 
63  bool commandIsPending( false );
64 
65  while ( ! sourceFile.eof() )
66  {
67  std::string line;
68  std::getline( sourceFile, line );
69 
70  size_t commentStartPos( line.find_first_of( '#' ) );
71  if ( commentStartPos != std::string::npos )
72  line.erase( commentStartPos );
73 
74  if ( line.empty() ||
75  line.find_first_not_of( " \t" ) == std::string::npos )
76  {
77  if ( commandIsPending )
78  {
79  sourceFile.close();
80  throw CexmcException( CexmcCFParseError );
81  }
82  continue;
83  }
84 
85  command += line;
86 
87  size_t length( command.length() );
88 
89  if ( command[ length - 1 ] == '\\' )
90  {
91  command.erase( length - 1 );
92  commandIsPending = true;
93  continue;
94  }
95 
96  CexmcCustomFilter::ParseResult curParseResult;
97 
98  std::string::const_iterator begin( command.begin() );
99  std::string::const_iterator end( command.end() );
100 
101  try
102  {
103  if ( ! CexmcCustomFilter::phrase_parse( begin, end, grammar,
104  CexmcCustomFilter::space, curParseResult ) ||
105  begin != end )
106  {
107  throw CexmcException( CexmcCFParseError );
108  }
109  }
110  catch ( ... )
111  {
112  sourceFile.close();
113  throw;
114  }
115 
116 #ifdef CEXMC_DEBUG_CF
117  G4cout << "Parsed expression AST:" << G4endl;
118  curParseResult.expression.Print();
119 #endif
120 
121  switch ( curParseResult.action )
122  {
123  case CexmcCustomFilter::KeepTPT :
124  case CexmcCustomFilter::DeleteTPT :
125  parseResultTPT.push_back( curParseResult );
126  break;
127  case CexmcCustomFilter::KeepEDT :
128  case CexmcCustomFilter::DeleteEDT :
129  parseResultEDT.push_back( curParseResult );
130  break;
131  default :
132  break;
133  }
134 
135  command = "";
136  commandIsPending = false;
137  }
138 
139  sourceFile.close();
140 
141  if ( commandIsPending )
142  throw CexmcException( CexmcCFParseError );
143 }
144 
145 
146 void CexmcCustomFilterEval::SetAddressedData(
147  const CexmcEventFastSObject * evFastSObject,
148  const CexmcEventSObject * evSObject )
149 {
150  astEval.SetAddressedData( evFastSObject, evSObject );
151 
152  for ( ParseResultVector::iterator k( parseResultTPT.begin() );
153  k != parseResultTPT.end(); ++k )
154  {
155  if ( evFastSObject == NULL || evSObject == NULL )
156  astEval.ResetAddressBinding( k->expression );
157  else
158  astEval.BindAddresses( k->expression );
159  }
160  for ( ParseResultVector::iterator k( parseResultEDT.begin() );
161  k != parseResultEDT.end(); ++k )
162  {
163  if ( evFastSObject == NULL || evSObject == NULL )
164  astEval.ResetAddressBinding( k->expression );
165  else
166  astEval.BindAddresses( k->expression );
167  }
168 }
169 
170 
171 bool CexmcCustomFilterEval::EvalTPT( void ) const
172 {
173  for ( ParseResultVector::const_iterator k( parseResultTPT.begin() );
174  k != parseResultTPT.end(); ++k )
175  {
176  if ( astEval( k->expression ) )
177  return k->action == CexmcCustomFilter::KeepTPT;
178  }
179 
180  return true;
181 }
182 
183 
184 bool CexmcCustomFilterEval::EvalEDT( void ) const
185 {
186  for ( ParseResultVector::const_iterator k( parseResultEDT.begin() );
187  k != parseResultEDT.end(); ++k )
188  {
189  if ( astEval( k->expression ) )
190  return k->action == CexmcCustomFilter::KeepEDT;
191  }
192 
193  return true;
194 }
195 
196 
197 #endif
198 
G4GLOB_DLL std::ostream G4cout
#define G4endl
Definition: G4ios.hh:61