/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/UTIL/lStdHep.cc

Go to the documentation of this file.
00001 //// lStdHep.cc
00002 //
00003 // This class is based on the light-weight XDR class lXDR,
00004 // and parses/writes StdHep files. It was mainly written,
00005 // to provide a faster alternative to the more cumbersome
00006 // methods using mcfio in StdHep.
00007 //
00008 // W.G.J. Langeveld, 24 May 2002
00009 //
00010 ////
00011 #include "UTIL/lStdHep.hh"
00012 #include "string.h"
00013 #include "stdlib.h"
00014 
00015 namespace UTIL{
00016 
00017 ////
00018 //
00019 // The main lStdHep class.
00020 //
00021 ////
00022 
00023 //
00024 // Constructors, destructor
00025 // ------------------------
00026 // Constructor opens file, destructor closes file. Once opened for
00027 // reading, the file cannot be written to, and v.v.
00028 //
00029 lStdHep::lStdHep(const char *filename, bool open_for_write) : lXDR(filename, open_for_write),
00030    version(0), title(0), comment(0), date(0), closingDate(0), blockIds(0), blockNames(0)
00031 {
00032    if (open_for_write) {
00033       setError(LSH_NOTSUPPORTED);
00034    }
00035    else {
00036       readFileHeader();
00037    }
00038    return;
00039 }
00040 
00041 lStdHep::~lStdHep()
00042 {
00043    delete [] version;
00044    delete [] date;
00045    delete [] closingDate;
00046    delete [] comment;
00047    delete [] title;
00048    delete [] blockIds;
00049    if (blockNames) {
00050       for (int i = 0; i < nBlocks; i++) {
00051          delete [] blockNames[i];
00052       }
00053       delete [] blockNames;
00054    }
00055    return;
00056 }
00057 
00058 void lStdHep::printFileHeader(FILE *fp)
00059 {
00060    if (fp == 0) fp = stdout;
00061 
00062    fprintf(fp, "====== File Header ===========\n");
00063    fprintf(fp, "    total blocks: %ld\n", ntot);
00064    fprintf(fp, "         version: %s\n",  version);
00065    fprintf(fp, "           title: %s\n",  title);
00066    fprintf(fp, "         comment: %s\n",  comment);
00067    fprintf(fp, "            date: %s",    date);
00068    fprintf(fp, "    closing date: %s",    closingDate);
00069    fprintf(fp, " expected events: %ld\n", numevts_expect);
00070    fprintf(fp, "          events: %ld\n", numevts);
00071    fprintf(fp, "      firstTable: %ld\n", firstTable);
00072    fprintf(fp, "        dimTable: %ld\n", dimTable);
00073    fprintf(fp, "        nNTuples: %ld\n", nNTuples);
00074    fprintf(fp, "         nBlocks: %ld\n", nBlocks);
00075    if (nBlocks) fprintf(fp, "     block names:\n");
00076    for (int i = 0; i < nBlocks; i++) {
00077       fprintf(fp, "                : %s\n", blockNames[i]);
00078    }
00079    fprintf(fp, "=============================\n");
00080    return;
00081 }
00082 
00083 void lStdHep::printEventTable(FILE *fp)
00084 {
00085    if (fp == 0) fp = stdout;
00086    eventTable.print(fp);
00087    return;
00088 }
00089 
00090 void lStdHep::printEventHeader(FILE *fp)
00091 {
00092    if (fp == 0) fp = stdout;
00093    event.printHeader(fp);
00094    return;
00095 }
00096 
00097 void lStdHep::printEvent(FILE *fp)
00098 {
00099    if (fp == 0) fp = stdout;
00100    event.print(fp);
00101    return;
00102 }
00103 
00104 void lStdHep::printTrack(int i, FILE *fp)
00105 {
00106    if (fp == 0) fp = stdout;
00107    if (i < event.nhep) {
00108       fprintf(fp, "    Track: id: %ld, vtx: (%g, %g, %g, %g), mom: (%g, %g, %g, %g, %g)\n",
00109                    pid(i), X(i), Y(i), Z(i), T(i), Px(i), Py(i), Pz(i), E(i), M(i));
00110       fprintf(fp, "    Track: wgt: %g, alpha QED: %g, alpha QCD: %g, idrup: %ld\n",
00111                    eventweight(), alphaQED(), alphaQCD(), idrup());
00112    }
00113    return;
00114 }
00115 
00116 void lStdHep::printBeginRunRecord(FILE *fp)
00117 {
00118    if (fp == 0) fp = stdout;
00119    return;
00120 }
00121 
00122 void lStdHep::printEndRunRecord(FILE *fp)
00123 {
00124    if (fp == 0) fp = stdout;
00125    return;
00126 }
00127 
00128 bool lStdHep::more(void)
00129 {
00130    return(getError() == LSH_SUCCESS);
00131 }
00132 
00133 long lStdHep::readEvent(void)
00134 {
00135 //
00136 // Look for an event or an event table
00137 //
00138    event.isEmpty = 1;
00139    while (1) {
00140       if (eventTable.ievt < eventTable.numEvts) {
00141          if (filePosition(eventTable.ptrEvents[eventTable.ievt]) !=
00142                           eventTable.ptrEvents[eventTable.ievt]) return(getError());
00143 
00144          if (event.read(*this) != LSH_SUCCESS) return(getError());
00145          eventTable.ievt++;
00146 
00147          if (event.isEmpty) continue;
00148          return(getError());
00149       }
00150 
00151       eventTable.isEmpty = 1;
00152       while (eventTable.isEmpty) {
00153          if (eventTable.nextlocator == -2) {
00154 //
00155 // This was the last event table, signal quitting. Not an error.
00156 //
00157             setError(LSH_ENDOFFILE);
00158             return(getError());
00159          }
00160          else if (eventTable.nextlocator == -1) {
00161             setError(LSH_EVTABLECORRUPT);
00162             return(getError());
00163          }
00164 //
00165 // Go to the next event table
00166 //
00167          if (filePosition(eventTable.nextlocator) != eventTable.nextlocator) return(getError());
00168          if (eventTable.read(*this) != LSH_SUCCESS) return(getError());
00169       }
00170    }
00171    return(getError());
00172 }
00173 
00174 long lStdHep::getEvent(lStdEvent &lse) const
00175 {
00176    if (long status = getError() != LSH_SUCCESS) return(status);
00177 
00178    lse.evtNum  = event.nevhep;
00179 
00180    lse.clear();
00181    for (int i = 0; i < event.nhep; i++) {
00182       lStdTrack lst;
00183       lst.X         = X(i);
00184       lst.Y         = Y(i);
00185       lst.Z         = Z(i);
00186       lst.T         = T(i);
00187       lst.Px        = Px(i);
00188       lst.Py        = Py(i);
00189       lst.Pz        = Pz(i);
00190       lst.E         = E(i);
00191       lst.M         = M(i);
00192       lst.pid       = pid(i);
00193       lst.status    = status(i);
00194       lst.mother1   = mother1(i);
00195       lst.mother2   = mother2(i);
00196       lst.daughter1 = daughter1(i);
00197       lst.daughter2 = daughter2(i);
00198       lse.push_back(lst);
00199    }
00200    return(LSH_SUCCESS);
00201 }
00202 
00203 long lStdHep::readEvent(lStdEvent &lse)
00204 {
00205    long status = readEvent();
00206    if (status != LSH_SUCCESS) return(status);
00207    return(getEvent(lse));
00208 }
00209 
00210 long lStdHep::readFileHeader(void)
00211 {
00212    long len, blockid;
00213 
00214    blockid   = readLong();
00215    if (blockid != LSH_FILEHEADER) {
00216       setError(LSH_BLOCKERROR);
00217       return(getError());
00218    }
00219    ntot      = readLong();
00220    version   = readString(len);
00221 
00222    title     = readString(len);
00223    comment   = readString(len);
00224    date      = readString(len);
00225    if ((strcmp(version, "2.00") == 0) || (strcmp(version, "1.00") == 0)) {
00226       closingDate = new char[len + 1];
00227       strcpy((char *) closingDate, date);
00228    }
00229    else {
00230       closingDate = readString(len);
00231    }
00232 
00233    numevts_expect = readLong();
00234    numevts        = readLong();
00235    firstTable     = readLong();
00236    dimTable       = readLong();
00237    nBlocks        = readLong();
00238    if (*version != '2') {
00239       nNTuples    = 0;
00240    }
00241    else {
00242       nNTuples     = readLong();
00243    }
00244 
00245    blockIds       = readLongArray(nBlocks);
00246    blockNames     = new const char *[nBlocks];
00247    for (int i = 0; i < nBlocks; i++) blockNames[i] = readString(len);
00248    if (nNTuples > 0) setError(LSH_NOTSUPPORTED);
00249 //
00250 // Read the first event table
00251 //
00252    eventTable.read(*this);
00253    return(getError());
00254 }
00255 
00256 long lStdHep::writeEvent(void)
00257 {
00258    return(LSH_NOTSUPPORTED);
00259 }
00260 
00261 long lStdHep::setEvent(const lStdEvent &lse)
00262 {
00263 // ***************set up event buffer!
00264    setNTracks(lse.size());
00265    setEvtNum(lse.evtNum);
00266 
00267    for (int i = 0; i < event.nhep; i++) {
00268       setX        (i, lse[i].X);
00269       setY        (i, lse[i].Y);
00270       setZ        (i, lse[i].Z);
00271       setT        (i, lse[i].T);
00272       setPx       (i, lse[i].Px);
00273       setPy       (i, lse[i].Py);
00274       setPz       (i, lse[i].Pz);
00275       setE        (i, lse[i].E);
00276       setM        (i, lse[i].M);
00277       setPid      (i, lse[i].pid);
00278       setStatus   (i, lse[i].status);
00279       setMother1  (i, lse[i].mother1);
00280       setMother2  (i, lse[i].mother2);
00281       setDaughter1(i, lse[i].daughter1);
00282       setDaughter2(i, lse[i].daughter2);
00283    }
00284    return(LSH_SUCCESS);
00285 }
00286 
00287 long lStdHep::writeEvent(lStdEvent &lse)
00288 {
00289    long status = writeEvent();
00290    if (status != LSH_SUCCESS) return(status);
00291    return(setEvent(lse));
00292 }
00293 
00294 lStdHep::EventTable::EventTable() :
00295    isEmpty(1), ievt(0), blockid(0), ntot(0), version(0),
00296    nextlocator(-3), numEvts(0), evtnums(0),
00297    storenums(0), runnums(0), trigMasks(0), ptrEvents(0)
00298 {
00299    return;
00300 }
00301 
00302 lStdHep::EventTable::~EventTable()
00303 {
00304    cleanup();
00305    return;
00306 }
00307 
00308 void lStdHep::EventTable::cleanup(void)
00309 {
00310    delete [] version;    version   = 0;
00311    delete [] evtnums;    evtnums   = 0;
00312    delete [] storenums;  storenums = 0;
00313    delete [] runnums;    runnums   = 0;
00314    delete [] trigMasks;  trigMasks = 0;
00315    delete [] ptrEvents;  ptrEvents = 0;
00316    isEmpty = 1;
00317    ievt    = ntot = blockid = numEvts = 0; // leave nextlocator alone!
00318    return;
00319 }
00320 
00321 long lStdHep::EventTable::read(lStdHep &ls)
00322 {
00323    long len;
00324 
00325    cleanup();
00326 
00327    blockid  = ls.readLong();
00328    ntot     = ls.readLong();
00329    version  = ls.readString(len);
00330 
00331    if (blockid != LSH_EVENTTABLE) {
00332       ls.setError(LSH_NOEVENTTABLE);
00333       return(ls.getError());
00334    }
00335    nextlocator = ls.readLong();
00336    numEvts     = ls.readLong();
00337    evtnums     = ls.readLongArray(len);
00338    storenums   = ls.readLongArray(len);
00339    runnums     = ls.readLongArray(len);
00340    trigMasks   = ls.readLongArray(len);
00341    ptrEvents   = ls.readLongArray(len);
00342    if (numEvts > 0) isEmpty = 0;
00343    return(ls.getError());
00344 }
00345 
00346 long lStdHep::EventTable::print(FILE *fp)
00347 {
00348    fprintf(fp, " EventTable: blockid: %ld, ntot: %ld, version: %s\n", blockid, ntot, version);
00349    fprintf(fp, " EventTable: nextlocator: %ld, numEvts: %ld\n", nextlocator, numEvts);
00350    for (int i = 0; i < numEvts; i++) {
00351       fprintf(fp, " EventTable: %d: evtnums %ld storenums %ld runnums %ld trigMasks %ld ptrEvents %ld\n",
00352                i, evtnums[i], storenums[i], runnums[i], trigMasks[i], ptrEvents[i]);
00353       if (i == 10) {
00354          fprintf(fp, " EventTable: etc.\n");
00355          break;
00356       }
00357    }
00358    return(0);
00359 }
00360 
00361 lStdHep::Event::Event() :
00362    isEmpty(1), blockid(0), ntot(0), version(0), blockIds(0),
00363    ptrBlocks(0), nevhep(0), nhep(0), isthep(0), idhep(0),
00364    jmohep(0), jdahep(0), phep(0), vhep(0), eventweight(0.0),
00365    alphaqed(0.0), alphaqcd(0.0), scale(0), spin(0), colorflow(0),
00366    idrup(0)
00367 {
00368    return;
00369 }
00370 
00371 lStdHep::Event::~Event()
00372 {
00373    cleanup();
00374 }
00375 
00376 void lStdHep::Event::cleanup(void)
00377 {
00378    delete [] version;     version   = 0;
00379    delete [] ptrBlocks;   ptrBlocks = 0;
00380    delete [] blockIds;    blockIds  = 0;
00381    delete [] isthep;      isthep    = 0;
00382    delete [] idhep;       idhep     = 0;
00383    delete [] jmohep;      jmohep    = 0;
00384    delete [] jdahep;      jdahep    = 0;
00385    delete [] phep;        phep      = 0;
00386    delete [] vhep;        vhep      = 0;
00387    delete [] scale;       scale     = 0;
00388    delete [] spin;        spin      = 0;
00389    delete [] colorflow;   colorflow = 0;
00390    blockid = ntot = nevhep = nhep = 0;
00391    isEmpty = 1;
00392    return;
00393 }
00394 
00395 long lStdHep::Event::read(lStdHep &ls)
00396 {
00397 //
00398 // Read event header
00399 //
00400    long len;
00401 
00402    cleanup();
00403 
00404    blockid = ls.readLong();
00405    ntot    = ls.readLong();
00406    version = ls.readString(len);
00407    if (blockid != LSH_EVENTHEADER) ls.setError(LSH_NOEVENT);
00408 
00409    evtnum    = ls.readLong();
00410    storenum  = ls.readLong();
00411    runnum    = ls.readLong();
00412    trigMask  = ls.readLong();
00413    nBlocks   = ls.readLong();
00414    dimBlocks = ls.readLong();
00415 
00416    if (*version == '2') {
00417       nNTuples = ls.readLong();
00418       dimNTuples = ls.readLong();
00419       if (dimBlocks) {
00420          blockIds  = ls.readLongArray(len);
00421          ptrBlocks = ls.readLongArray(len);
00422       }
00423       if (dimNTuples) {
00424          ls.setError(LSH_NOTSUPPORTED);
00425          return(ls.getError());
00426       }
00427    }
00428    else {
00429       nNTuples   = 0;
00430       dimNTuples = 0;
00431       blockIds   = ls.readLongArray(len);
00432       ptrBlocks  = ls.readLongArray(len);
00433    }
00434 //
00435 // Read event
00436 //
00437    for (int i = 0; i < nBlocks; i++) {
00438       blockid = ls.readLong();
00439       ntot    = ls.readLong();
00440       if (version) delete [] version;
00441       version = ls.readString(len);
00442 
00443       isEmpty = 0;
00444       switch (blockIds[i]) {
00445          case LSH_STDHEP          : // 101
00446             nevhep = ls.readLong();
00447             nhep   = ls.readLong();
00448             if (isthep) delete [] isthep;
00449             isthep = ls.readLongArray(len);
00450             if (idhep)  delete [] idhep;
00451             idhep  = ls.readLongArray(len);
00452             if (jmohep) delete [] jmohep;
00453             jmohep = ls.readLongArray(len);
00454             if (jdahep) delete [] jdahep;
00455             jdahep = ls.readLongArray(len);
00456             if (phep)   delete [] phep;
00457             phep   = ls.readDoubleArray(len);
00458             if (vhep)   delete [] vhep;
00459             vhep   = ls.readDoubleArray(len);
00460             break;
00461          case LSH_STDHEPEV4       : // 201
00462             nevhep = ls.readLong();
00463             nhep   = ls.readLong();
00464             if (isthep) delete [] isthep;
00465             isthep = ls.readLongArray(len);
00466             if (idhep)  delete [] idhep;
00467             idhep  = ls.readLongArray(len);
00468             if (jmohep) delete [] jmohep;
00469             jmohep = ls.readLongArray(len);
00470             if (jdahep) delete [] jdahep;
00471             jdahep = ls.readLongArray(len);
00472             if (phep)   delete [] phep;
00473             phep   = ls.readDoubleArray(len);
00474             if (vhep)   delete [] vhep;
00475             vhep   = ls.readDoubleArray(len);
00476 //
00477 // New stuff for STDHEPEV4:
00478 //
00479             eventweight = ls.readDouble();
00480             alphaqed    = ls.readDouble();
00481             alphaqcd    = ls.readDouble();
00482             if (scale) delete [] scale;
00483             scale       = ls.readDoubleArray(len);
00484             if (spin)  delete [] spin;
00485             spin        = ls.readDoubleArray(len);
00486             if (colorflow) delete [] colorflow;
00487             colorflow   = ls.readLongArray(len);
00488             idrup       = ls.readLong();
00489             break;
00490          case LSH_OFFTRACKARRAYS  : // 102
00491          case LSH_OFFTRACKSTRUCT  : // 103
00492          case LSH_TRACEARRAYS     : // 104
00493          case LSH_STDHEPM         : // 105
00494             break;
00495          case LSH_STDHEPBEG       : // 106
00496             bnevtreq  = ls.readLong();
00497             bnevtgen  = ls.readLong();
00498             bnevtwrt  = ls.readLong();
00499             bstdecom  = ls.readFloat();
00500             bstdxsec  = ls.readFloat();
00501             bstdseed1 = ls.readDouble();
00502             bstdseed2 = ls.readDouble();
00503             isEmpty  = 1;
00504             break;
00505          case LSH_STDHEPEND       : // 107
00506             enevtreq  = ls.readLong();
00507             enevtgen  = ls.readLong();
00508             enevtwrt  = ls.readLong();
00509             estdecom  = ls.readFloat();
00510             estdxsec  = ls.readFloat();
00511             estdseed1 = ls.readDouble();
00512             estdseed2 = ls.readDouble();
00513             isEmpty = 1;
00514             break;
00515          case LSH_STDHEPCXX       : // 108
00516             break;
00517       }
00518    }
00519    return(ls.getError());
00520 }
00521 
00522 long lStdHep::Event::printHeader(FILE *fp)
00523 {
00524    fprintf(fp, "  EventHeader: blockid: %ld, ntot: %ld, version: %s\n", blockid, ntot, version);
00525    fprintf(fp, "             : evtnum: %ld, storenum: %ld, runnum: %ld, trigMask: %ld, nBlocks: %ld, dimBlocks: %ld\n",
00526            evtnum, storenum, runnum, trigMask, nBlocks, dimBlocks);
00527    fprintf(fp, "             : nNTuples: %ld, dimNTuples: %ld\n", nNTuples, dimNTuples);
00528 
00529    for (int i = 0; i < nBlocks; i++) {
00530       const char *labels[10] = {"Event", "Off-track arrays", "Off-track struct", "Trace arrays",
00531                                 "Event with multiple interactions", "Begin run", "End run", "StdHepCXX",
00532                                 "EventV4", "Unknown" };
00533       int j = blockIds[i] - 101;
00534       if (blockIds[i] == LSH_STDHEPEV4) j = 8;
00535       if ((j < 0) || (j > 9)) j = 9;
00536       fprintf(fp, "             : %d: blockIds %ld (%s) ptrBlocks %ld\n",
00537               i, blockIds[i], labels[j], ptrBlocks[i]);
00538    }
00539    return(0);
00540 }
00541 
00542 long lStdHep::Event::print(FILE *fp)
00543 {
00544    fprintf(fp, "   Event: nevhep: %ld, nhep: %ld\n", nevhep, nhep);
00545    return(0);
00546 }
00547 
00548 }

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7