ROOT logo
#ifndef HMDCOFFSET_H
#define HMDCOFFSET_H

#include "hreconstructor.h"
#include "hstartdef.h"
#include "hmdctimecut.h"
#include "hlocation.h"

typedef Int_t   MyInt;
typedef Float_t MyFloat;
typedef TH1F    MyHist;

class TFile;
class HCategory;
class HIterator;
class HMdcCalPar;
class HMdcCalParRaw;
class HMdcLookupGeom;
class HMdcLookupRaw;
class HMdcTimeCut;
class HMdcSizesCells;
class TH2F;
class TNtuple;
class TF1;
class TString;

typedef MyInt MyField[6][4][16][96][2048];

class HMdcOffset : public HReconstructor {
protected:
    static const Int_t nbin;            // Number of bins ih the histogramms
    static const Int_t nbinm1;          // nbin - 1
    static const Int_t nbinp1;          // nbin + 1
    static const Int_t nSubEvents;      // The max. number of objects per event
    HCategory*         rawCat;          // pointer to the raw data
    HCategory*         hitStartCat;     // pointer to the cal data
    HCategory*         clusCat;         // pointer to the cluster data
    HIterator*         iter;            // iterator on raw data
    HIterator*         iter_start;      // iterator on start cal data.
    HIterator*         iter_clus;       // iterator on clus data.
    HLocation          locraw;          //
    HMdcCalParRaw*     calparraw;       // pointer to HMdcCalParRaw container
    HMdcTimeCut*       timecut;         // pointer to HMdcTimeCut container
    HMdcLookupGeom*    lookupgeom;      // pointer to HMdcLookupGeom container
    HMdcLookupRaw*     lookupraw;       // pointer to HMdcLookupRaw container
    HMdcSizesCells*    sizescells;      // pointer to HMdcSizesCells container

    Float_t   signalspeed;              //! speed of the signal on the wire [ns/mm]
    Int_t     eventcounter;
    Int_t     skipcounter;
    Int_t     nSkipEvents;              // number of skipped events per file
    Int_t     nStep;                    // step size for print events
    Float_t   validRange;               // allowed interval arround mean of offsets
    Bool_t    isPulserFile;             // flag for a external calibration file (pulser)
    Bool_t    noStart;                  // flag if starttime should not be used
    Bool_t    useTimeCuts;              // switch on/off time cuts usage
    Bool_t    useClusters;              // switch on/off clusters/raw
    Bool_t    useWireOffset;            // switch on/off substraction of signal time on wire
    Bool_t    useTof;                   // switch on/off substraction of minimum tof
    Bool_t    fillHistsOnly;            // switch to fill hists only (no offset calculation)
    Bool_t    readHists;                // switch to read hists from root file and than calculate offset
    Bool_t    perMBO;                   // switch to calc offsets per MBO
    Bool_t    perMBOafterSingle;        // switch to calc offsets per MBO from single offsets
    Float_t   filterwindow1;            // wide window arround mean to filter (perMBOafterSingle)
    Float_t   filterwindow2;            // narrow window arround mean to filter (perMBOafterSingle)
    Int_t     filenumber;               // counter for the read histogram files
    Char_t*   fNameAsciiOffset;         // file name of ascii output file
    Char_t*   fNameRootOffset;          // file name of root output file
    FILE*     ferrorlog;                // File pointer to errorlog
    TNtuple*  offsetTuple;              // pointer to NTuple for offset
    TNtuple*  offsetPulserTuple;        // pointer to NTuple for multiple peaks of pulser files
    Float_t   minfitthreshold;          // minimum threshold for linear fit (y-range)
    Float_t   maxfitthreshold;          // maximum threshold for linear fit (y-range)

    Int_t     offsetfitNoise;           // offset of the fit range for the subtraction of the noise (start at yequalzero going to the left)
    Int_t     widthfitNoise;            // width of the fit range
    Bool_t    fitNoise;                 // fit noise or don't

    Int_t     rangeGauss;               // fit range of the gaussian fit around calculated offset

    Int_t     cleanThreshold;           // threshold to clean hinv : bins with less than the threshold will be set to 0

    Float_t   cutT1L [4];                // lower cut in t1 (ns reverse) per module
    Float_t   cutT1R [4];                // upper cut in t1 (ns reverse) per module
    Float_t   cutT12 [4];                // cut in t12 (ns reverse) per module
    Bool_t    useCuts;                  // use build in cuts

    MyField*  hreverse;                 // array for the drift-time (Time1) to be filled in eventloop
    MyHist*   hinv;                     // drift-time histograms to hold collected data
    MyHist*   hint;                     // integrated drift-time histograms
    MyHist*   htime1temp;               // temp drift-time histogram
    TH2F*     htime1_mbo        [16];   // 2-D hist time1 vers tdc
    TH2F*     htime1_lay         [6];   // 2-D hist time1 vers cell
    TH2F*     htime1_lay_inv_norm[6];   // 2-D hist time1 vers cell normalized to counts
    TH2F*     htime1_lay_int     [6];   // 2-D hist integrated time1 vers cell
    TH2F*     htime1_lay_int_norm[6];   // 2-D hist integrated time1 vers cell normalized to counts
    TF1*      toffunc         [4][6];   // TF1 fits for minimum tof
    MyFloat   yequalzero;               // intersection point of the first  fit and the x-axis
    MyFloat   crosspointX;              // x-value of the intersection point of the two linear fits->offset

    MyFloat   fitpar0;                  // first fit parameter of the first linear fit
    MyFloat   fitpar0error;             // error of the first fit parameter of the first linear fit
    MyFloat   fitpar1;                  // second fit parameter of the first linear fit
    MyFloat   fitpar1error;             // error of the second fit parameter of the first linear fit

    MyFloat   fitparNoise0;             // first fit parameter of the second linear fit
    MyFloat   fitparNoise0error;        // error of the first fit parameter of the second linear fit
    MyFloat   fitparNoise1;             // second fit parameter of the second linear fit
    MyFloat   fitparNoise1error;        // error of the second fit parameter of the second linear fit
    MyFloat   totalsigma;               // sigma of the offset calculated from the sigmas of the two linear fits


    MyFloat   fitGaussMean;             // mean of the gaussian fit
    MyFloat   fitGaussSigma;            // sigma of the gaussian fit

    Float_t   meanhOffset;
    Float_t   offsets     [6][4][16][96];
    Float_t   offsetErr   [6][4][16][96];
    Float_t   offset1     [6][4][16][96];
    Int_t     integral    [6][4][16][96];
    Float_t   fitslope1   [6][4][16][96];
    Float_t   fitslope2   [6][4][16][96];
    Float_t   offsetpulser[6][4][16][96][5];
    Float_t   myoffset;
    Float_t   myerror;
    Bool_t    debug;                     //! debug flag
public:
    HMdcOffset();
    HMdcOffset(const Text_t* name,const Text_t* title);
    ~HMdcOffset();
    
    Bool_t           init();
    Bool_t           reinit();

    Bool_t           finalize();
    Int_t            execute();

    void             setSignalSpeed     (Float_t speed)             { signalspeed     = speed; }
    void             setCounter         (Int_t size)                { nStep           = size; }
    void             setOutputAscii     (const Char_t*);
    void             setOutputRoot      (const Char_t*);
    void             setPulserFile      ()                          { isPulserFile    = kTRUE; }
    void             setNotUseStart     (Bool_t nouse)              { noStart         = nouse; }
    void             setUseClusters     (Bool_t use)                { useClusters     = use; }
    void             setUseWireOffset   (Bool_t use)                { useWireOffset   = use; }
    void             setUseTof          (TString inp);
    void             setFillHistsOnly   (Bool_t fill)               { fillHistsOnly   = fill; }
    void             setReadHists       (TString inp,Bool_t print=kFALSE);
    void             setFillPerMBO      (Bool_t fillPerMBO)         { perMBO          = fillPerMBO; }
    void             setFillPerMBOafterSingle(Bool_t fillPerMBO)    { perMBOafterSingle = fillPerMBO; }
    void             setFilterWindow1   (Float_t window)            { filterwindow1   = window;}
    void             setFilterWindow2   (Float_t window)            { filterwindow2   = window;}
    void             setNSkipEvents     (Int_t nskipevents)         { nSkipEvents     = nskipevents; }
    void             setValidOffsetRange(Float_t range)             { validRange      = range; }
    void             setUseTimeCuts     (Bool_t cut)                { useTimeCuts     = cut; }
    void             setNoiseOffset     (Int_t i)                   { offsetfitNoise  = i; }
    void             setNoiseWidth      (Int_t i)                   { widthfitNoise   = i; }
    void             setThresholdMin    (Float_t f)                 { minfitthreshold = f; }
    void             setThresholdMax    (Float_t f)                 { maxfitthreshold = f; }
    void             setNoise           (Int_t o, Int_t w)          { offsetfitNoise  = o; widthfitNoise = w; }
    void             setFitNoise        (Bool_t fit)                { fitNoise        = fit;}
    void             setThreshold       (Float_t min, Float_t max)  { minfitthreshold = min; maxfitthreshold = max; }
    void             setRangeGauss      (Int_t i)                   { rangeGauss      = i; }
    void             setDebug           ()                          { debug           = kTRUE; }
    void             setCleanThreshold  (Int_t thresh)              { cleanThreshold  = thresh; }
    void             setCuts(Float_t c1l[],Float_t c1r[],Float_t c12[]){
	for(Int_t i=0;i<4;i++){
	    cutT1L[i]=c1l[i];
	    cutT1R[i]=c1r[i];
	    cutT12[i]=c12[i];
	}
	useCuts = kTRUE;
    }


protected:
    Int_t            executeRaw         ();
    Int_t            executeClus        ();
    void             setDefault         ();
    ofstream*        openAsciiFile      ();
    TDirectory*      Mkdir              (TDirectory *,const Char_t *, Int_t, Int_t p=1);
    void             createHist         (TFile*,Int_t, Int_t, Int_t, Int_t, Bool_t = kFALSE);
    void             createHist_2D      (Int_t, Int_t, Bool_t = kFALSE);
    void             fillHist           (Int_t, Int_t, Int_t, Int_t);
    void             fillHist_2D        (Int_t, Int_t, Int_t, Int_t);
    Int_t            fitHist            (Int_t, Int_t, Int_t, Int_t);
    void             writeHist          (TFile*);
    void             writeHist_2D       (Int_t s, Int_t m);
    void             deleteHist         ();
    void             deleteHist_2D      ();
    void             fillNTuples        (Int_t, Int_t, Int_t, Int_t);
    Float_t          getstarttime       ();
    void             writeAscii         (ofstream&, Int_t, Int_t, Int_t, Int_t);
    void             initVariables      ();
    void             findMultiplePeaks  (Int_t,Int_t,Int_t,Int_t);
    void             initMemory         ()
    {
	for(Int_t s = 0;s < 6; s ++){
	    for(Int_t m = 0; m < 4; m ++){
		for(Int_t mb = 0; mb < 16; mb ++){
		    for(Int_t t = 0; t <96; t ++){
			for(Int_t bin = 0; bin < 2048; bin ++){
			    (*hreverse) [s][m][mb][t][bin] = 0;
			}
		    }
		}
	    }
	}
    }
    void              initArrays         ()
    {
	for(Int_t s = 0; s < 6; s ++){
	    for(Int_t mo = 0; mo < 4; mo ++){
		for(Int_t mb = 0; mb < 16; mb ++){
		    for(Int_t t = 0; t < 96; t ++){
			offsets  [s][mo][mb][t] = 0;
			offsetErr[s][mo][mb][t] = 0;
			offset1  [s][mo][mb][t] = 0;
			integral [s][mo][mb][t] = 0;
                        fitslope1[s][mo][mb][t] = 0;
                        fitslope2[s][mo][mb][t] = 0;
			for(Int_t j = 0; j < 5; j ++)
			{
			    offsetpulser[s][mo][mb][t][j] = 0;
			}
		    }
		}
	    }
	}
    }
    Bool_t            testTimeCuts       (Int_t s, Int_t m,Float_t t1,Float_t t2)
    {
	if(useCuts)
	{
	    if(t1>=cutT1L[m] && t1<=cutT1R[m] && fabs(t2-t1)>cutT12[m]) return kTRUE;
            else                                               return kFALSE;


	} else {

	    if(timecut->cutTime1   (s,m,t1)  &&
	       timecut->cutTime2   (s,m,t2)  &&
	       timecut->cutTimesDif(s,m,t2,t1)){ return kTRUE; }
	    else { return kFALSE; }
	}

    }

    void             fillArrays          (TH1F*,Int_t, Int_t, Int_t, Int_t);
    void             fillCalParRaw       (TH1F*,Int_t, Int_t, Int_t, Int_t);
    void             printStatus         ();

public: // This has to be placed at the end (--> root dox)
    ClassDef(HMdcOffset, 0)  // class for the calculation of the offsets per tdc channel
};

#endif /* !HMDCOFFSET_H */
 hmdcoffset.h:1
 hmdcoffset.h:2
 hmdcoffset.h:3
 hmdcoffset.h:4
 hmdcoffset.h:5
 hmdcoffset.h:6
 hmdcoffset.h:7
 hmdcoffset.h:8
 hmdcoffset.h:9
 hmdcoffset.h:10
 hmdcoffset.h:11
 hmdcoffset.h:12
 hmdcoffset.h:13
 hmdcoffset.h:14
 hmdcoffset.h:15
 hmdcoffset.h:16
 hmdcoffset.h:17
 hmdcoffset.h:18
 hmdcoffset.h:19
 hmdcoffset.h:20
 hmdcoffset.h:21
 hmdcoffset.h:22
 hmdcoffset.h:23
 hmdcoffset.h:24
 hmdcoffset.h:25
 hmdcoffset.h:26
 hmdcoffset.h:27
 hmdcoffset.h:28
 hmdcoffset.h:29
 hmdcoffset.h:30
 hmdcoffset.h:31
 hmdcoffset.h:32
 hmdcoffset.h:33
 hmdcoffset.h:34
 hmdcoffset.h:35
 hmdcoffset.h:36
 hmdcoffset.h:37
 hmdcoffset.h:38
 hmdcoffset.h:39
 hmdcoffset.h:40
 hmdcoffset.h:41
 hmdcoffset.h:42
 hmdcoffset.h:43
 hmdcoffset.h:44
 hmdcoffset.h:45
 hmdcoffset.h:46
 hmdcoffset.h:47
 hmdcoffset.h:48
 hmdcoffset.h:49
 hmdcoffset.h:50
 hmdcoffset.h:51
 hmdcoffset.h:52
 hmdcoffset.h:53
 hmdcoffset.h:54
 hmdcoffset.h:55
 hmdcoffset.h:56
 hmdcoffset.h:57
 hmdcoffset.h:58
 hmdcoffset.h:59
 hmdcoffset.h:60
 hmdcoffset.h:61
 hmdcoffset.h:62
 hmdcoffset.h:63
 hmdcoffset.h:64
 hmdcoffset.h:65
 hmdcoffset.h:66
 hmdcoffset.h:67
 hmdcoffset.h:68
 hmdcoffset.h:69
 hmdcoffset.h:70
 hmdcoffset.h:71
 hmdcoffset.h:72
 hmdcoffset.h:73
 hmdcoffset.h:74
 hmdcoffset.h:75
 hmdcoffset.h:76
 hmdcoffset.h:77
 hmdcoffset.h:78
 hmdcoffset.h:79
 hmdcoffset.h:80
 hmdcoffset.h:81
 hmdcoffset.h:82
 hmdcoffset.h:83
 hmdcoffset.h:84
 hmdcoffset.h:85
 hmdcoffset.h:86
 hmdcoffset.h:87
 hmdcoffset.h:88
 hmdcoffset.h:89
 hmdcoffset.h:90
 hmdcoffset.h:91
 hmdcoffset.h:92
 hmdcoffset.h:93
 hmdcoffset.h:94
 hmdcoffset.h:95
 hmdcoffset.h:96
 hmdcoffset.h:97
 hmdcoffset.h:98
 hmdcoffset.h:99
 hmdcoffset.h:100
 hmdcoffset.h:101
 hmdcoffset.h:102
 hmdcoffset.h:103
 hmdcoffset.h:104
 hmdcoffset.h:105
 hmdcoffset.h:106
 hmdcoffset.h:107
 hmdcoffset.h:108
 hmdcoffset.h:109
 hmdcoffset.h:110
 hmdcoffset.h:111
 hmdcoffset.h:112
 hmdcoffset.h:113
 hmdcoffset.h:114
 hmdcoffset.h:115
 hmdcoffset.h:116
 hmdcoffset.h:117
 hmdcoffset.h:118
 hmdcoffset.h:119
 hmdcoffset.h:120
 hmdcoffset.h:121
 hmdcoffset.h:122
 hmdcoffset.h:123
 hmdcoffset.h:124
 hmdcoffset.h:125
 hmdcoffset.h:126
 hmdcoffset.h:127
 hmdcoffset.h:128
 hmdcoffset.h:129
 hmdcoffset.h:130
 hmdcoffset.h:131
 hmdcoffset.h:132
 hmdcoffset.h:133
 hmdcoffset.h:134
 hmdcoffset.h:135
 hmdcoffset.h:136
 hmdcoffset.h:137
 hmdcoffset.h:138
 hmdcoffset.h:139
 hmdcoffset.h:140
 hmdcoffset.h:141
 hmdcoffset.h:142
 hmdcoffset.h:143
 hmdcoffset.h:144
 hmdcoffset.h:145
 hmdcoffset.h:146
 hmdcoffset.h:147
 hmdcoffset.h:148
 hmdcoffset.h:149
 hmdcoffset.h:150
 hmdcoffset.h:151
 hmdcoffset.h:152
 hmdcoffset.h:153
 hmdcoffset.h:154
 hmdcoffset.h:155
 hmdcoffset.h:156
 hmdcoffset.h:157
 hmdcoffset.h:158
 hmdcoffset.h:159
 hmdcoffset.h:160
 hmdcoffset.h:161
 hmdcoffset.h:162
 hmdcoffset.h:163
 hmdcoffset.h:164
 hmdcoffset.h:165
 hmdcoffset.h:166
 hmdcoffset.h:167
 hmdcoffset.h:168
 hmdcoffset.h:169
 hmdcoffset.h:170
 hmdcoffset.h:171
 hmdcoffset.h:172
 hmdcoffset.h:173
 hmdcoffset.h:174
 hmdcoffset.h:175
 hmdcoffset.h:176
 hmdcoffset.h:177
 hmdcoffset.h:178
 hmdcoffset.h:179
 hmdcoffset.h:180
 hmdcoffset.h:181
 hmdcoffset.h:182
 hmdcoffset.h:183
 hmdcoffset.h:184
 hmdcoffset.h:185
 hmdcoffset.h:186
 hmdcoffset.h:187
 hmdcoffset.h:188
 hmdcoffset.h:189
 hmdcoffset.h:190
 hmdcoffset.h:191
 hmdcoffset.h:192
 hmdcoffset.h:193
 hmdcoffset.h:194
 hmdcoffset.h:195
 hmdcoffset.h:196
 hmdcoffset.h:197
 hmdcoffset.h:198
 hmdcoffset.h:199
 hmdcoffset.h:200
 hmdcoffset.h:201
 hmdcoffset.h:202
 hmdcoffset.h:203
 hmdcoffset.h:204
 hmdcoffset.h:205
 hmdcoffset.h:206
 hmdcoffset.h:207
 hmdcoffset.h:208
 hmdcoffset.h:209
 hmdcoffset.h:210
 hmdcoffset.h:211
 hmdcoffset.h:212
 hmdcoffset.h:213
 hmdcoffset.h:214
 hmdcoffset.h:215
 hmdcoffset.h:216
 hmdcoffset.h:217
 hmdcoffset.h:218
 hmdcoffset.h:219
 hmdcoffset.h:220
 hmdcoffset.h:221
 hmdcoffset.h:222
 hmdcoffset.h:223
 hmdcoffset.h:224
 hmdcoffset.h:225
 hmdcoffset.h:226
 hmdcoffset.h:227
 hmdcoffset.h:228
 hmdcoffset.h:229
 hmdcoffset.h:230
 hmdcoffset.h:231
 hmdcoffset.h:232
 hmdcoffset.h:233
 hmdcoffset.h:234
 hmdcoffset.h:235
 hmdcoffset.h:236
 hmdcoffset.h:237
 hmdcoffset.h:238
 hmdcoffset.h:239
 hmdcoffset.h:240
 hmdcoffset.h:241
 hmdcoffset.h:242
 hmdcoffset.h:243
 hmdcoffset.h:244
 hmdcoffset.h:245
 hmdcoffset.h:246
 hmdcoffset.h:247
 hmdcoffset.h:248
 hmdcoffset.h:249
 hmdcoffset.h:250
 hmdcoffset.h:251
 hmdcoffset.h:252
 hmdcoffset.h:253
 hmdcoffset.h:254
 hmdcoffset.h:255
 hmdcoffset.h:256
 hmdcoffset.h:257