From fda01b2e725869f515ebb147056e0a3acdbac73b Mon Sep 17 00:00:00 2001
From: fuchenxi <1256257282@qq.com>
Date: Sun, 22 Sep 2024 23:13:45 +0800
Subject: [PATCH] =?UTF-8?q?ele=5Freadout=E6=94=B9=E5=90=8Dreadout=E5=B9=B6?=
 =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 elec/__init__.py                    |  3 ++-
 elec/{ele_readout.py => readout.py} | 37 +++++++++++++++++++++++------
 gen_signal/draw_save.py             |  6 ++---
 gen_signal/gen_signal_main.py       |  6 ++---
 tct/tct_signal.py                   |  4 ++--
 util/math.py                        |  2 +-
 6 files changed, 41 insertions(+), 17 deletions(-)
 rename elec/{ele_readout.py => readout.py} (81%)

diff --git a/elec/__init__.py b/elec/__init__.py
index 935f8c5..ff61482 100644
--- a/elec/__init__.py
+++ b/elec/__init__.py
@@ -12,4 +12,5 @@ def main(kwargs):
         from . import drs4_get_fig
         drs4_get_fig.main()
     else:
-        raise NameError(label)
\ No newline at end of file
+        from . import readout
+        readout.main(label)
diff --git a/elec/ele_readout.py b/elec/readout.py
similarity index 81%
rename from elec/ele_readout.py
rename to elec/readout.py
index 8bd795b..2d5235c 100644
--- a/elec/ele_readout.py
+++ b/elec/readout.py
@@ -15,6 +15,7 @@ import json
 import ROOT
 
 from util.math import signal_convolution
+from util.output import output
 
 time_step = 50e-12
 
@@ -53,13 +54,13 @@ class Amplifier:
         2024/09/14
     """
     def __init__(self, currents: list[ROOT.TH1F], amplifier_name: str, time_step = time_step):
-        self.ele = []
+        self.amplified_current = []
 
         ele_json = "./setting/electronics/" + amplifier_name + ".json"
         with open(ele_json) as f:
             self.amplifier_parameters = json.load(f)
 
-        self.ele_name = self.amplifier_parameters['ele_name']
+        self.amplified_current_name = self.amplifier_parameters['ele_name']
         self.read_ele_num = len(currents)
 
         self.amplifier_define()
@@ -166,14 +167,36 @@ class Amplifier:
             n_bin = cu.GetNbinsX()
             t_bin = cu.GetBinWidth(0)
             time_duration = n_bin * t_bin
-            self.ele.append(ROOT.TH1F("electronics %s"%(self.ele_name)+str(i+1), "electronics %s"%(self.ele_name),
+            self.amplified_current.append(ROOT.TH1F("electronics %s"%(self.amplified_current_name)+str(i+1), "electronics %s"%(self.amplified_current_name),
                                 int(time_duration/time_step), 0, time_duration))
-            self.ele[i].Reset()
-            signal_convolution(cu, self.pulse_responce, self.ele[i])
+            self.amplified_current[i].Reset()
+            signal_convolution(cu, self.pulse_responce, self.amplified_current[i])
     
     def set_scope_output(self, currents: list[ROOT.TH1F]):
         for i in range(self.read_ele_num):
             cu = currents[i]
             input_Q_tot = cu.Integral()
-            output_Q_max = self.ele[i].GetMaximum()
-            self.ele[i].Scale(self.scale(output_Q_max, input_Q_tot))
+            output_Q_max = self.amplified_current[i].GetMaximum()
+            self.amplified_current[i].Scale(self.scale(output_Q_max, input_Q_tot))
+
+def main(label):
+    my_th1f = ROOT.TH1F("my_th1f", "my_th1f", 200, 0, 10e-9)
+    # input signal: square pulse
+    for i in range(21, 41):
+        my_th1f.SetBinContent(i, 1e-5)
+    ele = Amplifier([my_th1f], label)
+
+    c=ROOT.TCanvas("c","canvas1",1000,1000)
+    my_th1f.Draw("HIST")
+
+    origin_max = my_th1f.GetMaximum()
+    amp_max = ele.amplified_current[0].GetMaximum()
+    ratio = origin_max/amp_max
+    ele.amplified_current[0].Scale(ratio)
+    ele.amplified_current[0].Draw("SAME HIST")
+    path = output(__file__, label)
+    c.SaveAs(path+'/'+label+'_test.pdf')
+
+if __name__ == '__main__':
+    import sys
+    main(sys.argv[1])
\ No newline at end of file
diff --git a/gen_signal/draw_save.py b/gen_signal/draw_save.py
index 0eba851..ad21320 100644
--- a/gen_signal/draw_save.py
+++ b/gen_signal/draw_save.py
@@ -38,7 +38,7 @@ def draw_plots(my_d,ele_current,my_f,my_g4p,my_current,my_l=None,laser_path=None
         draw_ele_field_1D(my_d,my_f,path)
         draw_ele_field(my_d,my_f,"xz",my_d.det_model,my_d.l_y*0.5,path)
     for i in range(my_current.read_ele_num):
-        draw_current(my_d, my_current,ele_current.ele,i,ele_current.ele_name,path) # Draw current
+        draw_current(my_d, my_current,ele_current.amplified_current,i,ele_current.amplified_current_name,path) # Draw current
     #energy_deposition(my_g4p)   # Draw Geant4 depostion distribution
     if my_l != None:
         my_l.draw_nocarrier3D(path)
@@ -73,9 +73,9 @@ def save_signal_csv(ele_current,my_g4p,number,start_n,k,output_path="none"):
     output_file = output_path + "/t_" +str(number)+charge+e_dep+"events.csv"
     f1 = open(output_file,"w")
     f1.write("time[ns], Amplitude [mV] \n")
-    for i in range(ele_current.ele[k].GetNbinsX()):
+    for i in range(ele_current.amplified_current[k].GetNbinsX()):
         f1.write("%s,%s \n"%(i*ele_current.time_unit,
-                                ele_current.ele[k][i]))
+                                ele_current.amplified_current[k][i]))
     f1.close()
 
     print("output_file:%s"%output_file)
diff --git a/gen_signal/gen_signal_main.py b/gen_signal/gen_signal_main.py
index 2a05157..12a1931 100644
--- a/gen_signal/gen_signal_main.py
+++ b/gen_signal/gen_signal_main.py
@@ -17,7 +17,7 @@ from field import build_device as bdv
 from particle import g4simulation as g4s
 from field import devsim_field as devfield
 from current import cal_current as ccrt
-from elec import ele_readout as rdout
+from elec import readout as rdo
 from elec import ngspice_set_input as ngsip
 from elec import ngspice as ng
 
@@ -101,7 +101,7 @@ def main(kwargs):
         subprocess.run(['ngspice -b -r t0.raw output/T0_tmp.cir'], shell=True)
         ng.plot_waveform()
     else:
-        ele_current = rdout.Amplifier(my_current.sum_cu, amplifier)
+        ele_current = rdo.Amplifier(my_current.sum_cu, amplifier)
         draw_save.draw_plots(my_d,ele_current,my_f,my_g4p,my_current)
     
     del my_f
@@ -145,7 +145,7 @@ def batch_loop(my_d, my_f, my_g4p, amplifier, g4_seed, total_events, instance_nu
         if len(my_g4p.p_steps[event-start_n]) > 5:
             effective_number += 1
             my_current = ccrt.CalCurrentG4P(my_d, my_f, my_g4p, event-start_n)
-            ele_current = rdout.Amplifier(my_current.sum_cu, amplifier)
+            ele_current = rdo.Amplifier(my_current.sum_cu, amplifier)
             draw_save.save_signal_time_resolution(my_d,event,ele_current,my_g4p,start_n,my_f)
             del ele_current
     detection_efficiency =  effective_number/(end_n-start_n) 
diff --git a/tct/tct_signal.py b/tct/tct_signal.py
index f4f9789..2af29b6 100644
--- a/tct/tct_signal.py
+++ b/tct/tct_signal.py
@@ -14,7 +14,7 @@ import ROOT
 from field import build_device as bdv
 from field import devsim_field as devfield
 from current import cal_current as ccrt
-from elec import ele_readout as rdout
+from elec import readout as rdo
 from elec import ngspice_set_input as ngsip
 from elec import ngspice as ng
 from .source import TCTTracks
@@ -85,7 +85,7 @@ def main(kwargs):
         subprocess.run(['ngspice -b -r t0.raw output/T0_tmp.cir'], shell=True)
         ng.plot_waveform()
     else:
-        ele_current = rdout.Amplifier(my_current.sum_cu, amplifier)
+        ele_current = rdo.Amplifier(my_current.sum_cu, amplifier)
     
     if kwargs['scan'] != None: #assume parameter alter
         key = my_l.fz_rel
diff --git a/util/math.py b/util/math.py
index 2aca14f..017a5bb 100644
--- a/util/math.py
+++ b/util/math.py
@@ -107,4 +107,4 @@ def signal_convolution(signal_original: ROOT.TH1F, pulse_responce_function, sign
         so_i = so.GetBinContent(i)
         for j in range(-i,n_bin-i): 
             pr_j = pr(j*t_bin)
-            sc.Fill((i+j)*t_bin - 1e-14, so_i*pr_j*t_bin) # 1e-14 resolves float error
+            sc.Fill((i+j)*t_bin + 1e-14, so_i*pr_j*t_bin) # 1e-14 resolves float error
-- 
GitLab