Skip to content

Commit 8c34c6d

Browse files
authored
Merge pull request #362 from GallegoSav/issue_361
Adding the Compton Sequence variable during tra file reading
2 parents dc46b9b + 0c7dde0 commit 8c34c6d

5 files changed

Lines changed: 80 additions & 9 deletions

File tree

cosipy/data_io/ReadTraTest.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def read_tra_old(self, output_name, make_plots=True):
5454
'Psi local':psi_loc,\
5555
'Distance':dist,\
5656
'Chi galactic':chi_gal,\
57-
'Psi galactic':psi_gal}
57+
'Psi galactic':psi_gal,\
58+
'Compton Seq':CO_seq}
5859
5960
Note
6061
----
@@ -103,7 +104,10 @@ def read_tra_old(self, output_name, make_plots=True):
103104
chi_gal = []
104105
# Measured gal angle psi (lat direction)
105106
psi_gal = []
107+
# Compton seq
108+
CO_seq = []
106109

110+
107111
# Browse through tra file, select events, and sort into corresponding list:
108112
# Note: The Reader class from MEGAlib knows where an event starts and ends and
109113
# returns the Event object which includes all information of an event.
@@ -143,12 +147,17 @@ def read_tra_old(self, output_name, make_plots=True):
143147
chi_gal.append((Event.GetGalacticPointingRotationMatrix()*Event.Dg()).Phi())
144148
# Gal longitude angle corresponding to chi:
145149
psi_gal.append((Event.GetGalacticPointingRotationMatrix()*Event.Dg()).Theta())
146-
150+
# Compton sequence (nb of interaction)
151+
CO_seq.append(Event.SequenceLength())
152+
153+
147154
# Initialize arrays:
148155
erg = np.array(erg)
149156
tt = np.array(tt)
150157
et = np.array(et)
151158

159+
CO_seq = np.array(CO_seq)
160+
152161
latX = np.array(latX)
153162
lonX = np.array(lonX)
154163
# Change longitudes to from 0..360 deg to -180..180 deg
@@ -201,7 +210,8 @@ def read_tra_old(self, output_name, make_plots=True):
201210
'Psi local':self.psi_loc_old,
202211
'Distance':dist,
203212
'Chi galactic':self.chi_gal_old,
204-
'Psi galactic':self.psi_gal_old}
213+
'Psi galactic':self.psi_gal_old,
214+
'Compton Seq':CO_seq}
205215
self.cosi_dataset = cosi_dataset
206216

207217
# Write unbinned data to file (either fits or hdf5):

cosipy/data_io/UnBinnedData.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
7878
'Psi local':psi_loc,\
7979
'Distance':dist,\
8080
'Chi galactic':chi_gal,\
81-
'Psi galactic':psi_gal}\
81+
'Psi galactic':psi_gal,\
82+
'CO seq':CO_seq}\
8283
Arrays contain unbinned photon data.
8384
8485
Notes
@@ -126,6 +127,10 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
126127
dg_y = []
127128
dg_z = []
128129

130+
# Compton seq
131+
CO_seq = []
132+
133+
129134
# Define electron rest energy, which is used in calculation
130135
# of Compton scattering angle.
131136
c_E0 = 510.9989500015 # keV
@@ -210,6 +215,7 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
210215
erg.pop()
211216
phi.pop()
212217
tt.pop()
218+
CO_seq.pop()
213219
# Not all sims include ori info,
214220
# so also need to check before pop:
215221
if len(lonX) != 0:
@@ -280,6 +286,11 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
280286
lonZ.append(this_lonZ)
281287
latZ.append(this_latZ)
282288

289+
290+
#number of interaction
291+
if this_line[0] == "SQ":
292+
CO_seq.append(int(this_line[1]))
293+
283294
# Interaction position information:
284295
if (this_line[0] == "CH"):
285296

@@ -323,7 +334,8 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
323334
dg_x = np.array(dg_x)
324335
dg_y = np.array(dg_y)
325336
dg_z = np.array(dg_z)
326-
337+
CO_seq = np.array(CO_seq)
338+
327339
# Check if the input data has pointing information,
328340
# if not, set dummy values:
329341
if (use_ori == False) & (len(lonZ)==0):
@@ -410,7 +422,8 @@ def read_tra(self, input_name=None, output_name=None, run_test=False, use_ori=Fa
410422
'Psi local':psi_loc,
411423
'Distance':dist,
412424
'Chi galactic':chi_gal,
413-
'Psi galactic':psi_gal}
425+
'Psi galactic':psi_gal,
426+
'Compton Seq':CO_seq}
414427
self.cosi_dataset = cosi_dataset
415428

416429
# Option to write unbinned data to file (either fits or hdf5):
@@ -550,7 +563,7 @@ def write_unbinned_output(self, output_name):
550563

551564
# Data units:
552565
units=['keV','s','rad','rad',
553-
'rad','rad','rad','rad','cm','deg','deg']
566+
'rad','rad','rad','rad','cm','deg','deg','']
554567

555568
# For fits output:
556569
if self.unbinned_output == 'fits':
@@ -723,6 +736,47 @@ def select_data_energy(self, emin, emax, output_name=None, unbinned_data=None):
723736

724737
return
725738

739+
def select_data_COseq(self, seqmin, seqmax, output_name=None, unbinned_data=None):
740+
741+
"""Applies CO sequence cuts [seqmin,seqmax) to unbinnned data dictionary.
742+
743+
Parameters
744+
----------
745+
seqmin : int
746+
Minimum number of interaction.
747+
seqmax : int
748+
Maximum number of interaction.
749+
unbinned_data : str, optional
750+
Name of unbinned dictionary file.
751+
output_name : str, optional
752+
Prefix of output file (default is None, in which case no
753+
file is saved).
754+
"""
755+
756+
logger.info("Making data selections on Compton Sequence...")
757+
758+
# Option to read in unbinned data file:
759+
if unbinned_data:
760+
self.cosi_dataset = self.get_dict(unbinned_data)
761+
762+
# Get energy cut index:
763+
COseq_array = self.cosi_dataset["Compton Seq"]
764+
COseq_cut_index = (COseq_array >= seqmin) & (COseq_array < seqmax)
765+
766+
# Apply cuts to dictionary:
767+
for key in self.cosi_dataset:
768+
769+
self.cosi_dataset[key] = self.cosi_dataset[key][COseq_cut_index]
770+
771+
# Write unbinned data to file (either fits or hdf5):
772+
if output_name != None:
773+
logger.info("Saving file...")
774+
self.write_unbinned_output(output_name)
775+
776+
return
777+
778+
779+
726780
def combine_unbinned_data(self, input_files, output_name=None):
727781

728782
"""Combines input unbinned data files.
-179 KB
Binary file not shown.

tests/dataIO/test_unbinned_data_all.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ def test_unbinned_data_all(tmp_path):
9494
assert np.amax(analysis.cosi_dataset['Energies']) < 300
9595
assert np.amin(analysis.cosi_dataset['Energies']) >= 200
9696

97+
# Test Compton sequence selection method
98+
analysis.select_data_COseq(3,4,unbinned_data=tmp_path/"test_h5.hdf5")
99+
assert np.amax(analysis.cosi_dataset['Compton Seq']) < 4
100+
assert np.amin(analysis.cosi_dataset['Compton Seq']) >= 3
101+
97102
# Test reading tra with no pointing info:
98103
analysis.data_file = os.path.join(test_data.path,\
99104
"GalacticScan.inc1.id1.crab10sec.extracted.testsample.nopointinginfo.tra.gz")

tests/dataIO/test_unbinned_data_with_MEGAlib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def test_unbinned_data_with_MEGAlib(tmp_path):
5151
psi_loc_old = dict_old['Psi local'][:ntestsamples]
5252
chi_gal_old = dict_old['Chi galactic'][:ntestsamples]
5353
psi_gal_old = dict_old['Psi galactic'][:ntestsamples]
54-
54+
CO_seq_old = dict_old['Compton Seq'][:ntestsamples]
55+
5556
# For comparing chi_loc, psi_loc=0 values are arbitrary,
5657
# so we exclude them from the comparison.
5758
psi_zero_index = psi_loc_old == 0
@@ -78,12 +79,13 @@ def test_unbinned_data_with_MEGAlib(tmp_path):
7879
psi_loc_dict = {"old":psi_loc_old,"new":analysis.psi_loc_test,"name":"psi_loc","units":"rad"}
7980
chi_gal_dict = {"old":chi_gal_old[~chi_gal_bad_index],"new":analysis.chi_gal_test[~chi_gal_bad_index],"name":"chi_gal","units":"rad"}
8081
psi_gal_dict = {"old":psi_gal_old,"new":analysis.psi_gal_test,"name":"psi_gal","units":"rad"}
82+
CO_seq_dict = {"old":CO_seq_old,"new":analysis.cosi_dataset["Compton Seq"],"name":"Compton Seq","units":""}
8183

8284
# Make comparison:
8385
print("Comparing to MEGAlib calculation:")
8486
test_list = [energies_dict,time_dict,phi_dict,\
8587
dist_dict,lonX_dict,latX_dict,lonZ_dict,latZ_dict,lonY_dict,latY_dict,\
86-
chi_loc_dict,psi_loc_dict,chi_gal_dict,psi_gal_dict]
88+
chi_loc_dict,psi_loc_dict,chi_gal_dict,psi_gal_dict,CO_seq_dict]
8789
for each in test_list:
8890
diff = compare(each["old"],each["new"],each["name"],make_plots=False)
8991
thresh = 1e-10

0 commit comments

Comments
 (0)