int THaCodaFile::filterToFile(const char* output_file) { // A call to filterToFile filters from present file to output_file // using filter criteria defined by evtypes, evlist, and max_to_filt // which are loaded by public methods of this class. If no conditions // were loaded, it makes a copy of the input file (i.e. no filtering). int i; if(filename == output_file) { if(CODA_VERBOSE) { cout << "filterToFile: ERROR: "; cout << "Input and output files cannot be same " << endl; cout << "This is to protect you against overwriting data" << endl; } fStatus = CODA_ERROR; return fStatus; } FILE *fp; if ((fp = fopen(output_file,"r")) != NULL) { if(CODA_VERBOSE) { cout << "filterToFile: ERROR: "; cout << "Output file `" << output_file << "' exists " << endl; cout << "You must remove it by hand first. " << endl; cout << "This forces you to think and not overwrite data." << endl; } fclose(fp); fStatus = CODA_ERROR; return fStatus; } THaCodaFile* fout = new THaCodaFile(output_file,"w"); int nfilt = 0; while (codaRead() == S_SUCCESS) { int* rawbuff = getEvBuffer(); int evtype = rawbuff[1]>>16; int evnum = rawbuff[4]; int oktofilt = 1; if (CODA_DEBUG) { cout << "Input evtype " << dec << evtype; cout << " evnum " << evnum << endl; cout << "max_to_filt = " << max_to_filt << endl; cout << "evtype size = " << evtypes[0] << endl; cout << "evlist size = " << evlist[0] << endl; } if ( evtypes[0] > 0 ) { oktofilt = 0; for (i=1; i<=evtypes[0]; i++) { if (evtype == evtypes[i]) { oktofilt = 1; goto Cont1; } } } Cont1: if ( evlist[0] > 0 ) { oktofilt = 0; for (i=1; i<=evlist[0]; i++) { if (evnum == evlist[i]) { oktofilt = 1; goto Cont2; } } } Cont2: if (oktofilt) { nfilt++; if (CODA_DEBUG) { cout << "Filtering event, nfilt " << dec << nfilt << endl; } fStatus = fout->codaWrite(getEvBuffer()); if (fStatus != S_SUCCESS) { if (CODA_VERBOSE) { cout << "Error in filterToFile ! " << endl; cout << "codaWrite returned status " << fStatus << endl; } goto Finish; } if (max_to_filt > 0) { if (nfilt == max_to_filt) { goto Finish; } } } } Finish: delete fout; return S_SUCCESS; };