LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCSplitWriter.cc
Go to the documentation of this file.
1 #include "UTIL/LCSplitWriter.h"
2 #include "Exceptions.h"
3 
4 #include <sys/stat.h>
5 
6 #include <sstream>
7 #include <iomanip>
8 #include <iostream>
9 
10 #ifdef SPLIT_WRITER_NDIGITS
11 #define NDIGITS SPLIT_WRITER_NDIGITS
12 #else
13 #define NDIGITS 3
14 #endif
15 
16 #if defined(__CYGWIN__) || defined(__APPLE_CC__)
17 #define STAT64 stat
18 #else
19 #define STAT64 stat64
20 #endif
21 
22 
23 
24 using namespace EVENT ;
25 
26 
27 namespace UTIL{
28 
29 
30 
31  void LCSplitWriter::open(const std::string & filename) {
32  _count = 0 ;
33  setBaseFilename( filename ) ;
34  _wrt->open( getFilename() ) ;
35  }
36 
37  void LCSplitWriter::open(const std::string & , int ) {
38  throw Exception(" LCSplitWriter doesn't support NEW and APPEND mode ! "
39  " Please remove your old file(s) and use the default mode." ) ;
40  }
41 
42  void LCSplitWriter::writeRunHeader(const EVENT::LCRunHeader * hdr) {
43 
44  _wrt->flush() ;
45  if( fileSize() > _maxBytes ) {
46  _wrt->close() ;
47  ++_count ;
48  _wrt->open( getFilename() ) ;
49  }
50 
51  _wrt->writeRunHeader( hdr ) ;
52  }
53 
54  void LCSplitWriter::writeEvent(const EVENT::LCEvent * evt) {
55 
56  _wrt->flush() ;
57 
58  if( fileSize() > _maxBytes ) {
59 
60 // std::cout << " switching to new file - old size : " << fileSize() << " > " << _maxBytes << std::endl ;
61 
62  _wrt->close() ;
63  ++_count ;
64  _wrt->open( getFilename() ) ;
65 
66 // std::cout << " switching new file size : " << fileSize() << " - file " << getFilename() << std::endl ;
67 
68  }
69 
70  _wrt->writeEvent( evt ) ;
71  }
72 
73  void LCSplitWriter::close() {
74  _wrt->close() ;
75  }
76 
77  void LCSplitWriter::flush() {
78  _wrt->flush() ;
79  }
80 
81 
82 
83 
84  const std::string& LCSplitWriter::getFilename() {
85 
86  if( _count != _lastCount )
87  _filename = std::string( _baseFilename + "." + getCountingString( _count ) + _extension ) ;
88 
89  _lastCount = _count ;
90 
91  return _filename ;
92  }
93 
94 
95 
96  long64 LCSplitWriter::file_size( const char *fname) {
97 
98  struct STAT64 sbuf;
99 
100  int ret = STAT64(fname, &sbuf);
101 
102  if( ret < 0 )
103  return -1 ;
104 
105  return sbuf.st_size ;
106  }
107 
108  long64 LCSplitWriter::fileSize() {
109 
110 // // #include <sys/stat.h>
111 // struct stat64 sbuf;
112 
113 // int ret = stat64( getFilename().c_str() , &sbuf);
114 
115 // if( ret < 0 ) return -1 ;
116 
117 // return sbuf.st_size ;
118 
119  return file_size( getFilename().c_str() ) ;
120  }
121 
122 
123  void LCSplitWriter::setBaseFilename( const std::string& filename ) {
124 
125  unsigned dotPos = filename.find_last_of('.') ;
126 
127  if( ( dotPos > 0 ) && // we have a basefile name
128  ( dotPos == filename.length() - 6 ) && // with a 5 character extension
129  ( filename.rfind("lcio") == dotPos + 2 ) ) { // that ends on lcio
130 
131  _baseFilename = filename.substr( 0 , filename.length() - 6 ) ;
132  _extension = filename.substr( filename.length() - 6 , filename.length() ) ;
133 
134  }else{
135 
136  throw Exception(" LCSplitWriter only works with complete file names including extension, e.g. myfile.slcio" ) ;
137 
138 // _baseFilename = filename ;
139  }
140  }
141 
142  std::string LCSplitWriter::getCountingString(unsigned count) {
143 
144  std::stringstream countStream ;
145 
146 
147  countStream << std::setw( NDIGITS ) << std::setfill('0') << count ;
148 
149 // std::cout << " ------------ getCountingString(" << count << ") " << countStream.str() << std::endl ;
150 
151  return countStream.str() ;
152  }
153 
154 
155 }
Base exception class for LCIO - all other exceptions extend this.
Definition: Exceptions.h:21
#define STAT64
Interface for the run header.
Definition: LCRunHeader.h:23
T rfind(T...args)
T setw(T...args)
long long long64
64 bit signed integer,e.g.to be used for timestamps
Definition: LCIOTypes.h:14
STL class.
T find_last_of(T...args)
T setfill(T...args)
T str(T...args)
T length(T...args)
The main event interface.
Definition: LCEvent.h:31
T substr(T...args)
#define NDIGITS