Ignore:
Timestamp:
11/13/02 17:03:19 (22 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc

    r1599 r1600  
    4242//
    4343/////////////////////////////////////////////////////////////////////////////
    44 
    4544#include "MCT1ReadPreProc.h"
    4645
     
    8180//
    8281MCT1ReadPreProc::MCT1ReadPreProc(const char *fname, const char *name,
    83                                  const char *title) : fIn(NULL)
    84 {
    85     fName  = name  ? name  : "MCT1ReadPreProc";
     82                                 const char *title) : fIn(NULL), fEntries(0)
     83{
     84    fName  = name  ? name  : "MRead";
    8685    fTitle = title ? title : "Task to loop over events in CT1 ascii file";
    8786
     
    113112void MCT1ReadPreProc::AddFile(const char *txt)
    114113{
    115     TNamed *name = new TNamed(txt, "");
    116     fFileNames->AddLast(name);
     114    ifstream *store = fIn;
     115
     116    fIn = new ifstream(gSystem->ExpandPathName(txt));
     117
     118    if (!(*fIn))
     119    {
     120        *fLog << warn << "Cannot open file '" << txt << "'... ignored." << endl;
     121        fIn = store;
     122        return;
     123    }
     124
     125    fEntries += GetNumEvents();
     126
     127    delete fIn;
     128
     129    fIn = store;
     130
     131    fFileNames->AddLast(new TNamed(txt, ""));
    117132}
    118133
     
    123138// data members
    124139//
    125 void MCT1ReadPreProc::ProcessHeader(const struct outputpars &outpars)
    126 {
    127     fNumPixels = outpars.inumpixels; // number of pixels in the camera
    128 
    129     if (fNumPixels>iMAXNUMPIX || fNumPixels==0)
    130         fNumPixels=iMAXNUMPIX;
     140void MCT1ReadPreProc::ProcessRunHeader(const struct outputpars &outpars)
     141{
     142    if (outpars.inumpixels != iMAXNUMPIX)
     143        *fLog << warn << "WARNING! File doesn't contain " << iMAXNUMPIX << " Pixels... maybe corrupt." << endl;
     144
     145    fNumEventsInRun = 0;
    131146
    132147    //
     
    166181    *fLog << "Source:    RA=" << outpars.dsourcera_hours << "h  DEC=";
    167182    *fLog << outpars.dsourcedec_deg << "deg" << endl;
    168 
    169     *fLog << "Pixels:    " << fNumPixels << endl;
    170183
    171184    //int     inummuonpixels;   // number of pixels in the muon shield
     
    262275
    263276    // FIXME? Is x-y echanged between Mars CT1 geometry and CT1 definition?
    264     fSrcPos->SetXY(outpars.fypointcorr_deg/fGeom->GetConvMm2Deg(),
    265                    outpars.fxpointcorr_deg/fGeom->GetConvMm2Deg());
     277    fSrcPos->SetXY(-outpars.fypointcorr_deg/fGeom->GetConvMm2Deg(),
     278                   -outpars.fxpointcorr_deg/fGeom->GetConvMm2Deg());
     279    fSrcPos->SetReadyToSave();
    266280
    267281    /*
     
    342356
    343357    fIsMcFile = outpars.bmontecarlo==TRUE;
     358
     359    fPedest->SetReadyToSave();
    344360}
    345361
     
    348364// Read CT1 PreProc File Header:
    349365//
    350 Bool_t MCT1ReadPreProc::ReadHeader()
     366Bool_t MCT1ReadPreProc::ReadRunHeader()
    351367{
    352368    char cheadertitle[iHEADERTITLELENGTH];
     
    381397    fIn->read((Byte_t*)&outpars, sizeof(struct outputpars));
    382398
    383     ProcessHeader(outpars);
     399    ProcessRunHeader(outpars);
    384400
    385401    return kTRUE;
    386402}
    387403
    388 Bool_t MCT1ReadPreProc::ReadFooter()
     404Bool_t MCT1ReadPreProc::ReadRunFooter()
    389405{
    390406    char cheadertitle[iHEADERTITLELENGTH];
     
    400416
    401417    if (!s.BeginsWith(m(0,p)))
    402         return kFALSE;
     418    {
     419        fIn->seekg(-iHEADERTITLELENGTH, ios::cur);
     420        return kFALSE;
     421    }
    403422
    404423    *fLog << inf << cheadertitle << flush;
     
    423442     */
    424443
     444    fNumFilterEvts += filterres.ifilter_passed_evts;
     445    fNumRuns++;
     446
     447    *fLog << inf << "Read " << fNumEventsInRun << " events from run." << endl;
     448
     449    if (fNumEventsInRun!=(UInt_t)filterres.ifilter_passed_evts)
     450    {
     451        *fLog << warn << "WARNING! Number of events in run doesn't match number of read events." << endl;
     452        *fLog << "         File might be corrupt." << endl;
     453    }
     454
    425455    return kTRUE;
    426456}
     
    468498    *fLog << inf << "-----------------------------------------------------------------------" << endl;
    469499
    470     //*fLog << "File contains " << GetNumEvents() << " events." << endl;
    471 
    472     Bool_t rc = ReadHeader();
     500    *fLog << "File contains " << GetNumEvents() << " events." << endl;
     501
     502    // WORKAROUND for not working seekg(0) in GetNumEvents
     503    fIn->close();
     504    fIn->open(gSystem->ExpandPathName(name));
     505
     506    Bool_t rc = ReadRunHeader();
    473507
    474508    if (!rc)
    475         *fLog << warn << "Unable to read header... skipping file." << endl;
     509        *fLog << warn << "Unable to read first run header... skipping file." << endl;
    476510
    477511    return rc;
     
    480514Int_t MCT1ReadPreProc::GetNumEvents()
    481515{
    482     Int_t n = 0;
     516    *fLog << inf << "Scanning file for size" << flush;
     517
     518    const TString m(cEND_EVENTS_TEMPLATE);
     519    const Int_t p = m.First('%');
     520    const TString test = m(0, p);
     521
     522    Int_t nevts = 0;
     523    Int_t nruns = 0;
    483524
    484525    while (!fIn->eof())
    485526    {
    486         if (fIn->get()!=cEND_EVENTS_TEMPLATE[0])
    487             continue;
    488 
    489         char cheadertitle[iHEADERTITLELENGTH];
    490         fIn->read(cheadertitle, iHEADERTITLELENGTH-1);
    491 
    492         TString s = cheadertitle;
    493         TString m = cEND_EVENTS_TEMPLATE;
    494         Int_t p = m.First('%');
    495 
    496         if (!s.BeginsWith(m(1,p-1)))
    497             continue;
    498 
    499         int num;
    500 
    501         sscanf(cheadertitle, &cEND_EVENTS_TEMPLATE[1], &num);
    502 
    503         n += num;
    504     }
    505 
    506     fIn->seekg(0);
    507 
    508     return n;
     527        fIn->seekg(iHEADERTITLELENGTH, ios::cur);
     528        fIn->seekg(sizeof(struct outputpars), ios::cur);
     529
     530        while (1)
     531        {
     532            if (fIn->peek()==cEND_EVENTS_TEMPLATE[0])
     533            {
     534                char cheadertitle[iHEADERTITLELENGTH];
     535                fIn->read(cheadertitle, iHEADERTITLELENGTH);
     536
     537                const TString s = cheadertitle;
     538                if (s.BeginsWith(test))
     539                {
     540                    fIn->seekg(sizeof(struct filterresults), ios::cur);
     541                    nruns++;
     542                    break;
     543                }
     544
     545                fIn->seekg(-iHEADERTITLELENGTH, ios::cur);
     546            }
     547
     548            fIn->seekg(sizeof(struct eventrecord), ios::cur);
     549            if (fIn->eof())
     550                break;
     551            nevts++;
     552        }
     553        *fLog << "." << flush;
     554    }
     555
     556    *fLog << "done." << endl;
     557    *fLog << "Found " << nevts << " events in " << nruns << " runs." << endl;
     558
     559    return nevts;
    509560}
    510561
     
    542593    //  look for the source position in the camera
    543594    //
    544     fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam");
     595    fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", "Source");
    545596    if (!fSrcPos)
    546597        return kFALSE;
     
    566617    if (!fMcTrig)
    567618        return kFALSE;
     619
     620    fNumFilterEvts = 0;
     621    fNumRuns       = 0;
    568622
    569623    return kTRUE;
     
    582636     int   isecfrac_200ns;     // fractional part of isecs_since_midday
    583637     short snot_ok_flags;      // the bits in these two bytes are flags for additional information on the event: Everything OK =: all Bits = 0
    584      int   ipreproc_alt_arcs;  // "should be" alt according to preproc (arcseconds)
    585      int   ipreproc_az_arcs;   // "should be" az according to preproc (arcseconds)
    586638
    587639     // for ALT-AZ mount telescopes: rotation angle of the field of
     
    602654     float fhourangle;
    603655     */
    604     /*
    605      *fLog << event.isecs_since_midday << "s ";
    606      *fLog << event.ipreproc_alt_arcs << "s ";
    607      *fLog << event.ipreproc_az_arcs << "s ";
    608      *fLog << event.ifieldrot_arcs << "s ";
    609      *fLog << event.fhourangle << endl;
    610      */
    611656
    612657    //
     
    614659    // too the list with it's id, number of photons and error
    615660    //
    616     fNphot->InitSize(fNumPixels);
     661    fNphot->InitSize(iMAXNUMPIX);
    617662
    618663    // number of photoelectrons measured in each pixel only the
    619664    // actual number of pixels (outputpars.inumpixels) is written out
    620665    // short spixsig_10thphot[iMAXNUMPIX];
    621     for (Int_t i=0; i<fNumPixels; i++)
    622     {
    623         if (event.spixsig_10thphot[i]>0)
    624             fNphot->AddPixel(i, 0.1*event.spixsig_10thphot[i],
    625                              (*fPedest)[i].GetMeanRms());
    626     }
     666    for (Int_t i=0; i<iMAXNUMPIX; i++)
     667    {
     668        if (event.spixsig_10thphot[i]==0)
     669            continue;
     670
     671        fNphot->AddPixel(i, 0.1*event.spixsig_10thphot[i],
     672                         (*fPedest)[i].GetMeanRms());
     673    }
     674    fNphot->SetReadyToSave();
     675
     676    // int ipreproc_alt_arcs; // "should be" alt according to preproc (arcseconds)
     677    // int ipreproc_az_arcs;  // "should be" az according to preproc (arcseconds)
    627678
    628679    fMcEvt->SetTheta(TMath::Pi()*(0.5-1./180*event.ialt_arcs/3600));   // altitude (arcseconds)
    629680    fMcEvt->SetPhi(TMath::Pi()/180*event.iaz_arcs/3600);               // azimuth (arcseconds)
     681
     682    fMcEvt->SetReadyToSave();
    630683
    631684    if (!fIsMcFile)
     
    636689    fMcEvt->SetImpact(event.imcimpact_m*100);    // simulated impact
    637690
    638     fMcTrig->SetFirstLevel(event.imctriggerflag); // MC data from Dorota get a triggerflag: 1 means triggered, 0 not. */
     691    fMcTrig->SetFirstLevel(event.imctriggerflag);    // MC data from Dorota get a triggerflag: 1 means triggered, 0 not. */
     692    fMcTrig->SetReadyToSave();
    639693
    640694    //float fmcsize_phel;   // Simulated SIZE
     
    655709
    656710    //
    657     // If the first character isn't the first of the footer it must be
    658     // an event
    659     //
    660     if (fIn->peek()!=cEND_EVENTS_TEMPLATE[0])
    661         return kTRUE;
    662 
    663     //
    664     // Remember the current position if the first character faked a
    665     // footer
    666     //
    667     const Int_t pos = fIn->tellg();
    668 
    669     //
    670     // Try reading the footer. If it isn't succefull jump back...
    671     // must be an event
    672     //
    673     if (!ReadFooter())
    674     {
    675         fIn->seekg(pos, ios::beg);
    676         return kTRUE;
    677     }
    678 
    679     *fLog << inf << "Footer found." << endl;
    680 
    681     //
    682     // No after reading the footer check if we reached the end of the file
    683     //
    684     if (fIn->eof())
    685     {
    686         *fLog << "End of file." << endl;
    687         return kFALSE;
    688     }
    689 
    690     //
    691     // If the eof isn't reached a new header must follow. Check for it.
    692     //
    693     if (fIn->peek()!=cTITLE_TEMPLATE[0])
    694     {
    695         *fLog << inf << "Error finding new run header in file (possible EOF)... skipping rest of file." << endl;
    696         return kFALSE;
    697     }
    698 
    699     *fLog << "-----------------------------------------------------------------------" << endl;
    700     if (ReadHeader())
    701         return kTRUE;
    702 
    703     *fLog << inf << "Error reading run header in file... skipping rest of file." << endl;
    704     return kFALSE;
     711    // Because we can have 0-event runs in the file we loop as often
     712    // as we don't find a new footer-header combination.
     713    //
     714    while (1)
     715    {
     716        //
     717        // If the first character isn't the first of the footer it must be
     718        // an event
     719        //
     720        if (fIn->peek()!=cEND_EVENTS_TEMPLATE[0])
     721            return kTRUE;
     722
     723        //
     724        // Try reading the footer. If it isn't succefull...
     725        // must be an event
     726        //
     727        if (!ReadRunFooter())
     728            return kTRUE;
     729
     730        *fLog << inf << "Footer found." << endl;
     731
     732        //
     733        // No after reading the footer check if we reached the end of the file
     734        //
     735        if (fIn->eof())
     736        {
     737            *fLog << "End of file." << endl;
     738            return kFALSE;
     739        }
     740
     741        //
     742        // If the eof isn't reached a new header must follow. Check for it.
     743        //
     744        if (fIn->peek()!=cTITLE_TEMPLATE[0])
     745        {
     746            *fLog << inf << "Error finding new run header in file (possible EOF)... skipping rest of file." << endl;
     747            return kFALSE;
     748        }
     749
     750        *fLog << "-----------------------------------------------------------------------" << endl;
     751        if (!ReadRunHeader())
     752            return kTRUE;
     753    }
    705754}
    706755
     
    726775    struct eventrecord event;
    727776
    728     const int size1 = sizeof(event)-sizeof(event.spixsig_10thphot);
    729     const int size2 = sizeof(event.spixsig_10thphot[0])*fNumPixels;
    730 
    731     // read the eventrecord with the recorded number of stored pixels
    732     fIn->read((Byte_t*)&event, size1+size2);
    733 
    734     if (fIn->eof())
    735         return kFALSE;
     777    // read the eventrecord from the file
     778    fIn->read((Byte_t*)&event, sizeof(struct eventrecord));
    736779
    737780    ProcessEvent(event);
    738781
    739     fNumEvent++;
     782    fNumEvents++;
     783    fNumEventsInRun++;
    740784
    741785    return kTRUE;
    742786}
    743787
     788Bool_t MCT1ReadPreProc::PostProcess()
     789{
     790    *fLog << all;
     791    *fLog << "Number events passed the filter: " << fNumFilterEvts << endl;
     792    *fLog << "Number of Events read from file: " << fNumEvents << endl;
     793    *fLog << "Number of Runs read from file:   " << fNumRuns << endl;
     794
     795    if (fNumEvents!=fNumFilterEvts)
     796    {
     797        *fLog << warn << "WARNING! Number of events in file doesn't match number of read events..." << endl;
     798        *fLog << "         File might be corrupt." << endl;
     799    }
     800
     801    return kTRUE;
     802}
Note: See TracChangeset for help on using the changeset viewer.