LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcio_parallel_read.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCReader.h"
4 #include "UTIL/LCTOOLS.h"
5 #include "IMPL/LCEventImpl.h"
6 
7 #include <cstdlib>
8 #include <mutex>
9 #include <future>
10 
12 
13 using namespace std ;
14 using namespace lcio ;
15 
17  UTIL::LCTOOLS::dumpEvent( event );
18 }
19 
22 int main(int argc, char** argv ){
23 
24  // read file names from command line (only argument)
25  if( argc < 2) {
26  cout << " dump events in parallel from different input files" << endl << endl;
27  cout << " usage: lcio_parallel_read <input-file1> [[input-file2],...]" << endl ;
28  exit(1) ;
29  }
30 
31  int nFiles = argc-1 ;
32  std::vector<std::string> inputFiles ;
34 
35  for(int i=1 ; i <= nFiles ; i++) {
36  inputFiles.push_back( argv[i] ) ;
37  }
38 
40 
41  // loop through the list of input files
42  for(unsigned int i=0 ; i < inputFiles.size() ; i++) {
43 
44  std::string fileName = inputFiles.at(i) ;
45 
46  futures.push_back( std::async( std::launch::async, [&printMutex,fileName]() {
47  // create local reader for lcio file
48  LCFactory factory ;
49  LCReader* lcReader = factory.createLCReader() ;
50  lcReader->open( fileName ) ;
51 
52  EVENT::LCEvent *lcEvent = nullptr ;
53  int nevents = 0 ;
54 
55  while ( (lcEvent = lcReader->readNextEvent()) ) {
56  // lock before printing
57  std::lock_guard<std::mutex> lock( printMutex ) ;
58  dumpEvent( lcEvent ) ;
59  nevents ++ ;
60  }
61 
62  lcReader->close() ;
63  delete lcReader ;
64  return nevents ;
65  })) ;
66  }
67 
68  int totalNEvents = 0 ;
69 
70  for( unsigned int i=0 ; i<futures.size() ; i++ ) {
71  totalNEvents += futures.at(i).get() ;
72  }
73 
74  std::cout << "Printed " << totalNEvents << " events from " << inputFiles.size() << " files" << std::endl ;
75 
76  return 0 ;
77 }
78 
79 
std::mutex printMutex
static void dumpEvent(const EVENT::LCEvent *evt)
Simple function to dump event contents to the screen.
Definition: LCTOOLS.cc:51
T endl(T...args)
LCEvent * event
Definition: lsh.cc:80
STL class.
T at(T...args)
T push_back(T...args)
T exit(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 lock(T...args)
T size(T...args)
The main event interface.
Definition: LCEvent.h:31
static std::vector< std::string > FILEN
LCReader * lcReader
Definition: lsh.cc:78
T async(T...args)
void dumpEvent(EVENT::LCEvent *event)