Changeset 285 in Idarraga


Ignore:
Timestamp:
Apr 20, 2012, 5:11:44 PM (12 years ago)
Author:
idarraga
Message:
 
Location:
mafalda/MAFTools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mafalda/MAFTools/MAFTools.cpp

    r251 r285  
    1717#include <set>
    1818#include <iostream>
     19#include <fstream>
     20#include <stdlib.h>
    1921
    2022#include <TROOT.h>
     
    349351
    350352                        //cout << "next: " <<  nextNeighbor.first << " , " << nextNeighbor.second << " ---> "
    351                                         //<< totmap[ nextNeighbor.second * maxx + nextNeighbor.first ] << endl;
     353                        //<< totmap[ nextNeighbor.second * maxx + nextNeighbor.first ] << endl;
    352354
    353355                        // check the Set see if it existed
     
    390392        else
    391393                return (2*tolerancePixels + 2)*4 +
    392                 GetLengthOfSpiral(--tolerancePixels);
     394                                GetLengthOfSpiral(--tolerancePixels);
    393395
    394396}
     
    437439        spiral->yCntr = 1;
    438440        spiral->dir = 1;
     441
     442}
     443
     444void FillValuesForDisplay(Highlighter * hl, blob bl){
     445
     446        hl->UploadDisplayValue("Number of Pixels     ", bl.bP.nPixels);
     447        hl->UploadDisplayValue("Inner Pixels         ", bl.bP.nInnerPixels);
     448        hl->UploadDisplayValue("Total charge         ", bl.bP.totalCharge);
     449        hl->UploadDisplayValue("Width x              ", bl.bP.width_x);
     450        hl->UploadDisplayValue("Width y              ", bl.bP.width_y);
     451        hl->UploadDisplayValue("Geo center x         ", bl.bP.geoCenter_x);
     452        hl->UploadDisplayValue("Geo center y         ", bl.bP.geoCenter_y);
     453        hl->UploadDisplayValue("Weighted center x    ", bl.bP.weightedCenter_x);
     454        hl->UploadDisplayValue("Weighted center y    ", bl.bP.weightedCenter_y);
     455        hl->UploadDisplayValue("Box area             ", bl.bP.boxArea);
     456        hl->UploadDisplayValue("Circle area          ", bl.bP.circleArea);
     457        hl->UploadDisplayValue("Ellipse area         ", bl.bP.ellipseArea);
     458        hl->UploadDisplayValue("  circle/elipse      ", bl.bP.circleArea/bl.bP.ellipseArea);
     459        hl->UploadDisplayValue("Elipse A             ", bl.bP.ellipseA);
     460        hl->UploadDisplayValue("Elipse B             ", bl.bP.ellipseB);
     461        hl->UploadDisplayValue("Rotation angle [deg] ", bl.bP.rotAngle * 180 / TMath::Pi());
     462        hl->UploadDisplayValue("Chisquare/dof        ", bl.bP.chisquare_OverDof);
     463        hl->UploadDisplayValue("Fit slope            ", bl.bP.fitSlope);
     464        hl->UploadDisplayValue("Fit cut              ", bl.bP.fitCut);
     465        hl->UploadDisplayValue("Balance to minimum   ", bl.bP.balanceToMin);
     466        hl->UploadDisplayValue("Balance to maximum   ", bl.bP.balanceToMin);
     467        hl->UploadDisplayValue("Min dist to center   ", bl.bP.minToGeoCenter);
     468        hl->UploadDisplayValue("Max dist to center   ", bl.bP.maxToGeoCenter);
    439469
    440470}
     
    507537}
    508538
    509 }
    510 
    511 
     539
     540TimePixCalibration::TimePixCalibration(const char * af,
     541                const char * bf,
     542                const char * cf,
     543                const char * tf,
     544                int width, int height){
     545
     546        m_width = width;
     547        m_height = height;
     548
     549        ifstream afs(af, fstream::in);
     550        ifstream bfs(bf, fstream::in);
     551        ifstream cfs(cf, fstream::in);
     552        ifstream tfs(tf, fstream::in);
     553
     554        ////////////////////////////////////
     555        // a
     556        m_a = new double[m_width*m_height];
     557        double temp = 0.;
     558        int i = 0;
     559        while ( afs.good() ) {
     560                afs >> temp;
     561                if(afs.eof()) break;
     562                m_a[i++] = temp;
     563        }
     564        afs.close();
     565        if(i != m_width*m_height){
     566                cout << "Calibration does not seem to be complete for paremeter a." << endl;
     567                cout << "Got " << i << " items.  " << m_width << "*" << m_height
     568                                << " were requested.  Giving up." << endl;
     569                exit(1);
     570        }
     571
     572        ////////////////////////////////////
     573        // b
     574        m_b = new double[m_width*m_height];
     575        i = 0;
     576        while ( bfs.good() ) {
     577                bfs >> temp;
     578                if(bfs.eof()) break;
     579                m_b[i++] = temp;
     580        }
     581        bfs.close();
     582        if(i != m_width*m_height){
     583                cout << "Calibration does not seem to be complete for paremeter b." << endl;
     584                cout << "Got " << i << " items.  " << m_width << "*" << m_height
     585                                << " were requested.  Giving up." << endl;
     586                exit(1);
     587        }
     588
     589        ////////////////////////////////////
     590        // c
     591        m_c = new double[m_width*m_height];
     592        i = 0;
     593        while ( cfs.good() ) {
     594                cfs >> temp;
     595                if(cfs.eof()) break;
     596                m_c[i++] = temp;
     597        }
     598        cfs.close();
     599        if(i != m_width*m_height){
     600                cout << "Calibration does not seem to be complete for paremeter c." << endl;
     601                cout << "Got " << i << " items.  " << m_width << "*" << m_height
     602                                << " were requested.  Giving up." << endl;
     603                exit(1);
     604        }
     605
     606        ////////////////////////////////////
     607        // t
     608        m_t = new double[m_width*m_height];
     609        i = 0;
     610        while ( tfs.good() ) {
     611                tfs >> temp;
     612                if(tfs.eof()) break;
     613                m_t[i++] = temp;
     614        }
     615        tfs.close();
     616        if(i != m_width*m_height){
     617                cout << "Calibration does not seem to be complete for paremeter t." << endl;
     618                cout << "Got " << i << " items.  " << m_width << "*" << m_height
     619                                << " were requested.  Giving up." << endl;
     620                exit(1);
     621        }
     622
     623}
     624
     625TimePixCalibration::~TimePixCalibration(){
     626}
     627
     628double TimePixCalibration::GetE(pair<int,int> pix, int tot){
     629
     630        //TF1 * surr = GetSurrogateTF1(pix);
     631
     632        int index = MAFTools::XYtoC(pix, m_width);
     633        double par[] = { m_a[index], m_b[index], m_c[index], m_t[index] };
     634
     635/*
     636        if(pix.first == 111 && pix.second == 70) {
     637                std::cout << "a = " << par[0] << std::endl;
     638                std::cout << "b = " << par[1] << std::endl;
     639                std::cout << "c = " << par[2] << std::endl;
     640                std::cout << "t = " << par[3] << std::endl;
     641        }*/
     642
     643
     644        double evalTOT = 0.;
     645        double prev_e = 0.;
     646        double dist = 0.;
     647        double prev_dist = 10000;
     648
     649        // search
     650        for(double e = 3.0; e <= 10000. ; e+=0.05) { // steps of 0.05 keV, start search
     651
     652                //evalTOT = surr->Eval(e);
     653                evalTOT = SurrogateCalibFunction(e, par);
     654                if(evalTOT < 0) continue; // don't consider those values. Not physical.
     655                dist = TMath::Abs(evalTOT - tot);
     656                //if(pix.first == 111 && pix.second == 70) cout << "dist = " << dist << " | e = " << e << " | evalTOT = " << evalTOT << " | tot = " << tot << endl;
     657                if( dist > prev_dist ) break; // if I am going far, break.
     658
     659                prev_dist = dist;
     660                prev_e = e; // keep the last energy
     661        }
     662
     663        return prev_e;
     664}
     665
     666int TimePixCalibration::GetTOT (pair<int, int> pix, double E) {
     667
     668        int index = MAFTools::XYtoC(pix, m_width);
     669        double par[] = { m_a[index], m_b[index], m_c[index], m_t[index] };
     670
     671        return SurrogateCalibFunction(E, par);
     672        //return GetSurrogateTF1(pix)->Eval(E);
     673}
     674
     675double TimePixCalibration::SurrogateCalibFunction(double x, double * par){
     676        return par[0]*x + par[1] - (par[2]/(x-par[3]));
     677}
     678
     679// Only if needed one can fetch a few functions as TF1 objects
     680TF1 * TimePixCalibration::GetSurrogateTF1(pair<int, int> pix){
     681
     682        // check first if the function has been defined.  Use the Set.
     683        m_surrogateItr = m_surrogateSet.find(pix);
     684
     685        // If surrogate TF1 does not exist yet, build the function first
     686        if( m_surrogateItr == m_surrogateSet.end() ) {
     687                TString ftitle = "surrogate_";
     688                ftitle += pix.first; ftitle += "_"; ftitle += pix.second;
     689                // Usually the parameters come given in keV !!! WARNING !!!
     690                m_surrogateMap[pix] = new TF1(ftitle,"[0]*x + [1] - ([2]/(x-[3]))",0,1000); // 1 MeV
     691                // Populate parameters.  Look for right index first
     692                int index = MAFTools::XYtoC(pix, m_width);
     693                m_surrogateMap[pix]->SetParameters(m_a[index], m_b[index], m_c[index], m_t[index]);
     694        }
     695
     696        return m_surrogateMap[pix];
     697}
     698
     699}
    512700#endif
  • mafalda/MAFTools/MAFTools.h

    r251 r285  
    77
    88#include <set>
     9#include <map>
    910#include <iostream>
    1011#include <vector>
     
    1617//class blob;
    1718class MediPixAlgo;
     19class Highlighter;
    1820//class AllBlobsContainer;
    1921//typedef struct steerSpiral;
     
    2224
    2325namespace MAFTools {
    24  
    25   Bool_t LinearRegression(Float_t * slope, Float_t * cut,
    26                           Float_t * chisquare_OverDof, Float_t * chisquare, Float_t * ndf,
    27                           Float_t initslope, Float_t initycross,
    28                           std::set< std::pair<Int_t, Int_t> > blobContent);
    2926
    30   Int_t XYtoC(pair<Int_t, Int_t>, Int_t);
    31   Int_t XYtoC(Int_t, Int_t, Int_t);
     27Bool_t LinearRegression(Float_t * slope, Float_t * cut,
     28                Float_t * chisquare_OverDof, Float_t * chisquare, Float_t * ndf,
     29                Float_t initslope, Float_t initycross,
     30                std::set< std::pair<Int_t, Int_t> > blobContent);
    3231
    33   Int_t ConvertXYPositionToInteger(std::pair<Int_t, Int_t> position, Int_t dimX, Int_t dimY);
     32Int_t XYtoC(pair<Int_t, Int_t>, Int_t);
     33Int_t XYtoC(Int_t, Int_t, Int_t);
    3434
    35   Int_t ConvertXYPositionToInteger(Int_t positionX, Int_t positionY, Int_t dimX, Int_t dimY);
    36  
    37   Double_t CalcDistance(std::pair<Double_t, Double_t>,
    38                         std::pair<Double_t, Double_t>);
    39   Double_t CalcDistance(Double_t, Double_t,
    40                         Double_t, Double_t);
    41  
    42   Double_t CalcPerpDistanceToLine(Float_t, Float_t, pair<Int_t, Int_t>);
    43   Double_t CalcPerpDistanceToLine(Float_t, Float_t, Int_t, Int_t);
     35Int_t ConvertXYPositionToInteger(std::pair<Int_t, Int_t> position, Int_t dimX, Int_t dimY);
     36
     37Int_t ConvertXYPositionToInteger(Int_t positionX, Int_t positionY, Int_t dimX, Int_t dimY);
     38
     39Double_t CalcDistance(std::pair<Double_t, Double_t>,
     40                std::pair<Double_t, Double_t>);
     41Double_t CalcDistance(Double_t, Double_t,
     42                Double_t, Double_t);
     43
     44Double_t CalcPerpDistanceToLine(Float_t, Float_t, pair<Int_t, Int_t>);
     45Double_t CalcPerpDistanceToLine(Float_t, Float_t, Int_t, Int_t);
    4446
    4547
    46   /* Drawing */
    47   void DrawLine(MediPixAlgo *, Float_t, Float_t, Float_t, Float_t, Int_t, Int_t, EColor);
     48/* Drawing */
     49void FillValuesForDisplay(Highlighter * hl, blob bl);
     50void DrawLine(MediPixAlgo *, Float_t, Float_t, Float_t, Float_t, Int_t, Int_t, EColor);
    4851
    49   /* Word handling */
    50   std::string DumpWordInBinary(UInt_t word);
     52/* Word handling */
     53std::string DumpWordInBinary(UInt_t word);
    5154
    52   // Blobs handling
    53   vector<pair<Float_t, Float_t> > FindLimitPixels(blob);
     55// Blobs handling
     56vector<pair<Float_t, Float_t> > FindLimitPixels(blob);
    5457
    55   /*
    56    * Check if a blob appears within a certain distance from the edges
    57    * Parameters:
    58    *  - blob
    59    *  - vector containing farthest pixels in the blob (see FindLimitPixels(blob))
    60    *  - Width of sensor in units of pixels
    61    *  - Height of sensor in units of pixels
    62    */
    63   Bool_t BlobAtADistanceFromEdges(blob, vector<pair<Float_t, Float_t> >, Int_t, Int_t, Int_t, Int_t);
     58/*
     59 * Check if a blob appears within a certain distance from the edges
     60 * Parameters:
     61 *  - blob
     62 *  - vector containing farthest pixels in the blob (see FindLimitPixels(blob))
     63 *  - Width of sensor in units of pixels
     64 *  - Height of sensor in units of pixels
     65 */
     66Bool_t BlobAtADistanceFromEdges(blob, vector<pair<Float_t, Float_t> >, Int_t, Int_t, Int_t, Int_t);
    6467
    65   /**
    66    * Clustering stuff
    67    */
    68   int DigitalBlobReclustering(int, AllBlobsContainer *, blob, int, int, std::map<int,int>);
    69   bool StartBlob(int, int, int, AllBlobsContainer *, blob *, int, int, std::map<int,int>);
    70   bool FollowBlob(blob *, steerSpiral *, int, int, int, std::map<int,int>);
    71   void RewindSpiral(steerSpiral *);
    72   Int_t GetLengthOfSpiral(Int_t);
    73   pair<Int_t, Int_t> GetNextPosition(pair<Int_t, Int_t>, steerSpiral *);
    74   bool IsUnsafePosition(pair<Int_t, Int_t>, int, int);
    75   void ClearOneBlobData(blob *);
     68/**
     69 * Clustering stuff
     70 */
     71int DigitalBlobReclustering(int, AllBlobsContainer *, blob, int, int, std::map<int,int>);
     72bool StartBlob(int, int, int, AllBlobsContainer *, blob *, int, int, std::map<int,int>);
     73bool FollowBlob(blob *, steerSpiral *, int, int, int, std::map<int,int>);
     74void RewindSpiral(steerSpiral *);
     75Int_t GetLengthOfSpiral(Int_t);
     76pair<Int_t, Int_t> GetNextPosition(pair<Int_t, Int_t>, steerSpiral *);
     77bool IsUnsafePosition(pair<Int_t, Int_t>, int, int);
     78void ClearOneBlobData(blob *);
    7679
    77   /* Dump contents of a blob */
    78   void DumpBlobContents(blob);
     80/* Dump contents of a blob */
     81void DumpBlobContents(blob);
    7982
    80   int FindDiscontinuity(blob);
     83int FindDiscontinuity(blob);
     84
     85
     86///////////////////////////////////////////////
     87// TimePix specific
     88
     89class TimePixCalibration {
     90
     91public:
     92        // Provide the 4 calibration files, a,b,c,t
     93        TimePixCalibration(){};
     94        TimePixCalibration(const char *, const char *, const char *, const char *, int, int);
     95        ~TimePixCalibration();
     96
     97        double SurrogateCalibFunction(double x, double *);
     98        int GetTOT(pair<int, int>, double);
     99        double GetE(pair<int, int>, int);
     100
     101        TF1 * GetSurrogateTF1(pair<int, int>);
     102
     103private:
     104
     105        double * m_a;
     106        double * m_b;
     107        double * m_c;
     108        double * m_t;
     109
     110        int m_width;
     111        int m_height;
     112
     113        map<pair<int, int>, TF1* > m_surrogateMap;
     114        set< pair<int, int> > m_surrogateSet; // verify content
     115        set< pair<int, int> >::iterator m_surrogateItr;
     116
     117};
     118
    81119}
    82120
Note: See TracChangeset for help on using the changeset viewer.