// $Id: mcsmear.cc 2388 2007-01-10 16:46:03Z davidl $ // // Created June 22, 2005 David Lawrence #include #include #include #include using namespace std; #include #include #include #include "HDDM/hddm_s.hpp" void Smear(hddm_s::HDDM *record); void ParseCommandLineArguments(int narg, char* argv[]); void Usage(void); void ctrlCHandle(int x); std::vector Nevents_to_merge; std::vector loop_source; std::vector INFILENAMES; char *OUTFILENAME = NULL; int QUIT = 0; int MAXEVENTS=-1; bool HDDM_USE_COMPRESSION=false; bool HDDM_USE_INTEGRITY_CHECKS=false; #define _DBG_ std::cout << __FILE__ << ":" << __LINE__ << " " #define _DBG__ std::cout << __FILE__ << ":" << __LINE__ << std::endl //----------- // main //----------- int main(int narg,char* argv[]) { // Set up to catch SIGINTs for graceful exits signal(SIGINT,ctrlCHandle); ParseCommandLineArguments(narg, argv); // Dummy check if (Nevents_to_merge.size() != INFILENAMES.size()) { _DBG_ << "Size of Nevents_to_merge and INFILENAMES vectors" " not the same!" << std::endl; _DBG_ << "This indicates a bug in the program. Exiting ..." << std::endl; exit(-1); } if (Nevents_to_merge.size() != loop_source.size()) { _DBG_ << "Size of Nevents_to_merge and loop_source vectors" " not the same!" << std::endl; _DBG_ << "This indicates a bug in the program. Exiting ..." << std::endl; exit(-1); } // Open Input file(s) std::vector infiles; std::vector instreams; for (unsigned int i=0; i < INFILENAMES.size(); i++) { std::cout << " input file: " << INFILENAMES[i] << " (" << Nevents_to_merge[i] << " events)" << std::endl; std::ifstream *ifs = new std::ifstream(INFILENAMES[i]); if (! ifs->is_open()) { std::cout << " Error opening input file \"" << INFILENAMES[i] << "\"!" << std::endl; exit(-1); } hddm_s::istream *fin = new hddm_s::istream(*ifs); instreams.push_back(fin); infiles.push_back(ifs); } // Output file std::cout << " output file: " << OUTFILENAME << std::endl; std::ofstream *ofs = new std::ofstream(OUTFILENAME); if (! ofs->is_open()) { std::cout << " Error opening output file \"" << OUTFILENAME << "\"!" << std::endl; exit(-1); } hddm_s::ostream *fout = new hddm_s::ostream(*ofs); if (HDDM_USE_COMPRESSION) { std::cout << " Enabling bz2 compression of output HDDM file stream" << std::endl; fout->setCompression(hddm_s::k_bz2_compression); } else { std::cout << " HDDM compression disabled on output" << std::endl; } if (HDDM_USE_INTEGRITY_CHECKS) { std::cout << " Enabling data integrity check on output HDDM file stream" << std::endl; fout->setIntegrityChecks(hddm_s::k_crc32_integrity); } else { std::cout << " HDDM integrity checks disabled on output" << std::endl; } // Loop over events from input files, interleaving them // into the output file until one of the inputs does not // have enough events to make an output int NEvents = 0; int NEvents_read = 0; time_t last_time = time(NULL); while (true) { hddm_s::HDDM record; // Loop over inputs bool done=false; for (unsigned int i=0; i < instreams.size(); i++) { for (unsigned int j=0; j < (unsigned int)Nevents_to_merge[i]; j++) { if (! infiles[i]->good()) { // Looks like this source is out of events. Check if we need // to re-open this input to keep reading events from it. if (loop_source[i]) { delete instreams[i]; delete infiles[i]; infiles[i] = new std::ifstream(INFILENAMES[i]); instreams[i] = new hddm_s::istream(*infiles[i]); std::cout << std::endl << "Reopened \"" << INFILENAMES[i] << "\" ..." << std::endl; } else { done = true; break; } } *instreams[i] >> record; *fout << record; NEvents_read++; NEvents++; } if (done) break; } if (done) break; // Update ticker time_t now = time(NULL); if (now != last_time) { std::cout << " " << NEvents_read << " events read (" << NEvents << " event written) \r"; std::cout.flush(); last_time = now; } if (QUIT || (MAXEVENTS > 0 && NEvents >= MAXEVENTS)) break; } // Close all inputs for (unsigned int i=0; i < instreams.size(); i++) { delete instreams[i]; delete infiles[i]; } delete fout; delete ofs; std::cout << " " << NEvents << " events read" << std::endl; return 0; } //----------- // ParseCommandLineArguments //----------- void ParseCommandLineArguments(int narg, char* argv[]) { int Nmerge = 1; bool loop = false; INFILENAMES.clear(); loop_source.clear(); Nevents_to_merge.clear(); for (int i=1; i