From 4ee0864472156670af862748e875242ce7ae92a4 Mon Sep 17 00:00:00 2001
From: Hernan Asorey <asoreyh@gmail.com>
Date: Wed, 13 Jan 2016 11:25:00 -0300
Subject: [PATCH] Calculation of signal characteristic times and signal
 fractions oriented to improve muon/EM signal separation

---
 dump.cc     | 11 ++++++++---
 lago_data.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 lago_defs.h |  1 +
 3 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/dump.cc b/dump.cc
index aeb9ef1..965167e 100644
--- a/dump.cc
+++ b/dump.cc
@@ -58,12 +58,17 @@ int main (int argc, char **argv) {
   //
   // File reading and processing
   //
+  double rt=0., ft=0., tt=0.;
   cerr << "Reading file" << endl;
   while(NbPulses!=-1) {
     NbPulses=Input.ReadOneSecond(&Data,Pulse,MAXPULSEPERSEC);
-	for (int j=CHANNELS-1; j>=0; j--)
-		cout << Pulse[0].IsTriggered(j) << " ";
-	cout << endl;
+	for (int i=0; i<NbPulses; i++) {
+		rt = Pulse[i].GetRiseTime(2);
+		ft = Pulse[i].GetFallTime(2);
+		tt = Pulse[i].GetFullTime(2);
+		if (rt>0 && ft>0)
+			cout << rt << " " << ft << " " << tt << endl;
+	}
   }
   return 0;
 }
diff --git a/lago_data.h b/lago_data.h
index c5f5825..ebf05b1 100644
--- a/lago_data.h
+++ b/lago_data.h
@@ -139,7 +139,55 @@ class LagoEvent {
       return charge;
     }
 
-    void dump() {
+	double GetTfraction(int channel, int fraction) {
+		if (fraction < 10 || fraction > 100)
+			return -1;
+		double charge = (double) GetCharge(channel);
+		if (!charge)
+			return -2;
+		int i=0;
+		double s = 0.;
+		double dy = 0.;
+		double x = 0.;
+		double limit = fraction / 100.;
+		for (i=0; i<currentbinfilled; i++) {
+			dy = 1.0 * (trace[channel][i] - BASELINE) / charge;
+			s += dy;
+			if (s >= limit)
+				break;
+		}
+		if (!i)
+			return -3;
+		if (!dy)// no changes from previous bin? should not happen thanks to the equal sign in the comparisson, but just in case
+			return ((i-1.)*BIN); // return previous bin
+		x = (1.0 * i - ((s - limit) / dy)) * BIN;
+		return x;
+	}
+
+	double GetCharTime(int channel, int up, int low) {
+		if (up < low)
+			return -1; // stupid user
+		double Tu=GetTfraction(channel,up);
+		double Tl=GetTfraction(channel,low);
+		if (Tu >= 0 && Tl >= 0) 
+			return (Tu-Tl);
+		else
+			return -1;
+	}
+	
+	double GetRiseTime (int channel) {
+		return GetCharTime(channel,50,10);
+	}
+	
+	double GetFallTime (int channel) {
+		return GetCharTime(channel,90,50);
+	}
+	
+	double GetFullTime (int channel) {
+		return GetCharTime(channel,90,10);
+	}
+    
+	void dump() {
       std::cout 
 		  << "# trg: " << trigger << " cnt: " << counter << " clk: " << clockcount << " " 
 		  << std::endl;
diff --git a/lago_defs.h b/lago_defs.h
index b33a193..d3d5044 100644
--- a/lago_defs.h
+++ b/lago_defs.h
@@ -38,5 +38,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 #define CHANNELS 3
 #define TRACELEN 12
 #define BASELINE 50
+#define BIN 25.
 
 #endif
-- 
GitLab