Changeset 13400


Ignore:
Timestamp:
04/20/12 14:41:09 (13 years ago)
Author:
kraehenb
Message:
Completed the fast implementation to read calibrated data: calfits.h, CalFitsTest.py.
Location:
fact/tools/pyscripts/sandbox/kraehenb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/pyscripts/sandbox/kraehenb/CalFitsTest.py

    r13397 r13400  
     1#!/usr/bin/python -tt
     2# ********************************
     3# Test script for the CalFits class
     4#
     5# written by Thomas Kraehenbuehl, ETH Zurich
     6# tpk@phys.ethz.ch, +41 44 633 3973
     7# April 2012
     8# ********************************
     9
    110datafilename = '/fact/raw/2012/04/17/20120417_004.fits.gz'
    211calibfilename = '/fact/raw/2012/04/17/20120417_003.drs.fits.gz'
    3 #data_filename =  '/fact/raw/2012/01/31/20120131_018.fits.gz'
    4 #calib_filename = '/fact/raw/2012/01/31/20120131_014.drs.fits.gz'
    512
    6 DoCalFits=True
     13from ROOT import gSystem
     14gSystem.Load("calfits_h.so")
     15from ROOT import *
     16print "Testing object creation: "
     17caltest = CalFits(datafilename,calibfilename)
    718
    8 if DoCalFits:
    9         from ROOT import gSystem
    10         gSystem.Load("CalFits_h.so")
    11         from ROOT import *
    12         print "Testing object creation: "
    13         caltest = CalFits(datafilename,calibfilename)
    14         print "Testing the python access to the function: "
    15         print caltest.data_npix
     19print "Common variables:"
     20print "ROI: ", caltest.nroi
     21print "#Pix: ", caltest.npix
     22print "Number of events: ", caltest.nevents
     23print
    1624
    17         print "Calibfile ROI: ", caltest.calib_nroi
    18         print "BaselineMean: ", caltest.calibfile.GetN("BaselineMean")
    19         print "GainMean: ", caltest.calibfile.GetN("GainMean")
    20         print "TriggerOffsetMean: ", caltest.calibfile.GetN("TriggerOffsetMean")
    21         print "Datafile ROI: ", caltest.data_nroi
    22         print "Data: ", caltest.datafile.GetN("Data")
    23         print "StartCellData: ", caltest.datafile.GetN("StartCellData")
    24         print "Direct datafile access: ", caltest.datafile.GetN("StartCellData")
     25print "Information per Event:"
     26caltest.GetCalEvent()
     27print "Event ID: ", caltest.event_id
     28print "Trigger type: ", caltest.event_triggertype
     29print "Uncalibrated data: ", caltest.event_data
     30print "Calibrated data: ", caltest.event_caldata
     31print "Board times: ", caltest.event_boardtimes
     32print "Trigger offsets: ", caltest.event_offset
     33print
    2534
    26 #       print caltest.GetCalEvent()
    27         for i in range(1000):
    28                 caltest.GetCalEvent()
    29                 print caltest.data_eventid
    30         print
     35print "Examples of other information"
     36print "Calibfile ROI: ", caltest.calib_nroi
     37print "Column size BaselineMean: ", caltest.calibfile.GetN("BaselineMean")
     38print "Datafile ROI: ", caltest.data_nroi
     39print "Data: ", caltest.datafile.GetN("Data")
     40print "StartCellData: ", caltest.datafile.GetN("StartCellData")
     41print "Direct datafile access: ", caltest.datafile.GetN("StartCellData")
     42print
     43print "Columns of the datafile: "
     44caltest.datafile.PrintColumns()
    3145
    32         del caltest
     46while caltest.GetCalEvent():
     47    print caltest.event_id, caltest.event_triggertype, caltest.event_caldata[0]
     48print
    3349
    34 else:
    35         from pyfact import RawData
    36         pyfacttest = RawData(datafilename, calibfilename, return_dict = True)
    37         for i,event in enumerate(pyfacttest):
    38                 if i>0:
    39                         break
    40                 print event['acal_data'][0][0:5]
     50del caltest
  • fact/tools/pyscripts/sandbox/kraehenb/calfits.h

    r13396 r13400  
    1111//********************************
    1212//
    13 // Compilation:
    14 // ...
     13// Compilation (root in pyfact directory)
     14// root [0] gSystem->Load("/usr/lib64/libz.so");
     15// root [1] .L pyfits.h++
     16// root [2] .L ../sandbox/kraehenb/calfits.h++
    1517//
    1618// Usage in Python:
    17 // ...handles etc...
     19// See CalFitsTest.py
    1820//
    1921//********************************
    2022
    21 //ToDo: Code aufräumen, ToDo's abarbeiten, alle Event-Parameter zugänglich, shared library creation debuggen
     23//ToDo: alle Event-Parameter zugänglich, shared library creation debuggen
     24
     25#ifndef CALFITS_H
     26#define CALFITS_H
    2227
    2328#include <cstdio>
     29#include <string>
     30
     31#ifndef __MARS__
    2432#include <vector>
    25 #include <string>
    26 #include "pyfits.h"
     33#include <iomanip>
     34#include <iostream>
     35#define gLog cerr
     36#define ___err___ ""
     37#define ___all___ ""
     38#else
     39#include "MLog.h"
     40#include "MLogManip.h"
     41#define ___err___ err
     42#define ___all___ all
     43#endif
     44
     45#ifdef __EXCEPTIONS
     46#include <stdexcept>
     47#endif
     48
     49#include "../../pyfact/pyfits.h"
    2750
    2851class CalFits
     
    3457        fits datafile, calibfile; //Class name should be PyFits or better FactPyFits...
    3558       
    36         //Calibration variables
     59        //Basic file parameters
    3760        UInt_t calib_nroi, calib_npix;
    38         vector<float> calib_basemean;
    39         vector<float> calib_gainmean;
    40         vector<float> calib_triggeroffsetmean;
    41         //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
    42        
    43         //Data variables
     61        UInt_t calib_blm_size, calib_gm_size, calib_tom_size;
    4462        UInt_t data_nroi, data_npix, data_ndata;
    4563       
     
    4765        UInt_t nroi, npix, nevents;
    4866       
     67        //Calibration variables
     68        vector<float> calib_baselinemean;
     69        vector<float> calib_gainmean;
     70        vector<float> calib_triggeroffsetmean;
     71        //ToDo: use arrays of size 1440x1024 (x2 for wrap-arounds) and read all variables into those
     72       
    4973        //Event variables
    50         UInt_t data_eventid;
    51         vector<int16_t> data_data;
    52         vector<int16_t> data_offset;
     74        UInt_t event_id;
     75        UShort_t event_triggertype;
     76        vector<int16_t> event_data;
     77        vector<int16_t> event_offset;
     78        vector<int32_t> event_boardtimes;
     79        vector<float> event_caldata; //Vector for calibrated event
    5380       
    54         //Vector for calibrated event
    55         vector<float> calevent;
    56        
    57         CalFits(const string &datafilename, const string &calibfilename) //Construct with two filenames
     81        CalFits(const string &datafilename, const string &calibfilename) //Constructor with two filenames
    5882                : datafile(datafilename),
    5983                        calibfile(calibfilename)
    6084        {
    6185                //Read basic parameters of the two files
    62                 std::cout << "...Reading basic file parameters..." << std::endl;
     86//              std::cout << "...Reading basic file parameters..." << std::endl;
    6387                calib_nroi = calibfile.GetUInt("NROI");
    6488                calib_npix = calibfile.GetUInt("NPIX");
    6589                data_nroi = datafile.GetUInt("NROI");
    6690                data_npix = datafile.GetUInt("NPIX");
    67                 nevents = datafile.GetNumRows();
    6891                data_ndata = datafile.GetN("Data");
    6992               
    70                 //ToDo: check nroi*npix = ndata, calib_nroi=data_nroi, calib_npix=data_npix
     93                calib_blm_size = calibfile.GetN("BaselineMean")/calib_npix;
     94                calib_gm_size = calibfile.GetN("GainMean")/calib_npix;
     95                calib_tom_size = calibfile.GetN("TriggerOffsetMean")/calib_npix;
     96               
     97//              std::cout << "Column sizes: " << calib_blm_size << " " << calib_gm_size << " " << calib_tom_size << std::endl;
     98               
     99                //Define the common variables
     100                if((calib_nroi==data_nroi)&&(calib_npix==data_npix)&&(data_nroi*data_npix==data_ndata)&&(calib_blm_size==calib_gm_size)&&(calib_tom_size==calib_nroi)) {
     101                        nroi = data_nroi;
     102                        npix = data_npix;
     103                }
     104                else {
     105                        ostringstream str;
     106                        str << "Data/calib file error: NROI mismatch, NPIX mismatch, data column size wrong or calib columns mismatch.";
     107#ifdef __EXCEPTIONS
     108                        throw runtime_error(str.str());
     109#else
     110                        gLog << ___err___ << "ERROR - " << str.str() << endl;
     111                        return;
     112#endif
     113                }
     114                nevents = datafile.GetNumRows();
    71115               
    72116                //Read the calibration data
    73                 std::cout << "...Reading calibration data..." << std::endl;
    74                 calib_basemean.resize(calibfile.GetN("BaselineMean"),0);
    75                 calibfile.SetVecAddress("BaselineMean", calib_basemean);
     117//              std::cout << "...Reading calibration data..." << std::endl;
     118                calib_baselinemean.resize(calibfile.GetN("BaselineMean"),0);
     119                calibfile.SetVecAddress("BaselineMean", calib_baselinemean);
    76120                calib_gainmean.resize(calibfile.GetN("GainMean"),0);
    77121                calibfile.SetVecAddress("GainMean", calib_gainmean);
     
    80124                calibfile.GetRow(0);
    81125               
    82                 //Set the data vectors
    83                 std::cout << "...Setting datafile pointers..." << std::endl;
    84                 datafile.SetRefAddress("EventNum", data_eventid);
    85                 data_data.resize(data_ndata,0);
    86                 datafile.SetVecAddress("Data", data_data);
    87                 calevent.resize(data_ndata,0);
    88                 data_offset.resize(datafile.GetN("StartCellData"),0);
    89                 datafile.SetVecAddress("StartCellData", data_offset);
     126                //Get the column sizes per pixel
     127                //...
     128               
     129                //Set the event pointers
     130//              std::cout << "...Setting event pointers..." << std::endl;
     131                datafile.SetRefAddress("EventNum", event_id);
     132                datafile.SetRefAddress("TriggerType", event_triggertype);
     133               
     134                event_data.resize(data_ndata,0);
     135                event_caldata.resize(data_ndata,0);
     136                datafile.SetVecAddress("Data", event_data);
     137               
     138                event_offset.resize(datafile.GetN("StartCellData"),0);
     139                datafile.SetVecAddress("StartCellData", event_offset);
     140               
     141                event_boardtimes.resize(datafile.GetN("BoardTime"),0);
     142                datafile.SetVecAddress("BoardTime", event_boardtimes);
    90143        }
    91144       
    92         //double* GetCalEvent() { }
    93         int GetCalEvent()
     145        bool GetCalEvent()
    94146        {
    95147                if(datafile.GetNextRow() == false) {
    96                         std::cout << "Last event reached..." << std::endl;
    97                         //ToDo: raise exception or stop or whatever GetNextRow does
     148//                      std::cout << "Last event reached..." << std::endl;
     149                        return false;
    98150                }
    99151                else {
     
    101153                        for(UInt_t pixel=0;pixel<data_npix;pixel++) {
    102154                                for(UInt_t slice=0;slice<data_nroi;slice++) {
    103                                         //ToDo: replace 1024 by column size/npix
    104                                         drs_calib_offset = (slice+data_offset[pixel])%1024;
    105                                         calevent[pixel*data_nroi+slice] = float((data_data[pixel*data_nroi+slice]*2000./4096.-calib_basemean[pixel*1024+drs_calib_offset]-calib_triggeroffsetmean[pixel*calib_nroi+slice])/calib_gainmean[pixel*1024+drs_calib_offset]*1907.35);
    106                                         //Note: data_nroi=calib_nroi
     155                                        drs_calib_offset = (slice+event_offset[pixel])%calib_blm_size;
     156                                        event_caldata[pixel*data_nroi+slice] = float((event_data[pixel*data_nroi+slice]*2000./4096.-calib_baselinemean[pixel*calib_blm_size+drs_calib_offset]-calib_triggeroffsetmean[pixel*data_nroi+slice])/calib_gainmean[pixel*calib_blm_size+drs_calib_offset]*1907.35);
     157                                        //Note: data_nroi=calib_nroi, calib_blm_size=calib_gm_size
    107158                                }
    108159                        }
    109160                }
    110                 return 0;
     161                return true;
    111162        }
    112163};
     164#endif
Note: See TracChangeset for help on using the changeset viewer.