LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
addRandomAccess.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCWriter.h"
4 
5 // position of record name for last random access record
6 #define LCSIO_RANDOMACCESS_SIZE 112
7 
8 #include <iostream>
9 #include <sstream>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 
14 using namespace lcio ;
15 
17 
18 
22 int main(int argc, char** argv ){
23 
24 
25  // create sio writer
26  LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ;
27 
28  // read file names from command line (only argument)
29  if( argc < 2) {
30  std::cout << " usage: addRandomAccess <input-file1> [[input-file2],...]" << std::endl ;
31  exit(1) ;
32  }
33 
34  int firstFile = 1 ;
35  bool force = false ;
36  std::string firstArg( argv[1] ) ;
37 
38  if( firstArg == "-f" ){ // force: 'undocumented' feature for fixing files with broken TOC (SplitWriter)
39  firstFile++ ;
40  std::cout << " ====== called with -f (force) : will create a TOC for direct access in any case ! " << std::endl ;
41  force = true ;
42  }
43 
44  for(int i=firstFile ; i < argc ; i++){
45  FILEN.push_back( argv[i] ) ;
46  }
47 
48  int nFiles = argc - firstFile ;
49 
50  for( int i=0 ; i <nFiles ; ++i ) {
51 
52  try{
53 
54 
55  if( force) { // --------------------------------------------------------------------------------------------
56  // if called with force we rename the last LCIORandomAcces record so that it is ignored and the
57  // file treated as an old one w/o direct access....
58 
59  FILE* f = fopen( FILEN[i].c_str() , "r+") ;
60 
61  if( f != 0 ){
62 
63  fseek( f , -( LCSIO_RANDOMACCESS_SIZE ) , SEEK_END ) ;
64  std::string bla("") ;
65  bla.resize(17) ;
66 
67  int status = fread( &bla[0] , sizeof(char) , 16 , f ) ;
68 
69  if( !strcmp( bla.c_str() , "LCIORandomAccess") || status != 16 ){
70 
71 
72  status = fseek( f , -(16) , SEEK_CUR ) ;
73 
74  bla = "LCIORandomIGNORE" ;
75 
76  status = fwrite( &bla[0] , 1 , 16 , f ) ;
77 
78  }
79  }
80 
81  fclose( f ) ;
82  } //---------- end if(force) --------------------------------------------------------------------------------
83 
84 
85  lcWrt->open( FILEN[i] , LCIO::WRITE_APPEND ) ;
86 
87  lcWrt->close() ;
88 
89  }catch(IOException& e){
90 
91  std::cout << " io error in file " << FILEN[i] << " : " << e.what() << std::endl ;
92  }
93  }
94 
95  return 0 ;
96 }
97 
T endl(T...args)
T resize(T...args)
#define LCSIO_RANDOMACCESS_SIZE
STL class.
T push_back(T...args)
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
T c_str(T...args)
static std::vector< std::string > FILEN