|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import astropy.units as u |
| 4 | +from astropy.coordinates import SkyCoord, Angle |
| 5 | +from astropy.table import Table |
| 6 | +from astropy.visualization import simple_norm |
| 7 | + |
| 8 | +from gammapy.data import DataStore |
| 9 | +from gammapy.image import SkyImage |
| 10 | +from gammapy.scripts import SingleObsImageMaker |
| 11 | +from gammapy.utils.energy import Energy |
| 12 | +import regions |
| 13 | + |
| 14 | +import scipy.stats as ss |
| 15 | +import copy |
| 16 | +#get the observations |
| 17 | +name="PKS 2155-304" |
| 18 | +#name="Mkn 421" |
| 19 | +#name="1ES 0347-121" |
| 20 | +datastore = DataStore.from_dir("$HESS_DATA") |
| 21 | +src=SkyCoord.from_name(name) |
| 22 | +sep=SkyCoord.separation(src,datastore.obs_table.pointing_radec) |
| 23 | +srcruns=(datastore.obs_table[sep<1.5*u.deg]) |
| 24 | +obsid=srcruns['OBS_ID'].data |
| 25 | +good_obs=np.loadtxt("PKS2155_PA_201801.list.txt",usecols=(0)) |
| 26 | +obsid1=np.intersect1d(good_obs,obsid) |
| 27 | +mylist=datastore.obs_list(obsid1) |
| 28 | +#N=len(mylist) |
| 29 | + |
| 30 | +ref_image = SkyImage.empty( |
| 31 | + nxpix=400, nypix=400, binsz=0.02, |
| 32 | + xref=src.ra.deg, yref=src.dec.deg, |
| 33 | + coordsys='CEL', proj='TAN', |
| 34 | +) |
| 35 | + |
| 36 | +exclusion_mask_tevcat = SkyImage.read('$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') |
| 37 | +exclusion_mask_tevcat = exclusion_mask_tevcat.reproject(reference=ref_image) |
| 38 | + |
| 39 | +energy_band = Energy([0.52480746, 1.58489319], 'TeV') |
| 40 | +energy_band = Energy([0.5, 1.5], 'TeV') |
| 41 | +of1=Angle([0.0, 0.50251891], 'deg') |
| 42 | +of2=Angle([0.50251891, 1.20499801], 'deg') |
| 43 | +of3=Angle([1.20499801, 2.01007563], 'deg') |
| 44 | +of4=Angle([2.01007563, 2.48734169], 'deg') |
| 45 | +#of1=Angle([0.0, 1.2], 'deg') |
| 46 | +#of2=Angle([0.50251891, 1.2], 'deg') |
| 47 | +offset_band = [of1,of2,of3,of4] |
| 48 | +backnorm=[] |
| 49 | +Ncounts=[] |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +list_zen=filter(lambda o1: o1.pointing_zen.value<34.3, mylist) |
| 54 | + |
| 55 | +time=[o1.observation_live_time_duration.value for o1 in list_zen] |
| 56 | +zen_ang=[o1.pointing_zen.value for o1 in list_zen] |
| 57 | +muonef=[o1.muoneff for o1 in list_zen] |
| 58 | + |
| 59 | + |
| 60 | +N=len(list_zen) |
| 61 | +print N |
| 62 | + |
| 63 | +for i in range(N): |
| 64 | + if i%10 ==0: |
| 65 | + print "----running observation -- ",i |
| 66 | + |
| 67 | + obs=list_zen[i] |
| 68 | + |
| 69 | + events = datastore.obs(obs.obs_id).events |
| 70 | + counts_image = SkyImage.empty_like(ref_image) |
| 71 | + counts_image.fill_events(events) |
| 72 | + |
| 73 | + exclusion_region = regions.CircleSkyRegion(src,0.3*u.deg) |
| 74 | + mask = counts_image.region_mask(exclusion_region) |
| 75 | + |
| 76 | + mask1=copy.copy(mask) |
| 77 | + mask1.data=np.invert(mask.data) |
| 78 | + norm_off=[] |
| 79 | + Nc=[] |
| 80 | + for j in range(len(offset_band)): |
| 81 | + |
| 82 | + image_maker = SingleObsImageMaker( |
| 83 | + obs=obs, |
| 84 | + empty_image=ref_image, |
| 85 | + energy_band=energy_band, |
| 86 | + offset_band=offset_band[j], |
| 87 | + exclusion_mask=mask1, |
| 88 | + ) |
| 89 | + |
| 90 | + image_maker.counts_image() |
| 91 | + counts_image = image_maker.images['counts'] |
| 92 | + |
| 93 | + |
| 94 | + image_maker.bkg_image() |
| 95 | + background_image = image_maker.images['bkg'] |
| 96 | + #norm = simple_norm(background_image.data, stretch='sqrt', min_cut=0, max_cut=0.2) |
| 97 | + #background_image.plot(norm=norm, add_cbar=True) |
| 98 | + #plt.show() |
| 99 | + |
| 100 | + norm_off.append(image_maker.table_bkg_scale['bkg_scale'].data[0]) |
| 101 | + Nc.append(image_maker.table_bkg_scale['N_counts'].data[0]) |
| 102 | + |
| 103 | + backnorm.append(norm_off) |
| 104 | + Ncounts.append(Nc) |
| 105 | + |
| 106 | + |
| 107 | +print "exited from loop...now plot..." |
| 108 | + |
| 109 | +#plt.hist(backnorm) |
| 110 | +#plt.title("Energy band: "+str(energy_band)) |
| 111 | +#plt.ylabel("frequency of occurance") |
| 112 | +#plt.xlabel("Backscale factor") |
| 113 | +#plt.show() |
| 114 | + |
| 115 | +#plt.plot(Ncounts,backnorm,"b.") |
| 116 | +#plt.xlabel("Background counts") |
| 117 | +#plt.show() |
| 118 | + |
| 119 | +#time=[o1.observation_live_time_duration.value for o1 in mylist] |
| 120 | +#plt.plot(time[:-1],backnorm,"r+") |
| 121 | +#plt.xlabel("Observation Duration") |
| 122 | +#plt.show() |
| 123 | + |
| 124 | +#zen_ang=[o1.pointing_zen.value for o1 in mylist] |
| 125 | +#plt.plot(zen_ang[:-1],backnorm,"r+") |
| 126 | +#plt.xlabel("Zenith angle") |
| 127 | +#plt.show() |
| 128 | + |
| 129 | +#muonef=[o1.muoneff for o1 in mylist] |
| 130 | +#plt.plot(muonef[:-1],backnorm,"r+") |
| 131 | +#plt.xlabel("Muon effectivity") |
| 132 | +#plt.show() |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +bkg_counts = np.divide(Ncounts,backnorm) |
| 137 | + |
| 138 | +#plt.plot(zen_ang,np.divide(Ncounts,time),"r.",label="Observed") |
| 139 | +#plt.plot(zen_ang,np.divide(bkg_counts,time),"b.",label="Model predicted") |
| 140 | +#plt.xlabel("Zenith angle") |
| 141 | +#plt.ylabel("Count rate [/s]") |
| 142 | +#plt.title("Energy band: "+str(energy_band)) |
| 143 | +#plt.legend() |
| 144 | +#plt.show() |
| 145 | + |
| 146 | +#plt.plot(muonef,np.divide(Ncounts,time),"r.",label="Observed") |
| 147 | +#plt.plot(muonef,np.divide(bkg_counts,time),"b.",label="Model predicted") |
| 148 | +#plt.xlabel("Muon effectivity") |
| 149 | +#plt.ylabel("Count rate [/s]") |
| 150 | +#plt.title("Energy band: "+str(energy_band)) |
| 151 | +#plt.legend() |
| 152 | +#plt.show() |
| 153 | + |
| 154 | +#dev = np.absolute(np.subtract(1.0,backnorm)) |
| 155 | + |
| 156 | + |
| 157 | +Nc0=[row[0] for row in Ncounts] |
| 158 | +Nc1=[row[1] for row in Ncounts] |
| 159 | +Nc2=[row[2] for row in Ncounts] |
| 160 | +Nc3=[row[3] for row in Ncounts] |
| 161 | + |
| 162 | +b0=[row[0] for row in bkg_counts] |
| 163 | +b1=[row[1] for row in bkg_counts] |
| 164 | +b2=[row[2] for row in bkg_counts] |
| 165 | +b3=[row[3] for row in bkg_counts] |
| 166 | + |
| 167 | +bn0=[row[0] for row in backnorm] |
| 168 | +bn1=[row[1] for row in backnorm] |
| 169 | +bn2=[row[2] for row in backnorm] |
| 170 | +bn3=[row[3] for row in backnorm] |
| 171 | + |
| 172 | + |
| 173 | +plt.hist(bn0,alpha=0.5,label=str(of1)) |
| 174 | +plt.hist(bn1,alpha=0.5,label=str(of2)) |
| 175 | +plt.hist(bn2,alpha=0.5,label=str(of3)) |
| 176 | +plt.hist(bn3,alpha=0.5,label=str(of4)) |
| 177 | +plt.legend() |
| 178 | +plt.show() |
| 179 | + |
| 180 | + |
| 181 | +zenc=10 |
| 182 | + |
| 183 | +bn0_zc=[] |
| 184 | +bn1_zc=[] |
| 185 | +bn2_zc=[] |
| 186 | + |
| 187 | +for b,o1 in zip(bn0,list_zen): |
| 188 | + if o1.pointing_zen.value<zenc: |
| 189 | + bn0_zc.append(b) |
| 190 | + |
| 191 | +for b,o1 in zip(bn1,list_zen): |
| 192 | + if o1.pointing_zen.value<zenc: |
| 193 | + bn1_zc.append(b) |
| 194 | + |
| 195 | + |
| 196 | +for b,o1 in zip(bn2,list_zen): |
| 197 | + if o1.pointing_zen.value<zenc: |
| 198 | + bn2_zc.append(b) |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +plt.hist(bn0_zc,alpha=0.5,label=str(of1)) |
| 203 | +plt.hist(bn1_zc,alpha=0.5,label=str(of2)) |
| 204 | +plt.hist(bn2_zc,alpha=0.5,label=str(of3)) |
| 205 | +plt.legend() |
| 206 | +plt.show() |
| 207 | + |
| 208 | + |
| 209 | +N0_zc=[] |
| 210 | +N1_zc=[] |
| 211 | +N2_zc=[] |
| 212 | + |
| 213 | + |
| 214 | +for b,o1 in zip(Nc0,list_zen): |
| 215 | + if o1.pointing_zen.value<zenc: |
| 216 | + N0_zc.append(b) |
| 217 | + |
| 218 | +for b,o1 in zip(Nc1,list_zen): |
| 219 | + if o1.pointing_zen.value<zenc: |
| 220 | + N1_zc.append(b) |
| 221 | + |
| 222 | + |
| 223 | +for b,o1 in zip(Nc2,list_zen): |
| 224 | + if o1.pointing_zen.value<zenc: |
| 225 | + N2_zc.append(b) |
| 226 | + |
| 227 | + |
| 228 | +b0_zc=[] |
| 229 | +b1_zc=[] |
| 230 | +b2_zc=[] |
| 231 | +b3_zc=[] |
| 232 | + |
| 233 | + |
| 234 | +for b,o1 in zip(b0,list_zen): |
| 235 | + if o1.pointing_zen.value<zenc: |
| 236 | + b0_zc.append(b) |
| 237 | + |
| 238 | +for b,o1 in zip(b1,list_zen): |
| 239 | + if o1.pointing_zen.value<zenc: |
| 240 | + b1_zc.append(b) |
| 241 | + |
| 242 | + |
| 243 | +for b,o1 in zip(b2,list_zen): |
| 244 | + if o1.pointing_zen.value<zenc: |
| 245 | + b2_zc.append(b) |
| 246 | + |
| 247 | + |
| 248 | +list_zen1=filter(lambda o1: o1.pointing_zen.value<zenc, mylist) |
| 249 | +mul=[o1.muoneff for o1 in list_zen1] |
| 250 | + |
| 251 | +list_zen1=filter(lambda o1: o1.pointing_zen.value<zenc, mylist) |
| 252 | +time1=[o1.observation_live_time_duration.value for o1 in list_zen1] |
| 253 | + |
| 254 | +zen=[o1.pointing_zen.value for o1 in list_zen1] |
| 255 | + |
| 256 | +plt.plot(zen_ang,np.divide(Nc0,time),"r.",label=of1*u.deg) |
| 257 | +#plt.plot(zen_ang,np.divide(b0,time),"r+") |
| 258 | +plt.plot(zen_ang,np.divide(Nc1,time),"g.",label=of2*u.deg) |
| 259 | +#plt.plot(zen_ang,np.divide(b1,time),"g+") |
| 260 | +plt.plot(zen_ang,np.divide(Nc2,time),"b.",label=(of3*u.deg)) |
| 261 | +#plt.plot(zen_ang,np.divide(b2,time),"b+") |
| 262 | +plt.plot(zen_ang,np.divide(Nc3,time),"c.",label=(of4*u.deg)) |
| 263 | +#plt.plot(zen_ang,np.divide(b3,time),"c+") |
| 264 | +plt.legend() |
| 265 | +plt.show() |
| 266 | + |
| 267 | + |
| 268 | +plt.plot(zen,np.divide(N0_zc,time1),"r.") |
| 269 | + |
| 270 | +mu1=0.773 |
| 271 | +mu2=0.774 |
| 272 | + |
| 273 | + |
| 274 | +list_mu1=filter(lambda o1: o1.muoneff<mu2, list_zen1) |
| 275 | +list_mu1=filter(lambda o1: o1.muoneff>mu1, list_mu1) |
| 276 | + |
| 277 | +Nm0=[] |
| 278 | + |
| 279 | +for b,o1 in zip(N0_zc,list_zen1): |
| 280 | + if (o1.muoneff>mu1 and o1.muoneff<mu2): |
| 281 | + Nm0.append(b) |
| 282 | + |
| 283 | +tm=[o1.observation_live_time_duration.value for o1 in list_mu1] |
| 284 | +zm=[o1.pointing_zen.value for o1 in list_mu1] |
| 285 | + |
| 286 | +plt.plot(zen,np.divide(N0_zc,time1),"r.",label=of1*u.deg) |
| 287 | +plt.plot(zm,np.divide(Nm0,tm),"b+") |
| 288 | +plt.show() |
| 289 | + |
0 commit comments