File indexing completed on 2025-08-03 08:13:31
0001 from ROOT import *
0002 import sys
0003
0004
0005
0006 def merge_trees(outputfile, tree1, tree2) :
0007 outputfile.cd()
0008
0009 for branch in tree2.GetListOfBranches() :
0010 branch.SetTree(tree1)
0011 tree1.GetListOfBranches().Add(branch)
0012 tree1.GetListOfLeaves().Add(branch.GetLeaf(branch.GetName()))
0013 tree2.GetListOfBranches().Remove(branch)
0014 tree1.Write("", TObject.kOverwrite)
0015
0016 def sync_mbd_intt_DST(outputfname, f_mbd_name, t_mbd_name, f_intt_name, t_intt_name, Nevent):
0017
0018
0019 f_mbd = TFile.Open(f_mbd_name)
0020 t_mbd = f_mbd.Get(t_mbd_name)
0021 t_mbd_Nevt = t_mbd.GetEntries()
0022 print("-->", t_mbd, " Entry:", t_mbd_Nevt)
0023
0024
0025 f_intt = TFile.Open(f_intt_name)
0026 t_intt = f_intt.Get(t_intt_name)
0027 t_intt_Nevt = t_intt.GetEntries()
0028 print("-->", t_intt, " Entry:", t_intt_Nevt)
0029
0030
0031 outfile = TFile.Open(outputfname, 'recreate')
0032
0033
0034 t_intt_clone = t_intt.CloneTree(0)
0035 t_mbd_clone = t_mbd.CloneTree(0)
0036
0037
0038 if (Nevent > 0 and Nevent < t_mbd_Nevt):
0039 run_event = Nevent
0040 else:
0041 print("--> the requested event number is out of range, or not specified, use the full range of events in MBD tree.")
0042 run_event = t_mbd_Nevt
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 intt_mbd_bco_presync = TH2F("intt_mbd_bco_presync", "INTT - MBD", 100, 0, run_event * 1.5, 100, -10, 100000)
0054 intt_mbd_bco_presync.GetXaxis().SetTitle("evt")
0055 intt_mbd_bco_presync.GetYaxis().SetTitle("clock_diff")
0056
0057
0058 intt_mbd_bco_presync_1D = TH1F("intt_mbd_bco_presync_1D", "INTT - MBD", 100, -10, 100000)
0059 intt_mbd_bco_presync_1D.GetXaxis().SetTitle("clock_diff")
0060 intt_mbd_bco_presync_1D.GetYaxis().SetTitle("entry")
0061
0062 intt_mbd_bco_postsync = TH2F("intt_mbd_bco_postsync", "INTT - MBD", 100, 0, run_event * 1.5, 100, -10, 100000)
0063 intt_mbd_bco_postsync.GetXaxis().SetTitle("evt")
0064 intt_mbd_bco_postsync.GetYaxis().SetTitle("clock_diff")
0065
0066 intt_mbd_bco_postsync_1D = TH1F("intt_mbd_bco_postsync_1D", "INTT - MBD", 100, -10, 100000)
0067 intt_mbd_bco_postsync_1D.GetXaxis().SetTitle("clock_diff")
0068 intt_mbd_bco_postsync_1D.GetYaxis().SetTitle("entry")
0069
0070
0071 prev_mbdclk = 0
0072 prev_inttbcofull = 0
0073 import sys
0074
0075 mbd_evt_offset = 0
0076 intt_evt_offset = 0
0077
0078 print("--> start to synchronize the events")
0079 for entry in range(run_event):
0080 t_mbd.GetEntry(entry + mbd_evt_offset)
0081 t_intt.GetEntry(entry + intt_evt_offset)
0082
0083 mbdclk = t_mbd.femclk
0084 inttbcofull = t_intt.INTT_BCO
0085 inttbco16bit = inttbcofull & 0xFFFF
0086 mbd_prvdiff = (mbdclk - prev_mbdclk) & 0xFFFF
0087 intt_prvdiff = inttbcofull - prev_inttbcofull
0088
0089 if (entry % 1000) == 0:
0090 print(entry, hex(mbdclk), hex(inttbco16bit), "(mbd-intt)", hex((mbdclk - inttbco16bit) & 0xFFFF), "(MBD, femclk-prev)", hex(mbd_prvdiff), "(INTT, bco-prev)", hex(intt_prvdiff))
0091
0092 intt_mbd_bco_presync.Fill(entry, (mbdclk - inttbco16bit) & 0xFFFF)
0093 intt_mbd_bco_presync_1D.Fill((mbdclk - inttbco16bit) & 0xFFFF)
0094
0095 prev_mbdclk = mbdclk
0096 prev_inttbcofull = inttbcofull
0097
0098 is_sync = False
0099
0100 while entry + 1 + intt_evt_offset < t_intt_Nevt:
0101 t_intt.GetEntry(entry + 1 + intt_evt_offset)
0102 next_inttbco16bit = t_intt.INTT_BCO & 0xFFFF
0103
0104 t_mbd.GetEntry(entry + 1)
0105 next_mbdclk = t_mbd.femclk
0106
0107 if ((next_mbdclk - next_inttbco16bit) & 0xFFFF) != ((mbdclk - inttbco16bit) & 0xFFFF):
0108 intt_evt_offset += 1
0109 else:
0110 is_sync = True
0111 break
0112
0113 if is_sync:
0114 intt_mbd_bco_postsync.Fill(entry, (mbdclk - inttbco16bit) & 0xFFFF)
0115 intt_mbd_bco_postsync_1D.Fill((mbdclk - inttbco16bit) & 0xFFFF)
0116 t_intt_clone.Fill()
0117 t_mbd_clone.Fill()
0118
0119
0120 print("--> INTT Nevent post-sync: ",t_intt_clone.GetEntries())
0121 print("--> MBD Nevent post-sync: ",t_mbd_clone.GetEntries())
0122
0123 merge_trees(outfile, t_intt_clone, t_mbd_clone)
0124
0125 intt_mbd_bco_presync.Write("", TObject.kOverwrite)
0126 intt_mbd_bco_presync_1D.Write("", TObject.kOverwrite)
0127 intt_mbd_bco_postsync.Write("", TObject.kOverwrite)
0128 intt_mbd_bco_postsync_1D.Write("", TObject.kOverwrite)
0129 f_intt.Close()
0130 f_mbd.Close()
0131 outfile.Close()
0132
0133 print("--> Synchronization done")
0134 print("--> Merged file:", outputfname, ", Tree name: ", t_intt_name)
0135 print("--> TH2F plot: intt_mbd_bco_presync, can be checked in the merged file")
0136 print("--> TH1F plot: intt_mbd_bco_presync_1D, can be checked in the merged file")
0137
0138
0139
0140
0141 if __name__ == '__main__' :
0142 import sys
0143 args = sys.argv[1:]
0144
0145 Nevent = -200
0146
0147 if len(args) < 5 or len(args) > 6:
0148 print("--> No input or wrong arguments!")
0149 print("--> Usage: python3 python3_merge_test1.py <outfile_full_directory> <intt_file_full_directory> <intt_tree_name> <mbd_file_in_full_directory> <mbd_tree_name> <N event (optional)>")
0150 print("--> The file tree name will be intt tree name")
0151 sys.exit(1)
0152
0153 outputfname = args[0]
0154 print("--> Outputfname:", outputfname)
0155
0156 f_intt = args[1]
0157 t_intt = args[2]
0158 print("--> Direcotry of INTT file: ", f_intt)
0159 print("--> INTT tree name: ", t_intt)
0160
0161 f_mbd = args[3]
0162 t_mbd = args[4]
0163 print("--> Direcotry of MBD file: ", f_mbd)
0164 print("--> MBD tree name: ", t_mbd)
0165
0166 if len(args) == 6:
0167 Nevent = int(args[5])
0168
0169 gBenchmark.Start('InttMbdEvtCombiner')
0170
0171 sync_mbd_intt_DST(outputfname, f_mbd, t_mbd, f_intt, t_intt, Nevent)
0172
0173 gBenchmark.Show('InttMbdEvtCombiner')
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184