| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

RootRawWriter.cc

Go to the documentation of this file.
00001 /*
00002  *   Convert ".data" raw data file to ".rraw" root compitable raw data file
00003  *
00004  */
00005 const char* usage_string = "\n\
00006    Usage: \n\
00007         RootRawWriter.exe [options] [aRawData.data|aRawData.list] ...\n\
00008         In the \"aRawData.list\" each line is a \".data\" file name.\n\
00009         By default the \".rraw\" file is made next to the \".data\" file.\n\
00010 \n\
00011    options:\n\
00012         -d outdir: specify an output directory\n\
00013 \n\
00014    return codes:\n\
00015          0: standard unix code for success\n\
00016          1: error, no output produced\n\
00017          2: error, partial output may have been produced\n\
00018 ";
00019 /*   22 Jan, 2010
00020  *   Zhe Wang
00021  *
00022  *    Thu Jun 17 17:23:53 2010 bv@bnl.gov
00023  *    Let multiple .list or .data files be given.
00024  *
00025  *    Sun Jul 4 12:08:19 EDT 2010 bv@bnl.gov
00026  *    Let the user specify an output directory, fix return codes
00027  */
00028 
00029 #include <iostream>
00030 #include <fstream>
00031 #include <vector>
00032 #include <string>
00033 
00034 #include <unistd.h>
00035 
00036 #include "RawData/RawRecord.h"
00037 #include "RawData/RawRecordFormat.h"
00038 #include "RawData/RawDataReader.h"
00039 #include "RootRawWriter/RRawTree.h"
00040 
00041 using namespace std;
00042 using namespace DayaBay::RawRecord_v06;  
00043 
00044 // for getopt
00045 extern char *optarg;
00046 extern int optind, opterr, optopt;
00047 
00048 int usage(int code, const string extra_message = "");
00049 
00051 int main( int argc, char ** argv ) {
00052   int c=0;
00053   const char* outdir = 0;
00054   while ((c=getopt(argc,argv,"d:h?")) != EOF) {
00055     switch(c) {
00056     case 'd':
00057       outdir = optarg;
00058       break;
00059     case 'h':
00060     case '?':
00061       return usage(0);
00062     }
00063   }
00064 
00066   if( argc == 1 ) {
00067     return usage(1,"Require at least one input file.");
00068   }
00069 
00071   vector<string> filenames;
00072 
00073   for (int ind = optind; ind < argc; ++ind) {
00074 
00076     string parameter = argv[ind];
00077     string suffix = parameter.substr(parameter.size()-5,5);
00078     if( suffix != ".data" && suffix != ".list") {
00079       string msg = "Error: File extension \"" +suffix+ "\" not recognized.";
00080       return usage(1,msg);
00081     }
00082 
00084     if( suffix == ".data" ) {
00085       filenames.push_back(parameter.c_str());
00086       continue;
00087     }
00088 
00090     if( suffix == ".list" ) {
00091       ifstream filelist;
00092       filelist.open(parameter.c_str(), ios::in);
00093     
00095       string aLine;
00096       while(filelist >> aLine) {
00097         filenames.push_back(aLine);
00098       }
00099     
00100       filelist.close();
00101     }
00102   }
00103 
00104 
00106   for(unsigned int ci=0; ci<filenames.size(); ci++) {
00107     cout<<"RRaw::Processing "<<filenames[ci]<<endl;
00108 
00110     DayaBay::RawDataReader rr;
00111 
00113     bool ok = rr.open( filenames[ci].c_str() );
00114     if(!ok){
00115       cerr << "Failed to open " << filenames[ci] << endl;
00116       string msg = "Failed to open: \"" + filenames[ci] + "\"";
00117       return usage(1, msg);
00118     }
00119 
00121     RRawTree rrtree;
00122 
00124     string newfile = filenames[ci];
00125     newfile.replace(newfile.size()-4,4,"rraw");
00126 
00127     // But, if user wants output to go to some specific directory,
00128     // munge the filename to match
00129     if (outdir) {
00130       string::size_type ind = newfile.rfind("/");
00131       if (ind != string::npos) {
00132         newfile = newfile.substr(ind);
00133       }
00134       else {
00135         newfile = "/" + newfile;
00136       }
00137       newfile = outdir + newfile;
00138     }
00139 
00140     // Do not modify this line, scripts rely on it!
00141     cout << "RRaw::Processing to output file: " << newfile<< endl;
00142 
00143     int status = rrtree.open(newfile.c_str());
00144     if (!status) {
00145       cout <<"failed to open file: " << newfile << endl;
00146       return 2;
00147     }
00148 
00150     DayaBay::RawRecord* record;
00151     unsigned int nRecord;
00152     for( nRecord=1; /*empty field*/ ;nRecord++ ) {
00153       record = rr.nextRecord();
00154 
00156       if(!record) {
00157         return usage(2,"Error: Failed to load one record in raw data file.");
00158       }
00159       //cout<<record->size()<<endl;
00160       //record->dump( cout );
00161 
00163       unsigned int bufferSize = record->size();
00164 
00167       if( record->type() == RecordFormat::DataSeparator::type ) {
00168         DayaBay::RawDataSeparator* pSeparator 
00169           = dynamic_cast<DayaBay::RawDataSeparator*> (record);
00170         unsigned int bytes = pSeparator->blockSize();    
00171         unsigned int lines = (bytes+sizeof(unsigned int)-1)/sizeof(unsigned int);
00172         bufferSize = bufferSize + lines;
00173       }
00174 
00176       rrtree.fill( (Int32*)(record->buffer()), (Int32)bufferSize );
00177 
00179       if( record->type() == RecordFormat::FileEnd::type ) break;
00180     } // end of each record
00181 
00182     rrtree.close();
00183 
00184     cout<<"Read in "<<nRecord<<" records"<<endl;
00185 
00186   } // end of every file
00187 
00188   
00189   return 0;                     // 0 is unix standard for successful
00190 } // end of main
00191 
00192 
00194 int usage(int code, string extra_message)
00195 {
00196   cout << usage_string << endl;
00197   if (extra_message != "") {
00198     cout << extra_message << endl << endl;
00199   }
00200   return code;
00201 }
00202 
00203 
00204 
00205 // Local Variables: **
00206 // c-basic-offset:2 **
00207 // indent-tabs-mode:nil **
00208 // End: **
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:07:36 2011 for RootRawWriter by doxygen 1.4.7