00001
00103 #include <iostream>
00104 #include <iomanip>
00105 #include <string>
00106
00107 #include <getopt.h>
00108
00109 #include "TDragonRun.h"
00110 #include "TMidasEvent.h"
00111 #include "TDragonEvent.h"
00112 #include "TScalerEvent.h"
00113 #include "TEpicsEvent.h"
00114
00115 using std::cout;
00116 using std::cerr;
00117 using std::endl;
00118 using std::hex;
00119 using std::setw;
00120 using std::vector;
00121
00122 Int_t main(Int_t argc, Char_t **argv)
00123 {
00137 Int_t c;
00138 UInt_t loc;
00139 UInt_t nonOptions;
00140 string inFile;
00141 string outFile;
00142 string database;
00143
00144 while (1) {
00145
00146
00147
00148 static struct option longOptions[] = {
00149 {"help", 0, 0, 'h'},
00150 {"infile", 1, 0, 'i'},
00151 {"outfile", 1, 0, 'o'},
00152 {"database", 1, 0, 'd'},
00153 {0, 0, 0, 0}
00154 };
00155
00156 c = getopt_long(argc, argv, "hi:o:d:", longOptions, 0);
00157 if (argc == 1)
00158 c = 'h';
00159 else if (c == -1)
00160 break;
00161
00162 switch (c) {
00163 case 'i':
00164 inFile = optarg;
00165 break;
00166
00167 case 'o':
00168 outFile = optarg;
00169 break;
00170
00171 case 'd':
00172 database = optarg;
00173 break;
00174
00175 default:
00176 case 'h':
00177 cout << "Usage: makeDragonTree infile [outfile] [database]" << endl
00178 << "Utility to convert Dragon MIDAS data into ROOT trees." << endl
00179 << endl << "Options:" << endl
00180 << " -h, --help print this help message" << endl
00181 << " -i, --infile input MIDAS file to read" << endl
00182 << " -o, --outfile output ROOT file to write" << endl
00183 << " -d, --database which MySQL database to use" << endl
00184 << endl
00185 << "For example, to convert run15.mid using hardware " << endl
00186 << "information on gandalf.triumf.ca, do:" << endl << endl
00187 << "makeDragonTree run15.mid run15.root "
00188 << "mysql://gandalf.triumf.ca:3306/Online" << endl << endl
00189 << "Report bugs to <jonty@triumf.ca>." << endl;
00190 exit(0);
00191 }
00192 }
00193
00194
00195
00196
00197
00198 nonOptions = argc - optind;
00199 if (! inFile.empty() && outFile.empty()) {
00200 outFile = inFile;
00201 loc = outFile.find_last_of(".");
00202 if (loc == string::npos)
00203 loc = outFile.size();
00204 outFile.replace(loc, outFile.size(), ".root");
00205 }
00206 if (nonOptions >= 4 || ! outFile.empty() &&
00207 inFile.empty() && nonOptions < 1) {
00208 cerr << "makeDragonTree: ERROR - incorrect input parameters" << endl;
00209 exit(0);
00210 } else if (nonOptions >= 1 && inFile.empty()) {
00211 inFile = argv[optind];
00212 if (nonOptions >= 2 && outFile.empty())
00213 outFile = argv[optind + 1];
00214 else if (outFile.empty()) {
00215 outFile = inFile;
00216 loc = outFile.find_last_of(".");
00217 if (loc == string::npos)
00218 loc = outFile.size();
00219 outFile.replace(loc, outFile.size(), ".root");
00220 }
00221 if (nonOptions >= 3 && database.empty())
00222 database = argv[optind + 2];
00223 }
00224
00225
00226
00227
00228 TDragonRun dr;
00229 dr.OpenMidasFile(inFile.c_str());
00230 dr.OpenRootFile(outFile.c_str());
00231
00232
00233
00234
00235 vector<UShort_t> slots[4];
00236 if (! database.empty())
00237 dr.SetDatabase(database.c_str());
00238 dr.GetModules(&slots[0], "'Heavy-ion'", "'adc'");
00239 dr.GetModules(&slots[1], "'Heavy-ion'", "'tdc'");
00240 dr.GetModules(&slots[2], "'Gamma-ray'", "'adc'");
00241 dr.GetModules(&slots[3], "'Gamma-ray'", "'tdc'");
00242 dr.SetHiModules(&(slots[0].front()), slots[0].size(),
00243 &(slots[1].front()), slots[1].size());
00244 dr.SetGrModules(&(slots[2].front()), slots[2].size(),
00245 &(slots[3].front()), slots[3].size());
00246
00247 Short_t eventId;
00248 Bool_t haveBor = 0;
00249 Bool_t haveEor = 0;
00250 ULong_t unknownN = 0;
00251 ULong_t totalN = 0;
00252 ULong_t epicsGoodN = 0, epicsBadN = 0;
00253 ULong_t dragonGoodN = 0, dragonBadN = 0;
00254 ULong_t scalerGoodN = 0, scalerBadN = 0;
00255
00256
00257
00258
00259 while (dr.ReadMidasEvent()) {
00260 eventId = dr.GetMidasEvent()->GetEventId();
00261
00262
00263
00264
00265
00266
00267
00268 switch (eventId) {
00269 case TDragonRun::kEV_DRAGON_G:
00270 case TDragonRun::kEV_DRAGON_H:
00271 totalN++;
00272 if (! dr.UnpackDragonEvent()) {
00273 dragonBadN++;
00274 cerr << "makeDragonTree: ERROR - cannot unpack dragon event "
00275 << dr.GetMidasEvent()->GetSerialNumber() << endl;
00276 } else {
00277 dragonGoodN++;
00278 dr.WriteTree();
00279 dr.GetDragonEvent()->Clear();
00280 }
00281 break;
00282 case TDragonRun::kEV_SCALER:
00283 totalN++;
00284 if (! dr.UnpackScalerEvent()) {
00285 scalerBadN++;
00286 cerr << "makeDragonTree: ERROR - cannot unpack scaler event "
00287 << dr.GetMidasEvent()->GetSerialNumber() << endl;
00288 } else {
00289 scalerGoodN++;
00290 dr.WriteTree();
00291 dr.GetScalerEvent()->Clear();
00292 }
00293 break;
00294 case TDragonRun::kEV_EPICS:
00295 totalN++;
00296 if (! dr.UnpackEpicsEvent()) {
00297 epicsBadN++;
00298 cerr << "makeDragonTree: ERROR - cannot unpack epics event "
00299 << dr.GetMidasEvent()->GetSerialNumber() << endl;
00300 } else {
00301 epicsGoodN++;
00302 dr.WriteTree();
00303 dr.GetEpicsEvent()->Clear();
00304 }
00305 break;
00306 case TDragonRun::kEV_BOR:
00307 cout << "Begin of run..." << endl;
00308 haveBor = true;
00309 break;
00310 case TDragonRun::kEV_EOR:
00311 cout << "End of run..." << endl;
00312 haveEor = true;
00313 break;
00314 default:
00315 totalN++;
00316 unknownN++;
00317 cerr << "makeDragonTree: ERROR - unknown event id 0x" << hex << eventId
00318 << " with header:" << endl;
00319 dr.GetMidasEvent()->Print();
00320 break;
00321 }
00322
00323
00324
00325 dr.GetMidasEvent()->Clear();
00326 }
00327
00328
00329
00330 dr.CloseMidasFile();
00331 dr.CloseRootFile();
00332 if (! haveBor)
00333 cerr << "makeDragonTree: ERROR - no begin-of-run header,"
00334 << " file conversion may be incorrect" << endl;
00335 if (! haveEor)
00336 cerr << "makeDragonTree: ERROR - no end-of-run header,"
00337 << " file conversion may be incomplete" << endl;
00338 dr.TestDragonTree();
00339 cout.fill(' ');
00340 cout << "Total events " << dec << totalN << ":" << endl
00341 << "Event type: Good: Bad:" << endl
00342 << setw(11) << "dragon" << setw(9) << dragonGoodN
00343 << setw(9) << dragonBadN << endl
00344 << setw(11) << "scalers" << setw(9) << scalerGoodN
00345 << setw(9) << scalerBadN << endl
00346 << setw(11) << "epics" << setw(9) << epicsGoodN
00347 << setw(9) << epicsBadN << endl
00348 << setw(11) << "unknown" << setw(18) << unknownN << endl;
00349 return ! haveBor || ! haveEor;
00350 }