Plot of diffraction patterns

When a powder sample has been measured at DMC it is saved in hdf files. Several DataFiles can be combined into a common DataSet and plotted. The follwing code takes a DatsSet, here consisting of several DataFiles, and plot the dataSet. Two different settings for the binning method is used correctedTwoTheta equal to True and False. When False a naive summation across the 2D detector is performed where the out-of-plane component is not taken into account. That is, summation is performed vertically on the detector. For powder patterns around 90o, this is only a very minor error, but for scattering close to the direct beam a significant error is introduced. Instead, utilizing correctedTwoTheta = True is the correct way. The scattering 3D vector is calculated for each individual pixel on the 2D detector and it’s length is calculated.

 1from DMCpy import DataSet,DataFile, _tools
 2import matplotlib.pyplot as plt
 3
 4# To plot powder data we give the file number, or list of filenumbers as a string and the folder of the raw data
 5scanNumbers = '1285-1290'
 6folder = 'data'
 7year = 2022
 8
 9# a list of dataFiles are generated with loadDataFile running over all the dataFiles generated from _tools.fileListGenerator and twoThetaOffset acts on the dataFile
10dataFiles = [DataFile.loadDataFile(dFP) for dFP in _tools.fileListGenerator(scanNumbers,folder,year=year)]
11
12# We then create a data set based on the data files
13ds = DataSet.DataSet(dataFiles)
14
15for df in ds:
16   df.generateMask(lambda x: DataFile.maskFunction(x,maxAngle=180.0),replace=True)
17ds._getData()
18
19# We can also give the step size for the integration. Default is 0.125
20dTheta = 0.125
21
22# Generate a diffraction pattern where the 2D detector is integrated in Q-space
23ax,bins,Int,Int_err,monitor = ds.plotTwoTheta(dTheta=dTheta)
24ax.set_title('Integrated in Q')
25fig = ax.get_figure()
26fig.savefig('figure0.png',format='png'),r'docs/Tutorials/Powder/TwoThetaPowderQ.png'),format='png',dpi=300)
27
28# Generate a diffraction pattern where the 2D detector is integrated vertically
29# Note that when correctedTwoTheta, the x-axis is negaive and must be inverted
30ax2,bins2,Int2,Int_err2,monitor2 = ds.plotTwoTheta(correctedTwoTheta=False,dTheta=dTheta)
31ax2.set_title('Integrated vertically')
32fig2 = ax2.get_figure()
33fig2.savefig('figure1.png',format='png'),r'docs/Tutorials/Powder/TwoThetaPowderVertical.png'),format='png',dpi=300)
34
35# Generate a diffraction pattern where the 2D detector is integrated in Q-space with and 5 deg. angular mask
36for df in ds:
37   df.generateMask(lambda x: DataFile.maskFunction(x,maxAngle=5.0),replace=True)
38# for powder integration we need to use _getData() to apply the mask
39ds._getData()
40
41ax3,bins3,Int3,Int_err3,monitor3 = ds.plotTwoTheta(dTheta=dTheta)
42ax3.set_title('Integrated with 5 deg. angular mask')
43fig3 = ax3.get_figure()
44fig3.savefig('figure2.png',format='png'),r'docs/Tutorials/Powder/TwoThetaPowderMask.png'),format='png',dpi=300)
45
46# plot integration with and without mask together
47fig4,ax4 = plt.subplots()
48ax4.set_title('Comparison between no mask and integrated with 5 deg. angular mask')
49ax4.set_xlabel(r'$2\theta$ [deg]')
50ax4.set_ylabel(r'Intensity [arb]')
51# bins are the limits and we need to take the average to get corrct xbins
52plt.errorbar(0.5*(bins[1:]+bins[:-1]),Int,yerr=Int_err,label='No mask')
53plt.errorbar(0.5*(bins3[1:]+bins3[:-1]),Int3,yerr=Int_err3,label='5 deg. mask')
54plt.xlim(18,34)
55ax4.legend()
56fig4.savefig('figure3.png',format='png'),r'docs/Tutorials/Powder/TwoThetaPowderCombined.png'),format='png',dpi=300)
57
58# plot integration with Q integration and vertical integration
59fig5,ax5 = plt.subplots()
60ax5.set_title('Comparison between Q integration and vertical integration')
61ax5.set_xlabel(r'$2\theta$ [deg]')
62ax5.set_ylabel(r'Intensity [arb]')
63plt.errorbar(0.5*(bins[1:]+bins[:-1]),Int,yerr=Int_err,label='Q integration without mask')
64plt.errorbar(0.5*(bins3[1:]+bins3[:-1]),Int3,yerr=Int_err3,label='Q integration with 5 deg. mask')
65plt.errorbar(0.5*(bins2[1:]+bins2[:-1]),Int2,yerr=Int_err2,label='Vertical integration')
66plt.xlim(18,34)
67ax5.legend()
68fig5.savefig('figure4.png',format='png'),r'docs/Tutorials/Powder/TwoThetaPowderCombined2.png'),format='png',dpi=300)
Running the above code generates the following, similar looking, diffractograms utilizing the corrected and uncorrected twoTheta positions respectively. We highlight the reduced noice and the slightly sharper peaks in the corrected image.The down-up glitches of the diffraction pattern is from the interface between the detector sections. Integration with different masks and comparisons are also displayed.
../../_images/TwoThetaPowderQ.png
../../_images/TwoThetaPowderVertical.png
../../_images/TwoThetaPowderMask.png
../../_images/TwoThetaPowderCombined.png
../../_images/TwoThetaPowderCombined2.png