/
File.h
624 lines (511 loc) · 14.2 KB
/
File.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
\file
Declarations of file information and log-reading classes.
\author Thomas Burton
\date 2011-07-29
\copyright 2011 Brookhaven National Lab
*/
#ifndef INCLUDE_EICSMEAR_ERHIC_FILE_H_
#define INCLUDE_EICSMEAR_ERHIC_FILE_H_
#include <iostream>
#include <map>
#include <string>
#include <TObject.h>
#include <TObjString.h>
#include <TString.h>
#include "eicsmear/erhic/EventBase.h"
#include "eicsmear/erhic/EventHepMC.h"
#include "eicsmear/erhic/EventMilou.h"
#include "eicsmear/erhic/EventPythia.h"
#include "eicsmear/erhic/EventFactory.h"
namespace erhic {
/**
Base class for log file processors.
Reads a log file from a Monte Carlo generator and extracts information.
Inherited classes for each generator type implement the Extract()
method to gather the required information for that generator and the
Save() method to store it to file.
*/
class LogReader : public TObject {
public:
/**
Constructor.
*/
LogReader() { }
/**
Destructor.
*/
virtual ~LogReader() { }
/**
Return a new LogReader instance.
*/
virtual LogReader* Create() const = 0;
/**
Extract data from the named log file.
*/
virtual bool Extract(const std::string& file) = 0;
/**
Saves the extracted data to the current file, if one is open and is
writeable.
Returns -1 if the data cannot be saved.
To write the LogReader itself, use LogReader::Write().
*/
virtual Int_t Save() const = 0;
ClassDef(erhic::LogReader, 1)
};
/**
Processes PYTHIA log files.
Reads a log file and finds the total cross section and the number
of generated events when Extract() is called.
Writes those values as TObjStrings to the current directory when
Save() is called, assuming that directory is writeable.
*/
class LogReaderPythia : public LogReader {
public:
/**
Constructor.
*/
LogReaderPythia();
/**
Destructor.
*/
virtual ~LogReaderPythia();
/**
Return a new LogReaderPythia instance.
*/
LogReaderPythia* Create() const;
/**
Extract data from the named log file.
*/
bool Extract(const std::string& file);
/**
Write the extracted information to the current file, if it is
writeable. If you want to write the LogReaderPythia itself, use
LogReaderPythia::Write().
*/
Int_t Save() const;
protected:
TObjString crossSection_; ///> Total cross section in microbarns
TObjString nEvents_; ///> Total number of events generated and kept
TObjString nTrials_; ///> Total number of events generated
ClassDef(erhic::LogReaderPythia, 2)
};
inline LogReaderPythia* LogReaderPythia::Create() const {
return new LogReaderPythia;
}
/**
Processes PEPSI log files.
Reads a log file and finds the total cross section and the number
of generated events when Extract() is called.
Writes those values as TObjStrings to the current directory when
Save() is called, assuming that directory is writeable.
*/
class LogReaderPepsi : public LogReader {
public:
/**
Constructor.
*/
LogReaderPepsi();
/**
Destructor.
*/
virtual ~LogReaderPepsi();
/**
Return a new LogReaderPepsi instance.
*/
LogReaderPepsi* Create() const;
/**
Extract data from the named log file.
*/
bool Extract(const std::string& file);
/**
Write the extracted information to the current file, if it is
writeable. If you want to write the LogReaderPepsi itself, use
LogReaderPepsi::Write().
*/
Int_t Save() const;
protected:
TObjString crossSection_; ///> Total cross section in microbarns
TObjString nEvents_; ///> Total number of events generated and kept
TObjString nTrials_; ///> Total number of events generated
ClassDef(erhic::LogReaderPepsi, 2)
};
inline LogReaderPepsi* LogReaderPepsi::Create() const {
return new LogReaderPepsi;
}
/**
Processes DJANGOH log files.
Reads a log file and finds the total cross section and the number
of generated events when Extract() is called.
Writes those values as TObjStrings to the current directory when
Save() is called, assuming that directory is writeable.
*/
class LogReaderDjangoh : public LogReader {
public:
/**
Constructor.
*/
LogReaderDjangoh();
/**
Destructor.
*/
virtual ~LogReaderDjangoh();
/**
Return a new LogReaderDjangoh instance.
*/
LogReaderDjangoh* Create() const;
/**
Extract data from the named log file.
*/
bool Extract(const std::string& file);
/**
Write the extracted information to the current file, if it is
writeable. If you want to write the LogReaderDjangoh itself, use
LogReaderDjangoh::Write().
*/
Int_t Save() const;
protected:
TObjString crossSection_; ///> Total cross section in microbarns
TObjString nEvents_; ///> Total number of events generated and kept
TObjString nTrials_; ///> Total number of events generated
ClassDef(erhic::LogReaderDjangoh, 2)
};
inline LogReaderDjangoh* LogReaderDjangoh::Create() const {
return new LogReaderDjangoh;
}
/**
Processes PYTHIA log files.
Reads a log file and finds the total cross section and the number
of generated events when Extract() is called.
Writes those values as TObjStrings to the current directory when
Save() is called, assuming that directory is writeable.
*/
class LogReaderMilou : public LogReader {
public:
/**
Constructor.
*/
LogReaderMilou() { }
/**
Destructor.
*/
virtual ~LogReaderMilou() { }
/**
Return a new LogReaderMilou instance.
*/
LogReaderMilou* Create() const;
/**
Extract data from the named log file.
*/
bool Extract(const std::string& file);
/**
Write the extracted information to the current file, if it is
writeable. If you want to write the LogReaderMilou itself, use
LogReaderMilou::Write().
*/
Int_t Save() const;
/**
Returns the number of events reported by the log file.
Extract() should be called first.
*/
Int_t GetNEvents() const;
/**
Returns the total cross section reported by the log file.
Extract() should be called first.
*/
Double_t GetCrossSection() const;
/**
Returns the error on total cross section reported by the log file.
Extract() should be called first.
*/
Double_t GetCrossSectionError() const;
protected:
TObjString crossSection_; ///> Total cross section in nb
TObjString crossSectionError_; ///> Cross section error in nb
TObjString nEvents_; ///> Total number of events generated
ClassDef(erhic::LogReaderMilou, 1)
};
inline LogReaderMilou* LogReaderMilou::Create() const {
return new LogReaderMilou;
}
inline Int_t LogReaderMilou::GetNEvents() const {
return nEvents_.GetString().Atoi();
}
inline Double_t LogReaderMilou::GetCrossSection() const {
return crossSection_.GetString().Atof();
}
/**
Returns the error on total cross section reported by the log file.
Extract() should be called first.
*/
inline Double_t LogReaderMilou::GetCrossSectionError() const {
return crossSectionError_.GetString().Atof();
}
/**
Processes gmc_trans log files.
Reads a log file and finds the total cross section and the number
of generated events.
*/
class LogReaderGmcTrans : public LogReader {
public:
/**
Constructor.
*/
LogReaderGmcTrans();
/**
Destructor.
*/
virtual ~LogReaderGmcTrans();
/**
Return a new LogReaderGmcTrans instance.
*/
LogReaderGmcTrans* Create() const;
/**
Search the named file.
Store the total cross section and number of generated events
if they can be found.
*/
bool Extract(const std::string& filename);
/**
Write the stored cross section and number of events to
the active ROOT directory.
Returns the total number of bytes written, or a value <= 0
in the case of an error.
*/
Int_t Save() const;
/**
Returns the number of events reported by the log file.
Extract() should be called first.
*/
Int_t GetNEvents() const;
/**
Returns the total cross section reported by the log file.
Extract() should be called first.
*/
Double_t GetCrossSection() const;
protected:
TObjString mNEvents; ///< Number of generated events.
TObjString mCrossSection; ///< Total cross section in microbarns.
ClassDef(erhic::LogReaderGmcTrans, 1)
};
/**
Factory class for LogReaders.
Singleton class.
Creates a LogReader instance corresponding to a Monte Carlo
generator type.
*/
class LogReaderFactory {
public:
/**
Returns the single instance of LogReaderFactory.
*/
static LogReaderFactory& GetInstance();
/**
Returns a LogReader instance of the type for reading log files
from the Monte Carlo generator event type 'event'.
Returns NULL in the case of an unsupported generator.
The LogReader must be deleted by the user.
*/
LogReader* CreateReader(const EventBase& event) const;
/**
Returns a LogReader instance of the type for reading log files
from the Monte Carlo generator named 'name'.
Returns NULL in the case of an unsupported generator.
The LogReader must be deleted by the user.
*/
LogReader* CreateReader(const std::string& name) const;
/**
Returns a LogReader instance of the type for reading log files
from the Monte Carlo generator which produced the content in an
istream, by reading the first line of that stream.
Returns NULL in the case of an unsupported generator.
The LogReader must be deleted by the user.
*/
LogReader* CreateReader(std::istream&) const;
/**
Attempts to locate a log file corresponding to the named Monte
Carlo file.
Searches for a file with the same base name and extension '.log'.
Looks in
the current directory and, if mcFile gives a path containing
'TXTFILES', in the corresonding directory substituting 'LOGFILES'.
*/
std::string Locate(const std::string& mcFile) const;
protected:
/**
Constructor.
*/
LogReaderFactory();
/**
Destructor.
*/
~LogReaderFactory();
typedef std::map<std::string, LogReader*> Map;
Map prototypes_;
ClassDef(erhic::LogReaderFactory, 1)
};
/**
Abstract base class for Monte Carlo file types.
Describes a Monte Carlo file type and returns objects required for
processing or analysis of that file type.
*/
class FileType : public TObject {
public:
/**
Destructor.
*/
virtual ~FileType() { }
/**
Returns a new FileType instance.
*/
virtual FileType* Create() const = 0;
/**
Returns a new event object for the generator making this type of file.
*/
virtual EventBase* AllocateEvent() const = 0;
/**
Returns the name of the generator.
Should be entirely in lower case.
*/
virtual std::string GetGeneratorName() const;
/**
Sets the name of the generator.
Should be entirely in lower case.
*/
virtual void SetGeneratorName(const std::string newname="");
/**
Returns a reader to process the log file corresponding to this type of file.
*/
virtual LogReader* CreateLogReader() const = 0;
/**
Returns a new event object for the generator making this type of file.
*/
virtual VirtualEventFactory* CreateEventFactory(std::istream&) const = 0;
protected:
std::string generatorname;
ClassDef(erhic::FileType, 2)
};
/*
Templated file descriptor class, valid for Monte Carlo event classes.
e.g. File<EventPythia> describes a Pythia event file.
*/
template<typename T>
class File : public FileType {
public:
/**
Constructor.
If the string argument is not empty, the File attempts to open
a file with that name. If the file is opened it tries to extract
*/
File();
/**
Destructor.
*/
virtual ~File();
/**
Returns a new File object.
*/
virtual File<T>* Create() const;
/**
Allocates an event of the type for this file.
*/
virtual T* AllocateEvent() const;
/**
Create a LogReader for this type of Monte Carlo file.
Returns NULL if the file type is unsupported or has no LogReader
class implemented.
The LogReader must be deleted by the user.
*/
virtual LogReader* CreateLogReader() const;
/**
Returns a new event factory instance.
*/
virtual EventFromAsciiFactory<T>*
CreateEventFactory(std::istream& is) const {
return new EventFromAsciiFactory<T>(is);
}
protected:
T* t_;
// Warning: explicitly putting the erhic:: namespace before the class
// name doesn't seen to work for template classes.
ClassDef(File, 2)
};
template<>
class File<erhic::EventHepMC> : public FileType {
public:
/**
Constructor.
If the string argument is not empty, the File attempts to open
a file with that name. If the file is opened it tries to extract
*/
File(){}
/**
Destructor.
*/
virtual ~File(){}
/**
Returns a new File object.
*/
virtual File<erhic::EventHepMC>* Create() const {
return new File<erhic::EventHepMC>();
}
/**
Allocates an event of the type for this file.
*/
virtual erhic::EventHepMC* AllocateEvent() const {
return new erhic::EventHepMC();
}
/**
Create a LogReader for this type of Monte Carlo file.
Returns NULL if the file type is unsupported or has no LogReader
class implemented.
The LogReader must be deleted by the user.
*/
virtual LogReader* CreateLogReader() const {return nullptr;}
EventFromAsciiFactory<erhic::EventHepMC>* CreateEventFactory(std::istream& is) const {
return new EventFromAsciiFactory<erhic::EventHepMC>(is);}
protected:
erhic::EventHepMC* t_;
ClassDef(File<erhic::EventHepMC>, 1)
};
template<typename T>
inline T* File<T>::AllocateEvent() const {
return new T;
}
template<typename T>
inline File<T>* File<T>::Create() const {
return new File<T>();
}
/**
Factory class for Files.
Singleton class.
*/
class FileFactory {
public:
/**
Returns the single instance of FileFactory.
*/
static FileFactory& GetInstance();
/**
Returns a FileType object for the named generator.
*/
const FileType* GetFile(const std::string& generatorName) const;
/**
Returns a FileType object, determining the generator type from a stream.
The isp may need to be updated for hepmc, that's why it's passed by reference.
*/
const FileType* GetFile(std::shared_ptr<std::istream>& isp, const std::string fileName="") const;
protected:
/**
Constructor.
*/
FileFactory();
/**
Destructor.
*/
virtual ~FileFactory();
typedef std::map<std::string, FileType*> Map;
Map prototypes_;
};
} // namespace erhic
#endif // INCLUDE_EICSMEAR_ERHIC_FILE_H_