Ignore:
Timestamp:
06/21/12 11:58:54 (12 years ago)
Author:
tbretz
Message:
Added code to correct for the drs steps; added WriteFitsImp and code to process a sliding average.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/DrsCalib.h

    r14101 r14197  
    66
    77#include "fits.h"
     8#include "ofits.h"
     9
     10#ifdef __MARS__
     11#include "MTime.h"
     12#endif
    813
    914class DrsCalibrate
     
    212217    static double FindStep(const size_t ch0, const float *vec, int16_t roi, const int16_t pos, const uint16_t *map)
    213218    {
    214         if (pos<=1 || pos>=roi)
     219        // We have about 1% of all cases which are not ahndled here,
     220        // because the baseline jumps up just before the readout window
     221        // and down just after it. In this cases we could determine the jump
     222        // from the board time difference or throw the event away.
     223        if (pos==0 || pos>=roi)
    215224            return 0;
    216225
    217226        double step = 0; // before
     227        double rms  = 0; // before
     228        int    cnt  = 0;
    218229
    219230        // Exclude TM channel
     
    223234            const size_t sw = map[hw]*roi + pos;
    224235
    225             step += vec[sw]-vec[sw-1];
    226         }
    227 
    228         return step/8;
    229     }
    230 
    231     static void SubtractStep(const size_t ch0, const double avg, float *vec, int16_t roi, uint32_t dist, const uint16_t *map)
    232     {
    233         const int begin  = avg>0 ? dist :    0;
    234         const int end    = avg>0 ?  roi : dist;
     236            const double diff = vec[sw]-vec[sw-1];
     237
     238            step += diff;
     239            rms  += (vec[sw]-vec[sw-1])*(vec[sw]-vec[sw-1]);
     240
     241            cnt++;
     242        }
     243
     244        return cnt==0 ? 0 : step/cnt;
     245    }
     246
     247    static void SubtractStep(const size_t ch0, const double avg, float *vec, int16_t roi, int32_t pos, const uint16_t *map)
     248    {
     249        if (pos==0 || pos>=roi)
     250            return;
     251
     252        const int begin = avg>0 ? pos :   0;
     253        const int end   = avg>0 ? roi : pos;
    235254
    236255        const double sub = fabs(avg);
     
    255274        double   pos;
    256275        uint16_t cnt;
     276
     277        static bool sort(const Step &s, const Step &r) { return s.avg<r.avg; }
    257278    };
     279
     280    static Step AverageSteps(const std::vector<Step> &list, uint32_t n)
     281    {
     282        Step rc;
     283        for (auto it=list.begin(); it!=list.begin()+n; it++)
     284        {
     285            rc.pos += it->pos;
     286            rc.avg += it->avg;
     287            rc.rms += it->avg*it->avg;
     288        }
     289
     290        rc.cnt = n;
     291
     292        rc.pos /= rc.cnt;
     293        rc.avg /= rc.cnt;
     294        rc.rms /= rc.cnt;
     295
     296        rc.rms = sqrt(rc.rms-rc.avg*rc.avg);
     297
     298        return rc;
     299    }
     300
    258301
    259302    static Step CorrectStep(float *vec, uint16_t nch, uint16_t roi,
     
    261304                            const int16_t offset, const uint16_t *map)
    262305    {
    263         Step rc;
    264 
     306
     307        std::vector<Step> list;
     308
     309        // Fill steps into array
     310        // Exclude broken pixels?
     311        // Remove maximum and minimum patches (4max and 4min)?
    265312        for (size_t ch=0; ch<nch; ch += 9)
    266313        {
     
    273320                continue;
    274321
    275             rc.pos += dist;
    276             rc.avg += step;
    277             rc.rms += step*step;
    278             rc.cnt++;
    279         }
    280 
    281         if (rc.cnt==0 || rc.avg==0)
     322            Step rc;
     323            rc.pos = dist;
     324            rc.avg = step;
     325            list.push_back(rc);
     326        }
     327
     328        if (list.size()==0)
    282329            return Step();
    283330
    284         rc.pos /= rc.cnt;
    285         rc.avg /= rc.cnt;
    286         rc.rms /= rc.cnt;
    287 
    288         rc.rms = sqrt(rc.rms-rc.avg*rc.avg);
     331        Step rc = AverageSteps(list, list.size());;
     332
     333        if (rc.avg==0)
     334            return Step();
     335
     336        if (rc.rms>5)
     337        {
     338            partial_sort(list.begin(), list.begin()+list.size()/2,
     339                         list.end(), Step::sort);
     340
     341            rc = AverageSteps(list, list.size()/2);
     342        }
    289343
    290344        for (size_t ch=0; ch<nch; ch += 9)
     
    367421                    i += 2;
    368422                }
     423            }
     424        }
     425    }
     426
     427    static void SlidingAverage(float *const vec, const uint32_t roi, const uint16_t w)
     428    {
     429        if (w==0)
     430            return;
     431
     432        if (w>roi)
     433            return;
     434
     435        for (float *pix=vec; pix<vec+1440*roi; pix += roi)
     436        {
     437            for (float *ptr=pix; ptr<pix+roi-w; ptr++)
     438            {
     439                for (float *p=ptr+1; p<ptr+w; p++)
     440                    *ptr += *p;
     441                *ptr /= w;
    369442            }
    370443        }
     
    884957    }
    885958
     959    std::string WriteFitsImp(const std::string &filename, const std::vector<float> &vec) const
     960    {
     961        const size_t n = 1440*1024*4 + 1440*fRoi*2 + fNumTm*fRoi*2 + 3;
     962
     963        std::ofits file(filename.c_str());
     964        if (!file)
     965        {
     966            std::ostringstream msg;
     967            msg << "Could not open file '" << filename << "': " << strerror(errno);
     968            return msg.str();
     969        }
     970
     971        file.AddColumnInt("RunNumberBaseline");
     972        file.AddColumnInt("RunNumberGain");
     973        file.AddColumnInt("RunNumberTriggerOffset");
     974
     975        file.AddColumnFloat(1024*1440,   "BaselineMean",        "mV");
     976        file.AddColumnFloat(1024*1440,   "BaselineRms",         "mV");
     977        file.AddColumnFloat(1024*1440,   "GainMean",            "mV");
     978        file.AddColumnFloat(1024*1440,   "GainRms",             "mV");
     979        file.AddColumnFloat(fRoi*1440,   "TriggerOffsetMean",   "mV");
     980        file.AddColumnFloat(fRoi*1440,   "TriggerOffsetRms",    "mV");
     981        file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMMean", "mV");
     982        file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMRms",  "mV");
     983
     984#ifdef __MARS__
     985        const MTime now;
     986        file.SetStr(  "TELESCOP", "FACT",               "Telescope that acquired this data");
     987        file.SetStr(  "PACKAGE",  "MARS",               "Package name");
     988        file.SetStr(  "VERSION",  "1.0",                "Package description");
     989        //file.SetStr(  "CREATOR",  "root",               "Program that wrote this file");
     990        file.SetFloat("EXTREL",   1.0,                  "Release Number");
     991        file.SetStr(  "COMPILED", __DATE__" "__TIME__,  "Compile time");
     992        //file.SetStr(  "REVISION", REVISION,             "SVN revision");
     993        file.SetStr(  "ORIGIN",   "FACT",               "Institution that wrote the file");
     994        file.SetStr(  "DATE",     now.GetStringFmt("%Y-%m-%dT%H:%M:%S").Data(), "File creation date");
     995        file.SetInt(  "NIGHT",    now.GetNightAsInt(),  "Night as int");
     996        file.SetStr(  "TIMESYS",  "UTC",                "Time system");
     997        file.SetStr(  "TIMEUNIT", "d",                  "Time given in days w.r.t. to MJDREF");
     998        file.SetInt(  "MJDREF",   40587,                "MJD to UNIX time (seconds since 1970/1/1)");
     999#else
     1000        DataWriteFits2::WriteDefaultKeys(file);
     1001#endif
     1002        file.SetInt("STEP", fStep, "");
     1003
     1004        file.SetInt("ADCRANGE", 2000,    "Dynamic range of the ADC in mV");
     1005        file.SetInt("DACRANGE", 2500,    "Dynamic range of the DAC in mV");
     1006        file.SetInt("ADC",      12,      "Resolution of ADC in bits");
     1007        file.SetInt("DAC",      16,      "Resolution of DAC in bits");
     1008        file.SetInt("NPIX",     1440,    "Number of channels in the camera");
     1009        file.SetInt("NTM",      fNumTm,  "Number of time marker channels");
     1010        file.SetInt("NROI",     fRoi,    "Region of interest");
     1011
     1012        file.SetInt("NBOFFSET", fNumOffset,       "Num of entries for offset calibration");
     1013        file.SetInt("NBGAIN",   fNumGain/1953125, "Num of entries for gain calibration");
     1014        file.SetInt("NBTRGOFF", fNumTrgOff,       "Num of entries for trigger offset calibration");
     1015
     1016        // file.WriteKeyNT("DAC_A",    fData.fDAC[0],    "Level of DAC 0 in DAC counts")   ||
     1017        // file.WriteKeyNT("DAC_B",    fData.fDAC[1],    "Leval of DAC 1-3 in DAC counts") ||
     1018        // file.WriteKeyNT("DAC_C",    fData.fDAC[4],    "Leval of DAC 4-7 in DAC counts") ||
     1019
     1020        file.WriteTableHeader("DrsCalibration");
     1021
     1022        if (!file.WriteRow(vec.data()+1, n*sizeof(float)))
     1023        {
     1024            std::ostringstream msg;
     1025            msg << "Writing data to " << filename << " failed.";
     1026            return msg.str();
     1027        }
     1028
     1029        return std::string();
     1030    }
     1031
     1032
    8861033    std::string ReadFitsImp(const std::string &str)
    8871034    {
Note: See TracChangeset for help on using the changeset viewer.