+
+ //Evaluate time
+ fTime = sigStart-sigLength-3;
+ const Int_t nLine= 6 ; //Parameters of fitting
+ const Float_t eMinTOF = 10. ; //Choosed from beam-test and cosmic analyis
+ const Float_t kAmp=0.35 ; //Result slightly depends on them, so no getters
+ // Avoid too low peak:
+ if(fEnergy < eMinTOF){
+ return kTRUE;
+ }
+
+ // Find index posK (kLevel is a level of "timestamp" point Tk):
+ Int_t posK =sigLength-1 ; //last point before crossing k-level
+ Double_t levelK = pedestal + kAmp*fEnergy;
+ while(signal[posK] <= levelK && posK>=0){
+ posK-- ;
+ }
+ posK++ ;
+
+ if(posK == 0 || posK==sigLength-1){
+ return kTRUE;
+ }
+
+ // Find crosing point by solving linear equation (least squares method)
+ Int_t np = 0;
+ Int_t iup=posK-1 ;
+ Int_t idn=posK ;
+ Double_t sx = 0., sy = 0., sxx = 0., sxy = 0.;
+ Double_t x,y ;
+
+ while(np<nLine){
+ //point above crossing point
+ if(iup>=0){
+ x = sigLength-iup-1;
+ y = signal[iup];
+ sx += x;
+ sy += y;
+ sxx += (x*x);
+ sxy += (x*y);
+ np++ ;
+ iup-- ;
+ }
+ //Point below crossing point
+ if(idn<sigLength){
+ if(signal[idn]<pedestal){
+ idn=sigLength-1 ; //do not scan further
+ idn++ ;
+ continue ;
+ }
+ x = sigLength-idn-1;
+ y = signal[idn];
+ sx += x;
+ sy += y;
+ sxx += (x*x);
+ sxy += (x*y);
+ np++;
+ idn++ ;
+ }
+ if(idn>=sigLength && iup<0){
+ break ; //can not fit futher
+ }
+ }
+
+ Double_t det = np*sxx - sx*sx;
+ if(det == 0){
+ return kTRUE;
+ }
+ if(np == 0){
+ return kFALSE;
+ }
+ Double_t c1 = (np*sxy - sx*sy)/det; //slope
+ Double_t c0 = (sy-c1*sx)/np; //offset
+ if(c1 == 0){
+ return kTRUE;
+ }
+
+ // Find where the line cross kLevel:
+ fTime += (levelK - c0)/c1-5. ; //5: mean offset between k-Level and start times