Changeset 14790 for fact/tools


Ignore:
Timestamp:
01/24/13 15:49:35 (12 years ago)
Author:
Jens Buss
Message:
bugfix: resize destination vector in applyDrasCalibration everytime to
not leave zeros in vector
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/rootmacros/DrsCalibration.C

    r13535 r14790  
    55
    66float getValue( int slice, int pixel,
    7         vector<float> &drs_basemean,
    8         vector<float> &drs_gainmean,
    9         vector<float> &drs_triggeroffsetmean,
    10         unsigned int    RegionOfInterest,
    11         vector<int16_t> AllPixelDataVector,
    12         vector<int16_t> StartCellVector
     7    vector<float> &drs_basemean,
     8    vector<float> &drs_gainmean,
     9    vector<float> &drs_triggeroffsetmean,
     10    unsigned int    RegionOfInterest,
     11    vector<int16_t> AllPixelDataVector,
     12    vector<int16_t> StartCellVector
    1313){
    14         const float dconv = 2000/4096.0;
     14    const float dconv = 2000/4096.0;
    1515
    16         float vraw, vcal;
     16    float vraw, vcal;
    1717
    18         unsigned int pixel_pt;
    19         unsigned int slice_pt;
    20         unsigned int cal_pt;
    21         unsigned int drs_cal_offset;
     18    unsigned int pixel_pt;
     19    unsigned int slice_pt;
     20    unsigned int cal_pt;
     21    unsigned int drs_cal_offset;
    2222
    23         // printf("pixel = %d, slice = %d\n", slice, pixel);
     23    // printf("pixel = %d, slice = %d\n", slice, pixel);
    2424
    25         pixel_pt = pixel * RegionOfInterest;
    26         slice_pt = pixel_pt + slice;
    27         drs_cal_offset = ( slice + StartCellVector[ pixel ] )%RegionOfInterest;
    28         cal_pt    = pixel_pt + drs_cal_offset;
     25    pixel_pt = pixel * RegionOfInterest;
     26    slice_pt = pixel_pt + slice;
     27    drs_cal_offset = ( slice + StartCellVector[ pixel ] )%RegionOfInterest;
     28    cal_pt    = pixel_pt + drs_cal_offset;
    2929
    30         vraw = AllPixelDataVector[ slice_pt ] * dconv;
    31         vcal = ( vraw - drs_basemean[ cal_pt ] - drs_triggeroffsetmean[ slice_pt ] ) / drs_gainmean[ cal_pt ]*1907.35;
     30    vraw = AllPixelDataVector[ slice_pt ] * dconv;
     31    vcal = ( vraw - drs_basemean[ cal_pt ] - drs_triggeroffsetmean[ slice_pt ] ) / drs_gainmean[ cal_pt ]*1907.35;
    3232
    33         return( vcal );
     33    return( vcal );
    3434}
    3535
     
    5151// openCalibFits()
    5252//
    53 size_t applyDrsCalibration( vector<float> &destination,
    54         int pixel,
    55   int LeaveOutLeft,
    56   int LeaveOutRight,
    57         vector<float> &drs_basemean,
    58         vector<float> &drs_gainmean,
    59         vector<float> &drs_triggeroffsetmean,
    60         unsigned int RegionOfInterest,
    61         vector<int16_t> AllPixelDataVector,
    62         vector<int16_t> StartCellVector,
    63         int verbosityLevel
     53size_t applyDrsCalibration(
     54        vector<float> &destination,
     55        int pixel,
     56        int LeaveOutLeft,
     57        int LeaveOutRight,
     58        vector<float> &drs_basemean,
     59        vector<float> &drs_gainmean,
     60        vector<float> &drs_triggeroffsetmean,
     61        unsigned int RegionOfInterest,
     62        vector<int16_t> AllPixelDataVector,
     63        vector<int16_t> StartCellVector,
     64        int verbosityLevel
    6465){
    65         // In order to minimize mem free and alloc actions
    66         // the destination vector is only resized, if it is way too big
    67         // or too small
    68         size_t DestinationLength = RegionOfInterest-LeaveOutRight-LeaveOutLeft;
    69         if ( destination.size() < DestinationLength || destination.size() > 2 * DestinationLength ){
    70                 destination.resize( DestinationLength );
    71         }
     66    // In order to minimize mem free and alloc actions
     67    // the destination vector is only resized, if it is way too big
     68    // or too small
     69    size_t DestinationLength = RegionOfInterest-LeaveOutRight-LeaveOutLeft;
     70    if ( destination.size() != DestinationLength){
     71        destination.resize( DestinationLength );
     72    }
    7273
    73         //      We do not entirely know how the calibration constants, which are saved in a filename.drs.fits file
    74         // were calculated, so it is not fully clear how they should be applied to the raw data for calibration.
    75         // apparently the calibration constants were transformed to the unit mV, which means we have to do the same to
    76         // the raw data prior to apply the calibration
    77         //
    78         // on the FAD board, there is a 12bit ADC, with a 2.0V range, so the factor between ADC units and mV is
    79         // ADC2mV = 2000/4096. = 0.48828125 (numerically exact)
    80         //
    81         // from the schematic of the FAD we learned, that the voltage at the ADC
    82         // should be 1907.35 mV when the calibration DAC is set to 50000.
    83         //
    84         // One would further assume that the calibration constants are calculated like this:
     74    //  We do not entirely know how the calibration constants, which are saved in a filename.drs.fits file
     75    // were calculated, so it is not fully clear how they should be applied to the raw data for calibration.
     76    // apparently the calibration constants were transformed to the unit mV, which means we have to do the same to
     77    // the raw data prior to apply the calibration
     78    //
     79    // on the FAD board, there is a 12bit ADC, with a 2.0V range, so the factor between ADC units and mV is
     80    // ADC2mV = 2000/4096. = 0.48828125 (numerically exact)
     81    //
     82    // from the schematic of the FAD we learned, that the voltage at the ADC
     83    // should be 1907.35 mV when the calibration DAC is set to 50000.
     84    //
     85    // One would further assume that the calibration constants are calculated like this:
    8586
    86         // The DRS Offset of each bin in each channel is the mean value in this very bin,
    87         // obtained from so called DRS pedestal data
    88         // Its value is about -1820 ADC units or -910mV
     87    // The DRS Offset of each bin in each channel is the mean value in this very bin,
     88    // obtained from so called DRS pedestal data
     89    // Its value is about -1820 ADC units or -910mV
    8990
    90         // In order to obtain the DRS Gain of each bin of each channel
    91         // again data is takes, with the calibration DAC set to 50000
    92         // This is called DRS calibration data.
    93         // We assume the DRS Offset is already subtracted from the DRS calibration data
    94         // so the typical value is assumed to be ~3600 ADC units oder ~1800mV
    95         // As mentioned before, the value *should* be 1907.35 mV
    96         // So one might assume that the Gain is a number, which actually converts ~3600 ADC units into 1907.35mV for each bin of each channel.
    97         // So that the calibration procedure looks like this
    98         // TrueValue = (RawValue - Offset) * Gain
    99         // The numerical value of Gain would differ slightly from the theoretical value of 2000/4096.
    100         // But this is apparently not the case.
    101         // The Gain, as it is stored in the DRS calibration file of FACT++ has numerical values
    102         // around +1800.
    103         // So it seems one should calibrate like this:
    104         // TrueValue = (RawValue - Offset) / Gain * 1907.35
     91    // In order to obtain the DRS Gain of each bin of each channel
     92    // again data is takes, with the calibration DAC set to 50000
     93    // This is called DRS calibration data.
     94    // We assume the DRS Offset is already subtracted from the DRS calibration data
     95    // so the typical value is assumed to be ~3600 ADC units oder ~1800mV
     96    // As mentioned before, the value *should* be 1907.35 mV
     97    // So one might assume that the Gain is a number, which actually converts ~3600 ADC units into 1907.35mV for each bin of each channel.
     98    // So that the calibration procedure looks like this
     99    // TrueValue = (RawValue - Offset) * Gain
     100    // The numerical value of Gain would differ slightly from the theoretical value of 2000/4096.
     101    // But this is apparently not the case.
     102    // The Gain, as it is stored in the DRS calibration file of FACT++ has numerical values
     103    // around +1800.
     104    // So it seems one should calibrate like this:
     105    // TrueValue = (RawValue - Offset) / Gain * 1907.35
    105106
    106         // When these calibrations are done, one ends up with a quite nice calibrated voltage.
    107         // But it turns out that, if one returns the first measurement, and calculates the mean voltages
    108         // in each bin of the now *logical* DRS pipeline, the mean voltage is not zero, but slightly varies
    109         // So one can store these systematical deviations from zero in the logical pipeline as well, and subtract them.
    110         // The remaining question is, when to subtract them.
    111         // I assume, in the process of measuring this third calibration constant, the first two
    112         // calibrations are already applied to the raw data.
     107    // When these calibrations are done, one ends up with a quite nice calibrated voltage.
     108    // But it turns out that, if one returns the first measurement, and calculates the mean voltages
     109    // in each bin of the now *logical* DRS pipeline, the mean voltage is not zero, but slightly varies
     110    // So one can store these systematical deviations from zero in the logical pipeline as well, and subtract them.
     111    // The remaining question is, when to subtract them.
     112    // I assume, in the process of measuring this third calibration constant, the first two
     113    // calibrations are already applied to the raw data.
    113114
    114         // So the calculation of the calibrated volatage from some raw voltage works like this:
    115         // assume the raw voltage is the s'th sample in channel c. While the Trigger made the DRS stopp in its t'th cell.
    116         // note, that the DRS pipeline is always 1024 bins long. This is constant of the DRS4 chip.
     115    // So the calculation of the calibrated volatage from some raw voltage works like this:
     116    // assume the raw voltage is the s'th sample in channel c. While the Trigger made the DRS stopp in its t'th cell.
     117    // note, that the DRS pipeline is always 1024 bins long. This is constant of the DRS4 chip.
    117118
    118         // TrueValue[c][s] = ( RawValue[c][s] - Offset[c][ (c+t)%1024 ] ) / Gain[c][ (c+t)%1024 ] * 1907.35 - TriggerOffset[c][s]
    119        
    120         const float dconv = 2000/4096.0;
    121         float vraw;
     119    // TrueValue[c][s] = ( RawValue[c][s] - Offset[c][ (c+t)%1024 ] ) / Gain[c][ (c+t)%1024 ] * 1907.35 - TriggerOffset[c][s]
    122120
    123         if ( RegionOfInterest != AllPixelDataVector.size()/1440 ){
    124                 if (verbosityLevel > 0){
    125                         cout << "RegionOfInterest != AllPixelDataVector.size()/1440" << endl;
    126                         cout << "RegionOfInterest: " << RegionOfInterest << endl;
    127                         cout << "AllPixelDataVector.size(): " << AllPixelDataVector.size() << endl;
    128                         cout << "aborting" << endl;
    129                 }
    130                 return 0;
    131         }
     121    const float dconv = 2000/4096.0;
     122    float vraw;
    132123
    133         // the vector drs_triggeroffsetmean is not 1440 * 1024 entries long
    134         // but has hopefully the length 1440 * RegionOfInterest (or longer)
    135         if ( drs_triggeroffsetmean.size() < 1440*RegionOfInterest ){
    136                 if (verbosityLevel > 0){
    137                         cout << "Error: drs_triggeroffsetmean.size() < 1440*RegionOfInterest" << endl;
    138                         cout << "drs_triggeroffsetmean.size():" << drs_triggeroffsetmean.size() << endl;
    139                         cout << "RegionOfInterest" << RegionOfInterest << endl;
    140                         cout << "aborting" << endl;
    141                 }
    142                 return 0;
    143         }
     124    if ( RegionOfInterest != AllPixelDataVector.size()/1440 ){
     125        if (verbosityLevel > 0){
     126            cout << "RegionOfInterest != AllPixelDataVector.size()/1440" << endl;
     127            cout << "RegionOfInterest: " << RegionOfInterest << endl;
     128            cout << "AllPixelDataVector.size(): " << AllPixelDataVector.size() << endl;
     129            cout << "aborting" << endl;
     130        }
     131        return 0;
     132    }
    144133
    145         int DataPos, OffsetPos, TriggerOffsetPos;
    146        
    147         for ( unsigned int sl = LeaveOutLeft; sl < RegionOfInterest-LeaveOutRight ; sl++){
     134    // the vector drs_triggeroffsetmean is not 1440 * 1024 entries long
     135    // but has hopefully the length 1440 * RegionOfInterest (or longer)
     136    if ( drs_triggeroffsetmean.size() < 1440*RegionOfInterest ){
     137        if (verbosityLevel > 0){
     138            cout << "Error: drs_triggeroffsetmean.size() < 1440*RegionOfInterest" << endl;
     139            cout << "drs_triggeroffsetmean.size():" << drs_triggeroffsetmean.size() << endl;
     140            cout << "RegionOfInterest" << RegionOfInterest << endl;
     141            cout << "aborting" << endl;
     142        }
     143        return 0;
     144    }
    148145
    149                 DataPos = pixel * RegionOfInterest + sl;
    150                 // Offset and Gain vector *should look the same
    151                 OffsetPos = pixel * drs_basemean.size()/1440 + (sl + StartCellVector[pixel])%(drs_basemean.size()/1440);
     146    int DataPos, OffsetPos, TriggerOffsetPos;
    152147
    153                 TriggerOffsetPos = pixel * drs_triggeroffsetmean.size()/1440 + sl;
     148    for ( unsigned int sl = LeaveOutLeft; sl < RegionOfInterest-LeaveOutRight ; sl++){
    154149
    155                 vraw = AllPixelDataVector[ DataPos ] * dconv;
    156                 vraw -= drs_basemean[ OffsetPos ];
    157                 vraw -= drs_triggeroffsetmean[ TriggerOffsetPos ];
    158                 vraw /= drs_gainmean[ OffsetPos ];
    159                 vraw *= 1907.35;
     150        DataPos = pixel * RegionOfInterest + sl;
     151        // Offset and Gain vector *should look the same
     152        OffsetPos = pixel * drs_basemean.size()/1440 + (sl + StartCellVector[pixel])%(drs_basemean.size()/1440);
     153
     154        TriggerOffsetPos = pixel * drs_triggeroffsetmean.size()/1440 + sl;
     155
     156        vraw = AllPixelDataVector[ DataPos ] * dconv;
     157        vraw -= drs_basemean[ OffsetPos ];
     158        vraw -= drs_triggeroffsetmean[ TriggerOffsetPos ];
     159        vraw /= drs_gainmean[ OffsetPos ];
     160        vraw *= 1907.35;
    160161
    161162//              slice_pt = pixel_pt + sl;
     
    166167//              destination.push_back(vcal);
    167168
    168                 destination[sl-LeaveOutLeft] = vraw;
    169         }
     169        destination[sl-LeaveOutLeft] = vraw;
     170    }
    170171
    171         return destination.size();
     172    return destination.size();
    172173}
Note: See TracChangeset for help on using the changeset viewer.