a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
SlhaParser.cpp
Go to the documentation of this file.
1/*
2 * SlhaParser.cpp
3 *
4 * Created on: Jan 11, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 * Copyright 2012 Ben O'Leary
7 *
8 * This file is part of LesHouchesParserClasses, released under the
9 * GNU General Public License. Please see the accompanying
10 * README.LHPC_CPP.txt file for a full list of files, brief documentation
11 * on how to use these classes, and further details on the license.
12 */
13
14#include "SLHA.hpp"
15
16namespace LHPC
17{
18 void
19 SlhaParser::copyWithoutBlock( std::string const& originalFilename,
20 std::string const& blockToStrip,
21 std::string const& copyFilename )
22 {
24 false );
25 fileParser.openFile( originalFilename );
26 std::string dataString;
27 std::string commentString;
28 std::string firstWordOfLine;
29 std::string restOfLine;
30 bool copyingLine( true );
31 std::stringstream copyStream;
34 {
36 &restOfLine ) );
39 {
40 // if we find a DECAY, then we're not stripping out a block any more.
41 copyingLine = true;
42 }
45 {
46 /* if we find a BLOCK, then we're not stripping any more if this BLOCK
47 * is not the type that we are stripping, but we are (still) stripping
48 * if it is.
49 */
52 blockToStrip ) );
53 }
54 if( copyingLine
55 &&
56 !( dataString.empty()
57 && commentString.empty() ) )
58 {
59 // if we're not stripping out a block, record the line again (except
60 // for blank lines).
61 copyStream << dataString << commentString << "\n";
62 }
63 }
64 std::ofstream outputStream( copyFilename.c_str() );
65 outputStream << copyStream.str();
66 outputStream.close();
67 }
68
69 SlhaParser::SlhaParser( bool const shouldRecordBlocksNotRegistered,
70 bool const isVerbose ) :
71 isVerbose( isVerbose ),
72 shouldRecordBlocksNotRegistered( shouldRecordBlocksNotRegistered ),
73 fileParser( "#",
74 this->isVerbose ),
75 blockMap(),
76 blockMapIterator(),
77 mapInserter( "",
78 NULL ),
79 currentBlockPointer( NULL ),
80 dataString( "" ),
81 commentString( "" ),
82 firstWordOfLine( "" ),
83 wordsOfLine( 2 ),
84 observingSpectrumUpdater(),
85 ownsFmassBlock( false ),
86 fmassBlockPointer( NULL ),
87 fmassMap(),
88 ownsFmasserrBlock( false ),
89 fmasserrBlockPointer( NULL ),
90 fmasserrMap(),
91 ownsMassBlock( false ),
92 massBlockPointer( NULL ),
93 massMap(),
94 successfullyRead( false )
95 {
96 // just an initialization list.
97 }
98
100 {
101 if( ownsMassBlock )
102 {
103 delete massBlockPointer;
104 }
105 if( ownsFmassBlock )
106 {
107 delete fmassBlockPointer;
108 }
109 blockMapIterator = blockMap.begin();
110 while( blockMap.end() != blockMapIterator )
111 {
112 delete blockMapIterator->second;
114 }
115 }
116
117
118
119 void
121 // this registers blockToUpdate so that its data get updated every time a
122 // new block of the appropriate name is read.
123 {
124 blockMapIterator = blockMap.find( blockToUpdate.getName() );
125 if( blockMap.end() == blockMapIterator )
126 {
127 mapInserter.first.assign( blockToUpdate.getName() );
130 blockMap.insert( mapInserter );
131 }
132 else
133 {
135 }
136 if( blockToUpdate.isFmassBlock() )
137 {
138 if( ownsFmassBlock )
139 {
140 delete fmassBlockPointer;
141 ownsFmassBlock = false;
142 }
143 fmassBlockPointer = &blockToUpdate;
144 }
145 else if( blockToUpdate.isFmasserrBlock() )
146 {
148 {
150 ownsFmasserrBlock = false;
151 }
152 fmasserrBlockPointer = &blockToUpdate;
153 }
154 else if( blockToUpdate.isMassBlock() )
155 {
156 if( ownsMassBlock )
157 {
158 delete massBlockPointer;
159 ownsMassBlock = false;
160 }
161 massBlockPointer = &blockToUpdate;
162 }
163 currentBlockPointer->registerObserver( &blockToUpdate );
164 }
165
166 bool
167 SlhaParser::readFile( std::string const& slhaFileName )
168 /* this opens the file with name given by slhaFileName, parses its data
169 * into strings, stores them in its BaseStringBlocks within its
170 * SameNameBlockSets, & updates all the observing SlhaBlocks & MassSpectrums.
171 */
172 {
174 clearBlocks();
175 // new data should overwrite the old data, not append to it.
176 successfullyRead = fileParser.openFile( slhaFileName );
178 commentString ) )
179 {
181 // some perverts use tabs in their SLHA files.
185 {
187 }
190 {
192 }
193 else if( NULL != currentBlockPointer )
194 // otherwise, if a block is being recorded...
195 {
198 }
200 // otherwise, if a decay is being recorded...
201 {
203 }
204 commentString.clear();
205 }
206 // the last block is closed after the last line has been read in:
208 // after reading in the file, any recorded masses are passed to
209 // spectrumToUpdate if it is not NULL:
211 return successfullyRead;
212 }
213
214 void
216 // this ensures that if there is a spectrum to update, there are both
217 // blocks for MASS & FMASS.
218 {
219 if( !(observerList.empty()) )
220 {
221 if( NULL == fmassBlockPointer )
222 // if there is at least 1 spectrum to update, but no fmass block...
223 {
224 ownsFmassBlock = true;
227 "FMASS",
229 isVerbose,
230 9 );
232 if( blockMap.end() == blockMapIterator )
233 {
234 mapInserter.first.assign( fmassBlockPointer->getName() );
236 = new SLHA::SameNameBlockSet( mapInserter.first );
238 blockMap.insert( mapInserter );
239 }
240 else
241 {
243 }
245 }
246 if( NULL == fmasserrBlockPointer )
247 // if there is at least 1 spectrum to update, but no fmasserr block...
248 {
249 ownsFmasserrBlock = true;
252 "FMASSERR",
254 isVerbose,
255 9 );
257 if( blockMap.end() == blockMapIterator )
258 {
259 mapInserter.first.assign( fmasserrBlockPointer->getName() );
261 = new SLHA::SameNameBlockSet( mapInserter.first );
263 blockMap.insert( mapInserter );
264 }
265 else
266 {
268 }
270 }
271 if( NULL == massBlockPointer )
272 // if there is at least 1 spectrum to update, but no mass block...
273 {
274 ownsMassBlock = true;
278 isVerbose,
279 9 );
281 if( blockMap.end() == blockMapIterator )
282 {
283 mapInserter.first.assign( massBlockPointer->getName() );
285 = new SLHA::SameNameBlockSet( mapInserter.first );
287 blockMap.insert( mapInserter );
288 }
289 else
290 {
292 }
294 }
295 }
296 }
297
298 void
300 // this parses the block header line & sets currentBlockPointer
301 // appropriately, & calls checkForBlockScaleOrDecayWidth().
302 {
304 if( 2 <= wordsOfLine.getSize() )
305 // if the line at least has a string for the block name...
306 {
309 if( blockMap.end() != blockMapIterator )
310 // if the name corresponds to a block that already exists in the map...
311 {
313 }
315 // otherwise, if it should be recorded anyway...
316 {
317 mapInserter.first.assign( wordsOfLine[ 1 ] );
319 // a new block is required.
321 blockMap.insert( mapInserter );
322 }
323 if( NULL != currentBlockPointer )
324 {
325 double currentBlockScale( 0.0 );
326 // if no scale is given by "Q=", the block is assigned the default
327 // scale of 0.0 GeV.
328 if( 3 <= wordsOfLine.getSize() )
329 {
330 currentBlockScale =
332 }
335 currentBlockScale );
336 }
337 }
338 }
339
340}
bool openFile(std::string const &fileName)
bool parseNextLineOfFile(std::string &stringForData, std::string &stringForComment)
std::list< observerWithBool > observerList
virtual void registerObserver(PushedToObserver< PushedClass > *const joiningObserver)
static void transformToLowercase(std::string &stringToTransform)
static std::string firstWordOf(std::string const &stringToParse, std::string *const remainderString=NULL, std::string const &separatorChars=whitespaceChars)
static bool stringsMatchIgnoringCase(std::string const &firstString, std::string const &secondString)
static double stringToDouble(std::string const &stringToInterpret)
static void transformToUppercase(std::string &stringToTransform)
static double const notANumber
Definition: UsefulStuff.hpp:28
virtual bool isMassBlock() const
virtual bool isFmassBlock() const
std::string const & getName() const
virtual bool isFmasserrBlock() const
static std::string const blockIdentifierString
static std::string const decayIdentifierString
void recordHeader(std::string const &headerString, std::string const &commentString, double const blockScale)
void recordBodyLine(std::string const &dataString, std::string const &commentString)
void checkForMassBlocksForSpectrum()
Definition: SlhaParser.cpp:215
virtual void registerBlock(SLHA::BaseSlhaBlock &blockToUpdate)
Definition: SlhaParser.cpp:120
std::string firstWordOfLine
Definition: SlhaParser.hpp:74
BOL::VectorlikeArray< std::string > wordsOfLine
Definition: SlhaParser.hpp:75
SLHA::SameNameBlockSet * currentBlockPointer
Definition: SlhaParser.hpp:71
SLHA::BaseSlhaBlock * fmassBlockPointer
Definition: SlhaParser.hpp:78
virtual ~SlhaParser()
Definition: SlhaParser.cpp:99
SLHA::BaseSlhaBlock * massBlockPointer
Definition: SlhaParser.hpp:84
std::map< std::string, SLHA::SameNameBlockSet * > blockMap
Definition: SlhaParser.hpp:67
std::pair< std::string, SLHA::SameNameBlockSet * > mapInserter
Definition: SlhaParser.hpp:70
void prepareForEitherBlockOrDecay()
Definition: SlhaParser.hpp:250
std::map< std::string, SLHA::SameNameBlockSet * >::iterator blockMapIterator
Definition: SlhaParser.hpp:69
SlhaParser(bool const shouldRecordBlocksNotRegistered=true, bool const isVerbose=true)
Definition: SlhaParser.cpp:69
bool readFile(std::string const &slhaFileName)
Definition: SlhaParser.cpp:167
bool const isVerbose
Definition: SlhaParser.hpp:64
void prepareToReadNewDecay()
Definition: SlhaParser.hpp:186
std::string commentString
Definition: SlhaParser.hpp:73
bool const shouldRecordBlocksNotRegistered
Definition: SlhaParser.hpp:65
SpectrumUpdater observingSpectrumUpdater
Definition: SlhaParser.hpp:76
void recordDecayLine()
Definition: SlhaParser.hpp:203
void prepareToReadNewBlock()
Definition: SlhaParser.cpp:299
SLHA::BaseSlhaBlock * fmasserrBlockPointer
Definition: SlhaParser.hpp:81
void ensureSpectraRecordMasses()
Definition: SlhaParser.hpp:235
BOL::CommentedTextParser fileParser
Definition: SlhaParser.hpp:66
std::string dataString
Definition: SlhaParser.hpp:72
static void copyWithoutBlock(std::string const &originalFilename, std::string const &blockToStrip, std::string const &copyFilename)
Definition: SlhaParser.cpp:19
void finishUpEitherBlockOrDecay()
Definition: SlhaParser.hpp:218