Package: cafplot
cafplotLibrary to plotCAFAnaobjects in python/matplotlib.cafplotaims to provide a set of function to allow easy plotting of CAFAna
objects (Spectrum, Surface, etc.) with matplotlib library.Whycafplot?So, why usecafplotfor plotting instead of directly using CAFAna/ROOT
functions? Here are several reasons why you might be interested incafplotEase of Development. Dynamic nature of the python typing system,
versatile and mature standard library and rich python package ecosystem will
make coding in python a light breeze compared to c++. Expect your python
script be2-3 times shorterthan the comparable c++ version.Run Anywhere.cafplotrelies only on a few standard python packages
for plotting. This allows you to runcafplotscripts on almost machine
with minimal setup. Which is in stark contrast to CAFAna scripts, where
you need to setup/compile NOvA-Art framework first.Speed. Yes, plotting with python/matplotlib is muchfasterthan using
CAFAna/ROOT. By the time it takes to compile your plotting CAFAna macro
an analogous python script would have finished running.Transferable Knowledge. Unlike knowledge of the ROOT framework which is
academia specific, knowledge of python/matplotlib stack is in demand in both
academia and industry.InstallationYou can installcafploteither usingpipor manually.Using PyPISimply run:$pipinstallcafplotManual InstallationClose the repository$gitclonehttps://github.com/usert5432/cafplotInstallcafplot$cdcafplot$pythonsetup.pyinstallSupport for Reading ROOT FilesIf you need support for reading ROOT files thenuprootpackage is required
as well:$pipinstalluprootGetting StartedLets look at a simple example of plotting CAFAna Spectrum.cafplotitself
does not support creation of CAFAna Spectra from the caf files and it is meant
only for plotting. Correspondingly, the Spectrum must be filled and created
in CAFAna first.For the purposes of this example, we will use CAFAna demo scriptCAFAna/tute/demo2p5a.Cto create a spectrum. After running this demo
script we will get a ROOT file namedsave_your_spectra_to_disk.rootwith a
1D Spectrumdir_nhit_spectra. Now, we will plot it usingcafplot.
First, we need to import a couple of packages:importmatplotlib.pyplotaspltfromcafplotimportloadfromcafplot.plotimport*Once imports are done, let’s load the CAFAna Spectrum from a filesave_your_spectra_to_disk.root(this will requireuprootpackage
installed):root_file=load("save_your_spectra_to_disk.root")spectrum=root_file.get_spectrum("dir_nhit_spectra")After this operation we will have CAFAna Spectrum loaded tospectrumvariable. Similar to CAFAna, before plotting Spectrum we need to extract a
normalized histogram from it. Let’s normalize it by POT to9e20.root_hist=spectrum.rhist(pot=9e20)Great, now we are ready to plot our first spectrum withcafplotf,ax=plt.subplots()# Create matplotlib Figure/Axesplot_rhist1d(ax,root_hist,'NHit',histtype="step",color="red")plt.show()# show plotYou should see the plotted spectrum opened in a separate window.DocumentationBasic example of how to usecafplotis outlined in theGetting Startedsection. More complete examples ofcafplotusage are provided in theexamples/subdirectory. In particular, you might be interested inspectrum_plot.py– complete example on how to plot CAFAna Spectrum
andsurf_plot.py– example of plotting CAFAna FrequentistSurface.The further documentation please refer to python docstrings for eachcafplotmodule.Dependenciescafplotis written with python version 3 in mind and won’t work with python
version 2. It requires the following packages for the proper operation:numpymatplotlibscipy- at this momentcafplotuses only stats subpackage
ofscipy.Additionally, one may want to installuprootif support of reading
CAFAna objects from ROOT files is required.Code Overviewcafplothas several subpackages with different purposes:plotsubpackage contains a collection of functions for plottingRHist,Spectrum,Surfaceobjects.rfilesubpackage contains functions and classes for loading CAFAna
objects from different files (currently supports ROOT files and json files).rhistsubpackage containsRHistclass that approximates behavior
of the ROOT histogram classes.spectrumsubpackage definesSpectrumclass to work with CAFAna
Spectrum.statssubpackage for various statistical routines.surfacesubpackage containsSurfaceclasses to work with CAFAna
surfaces.TODOAdd support for plotting binned statisticsAdd support for plotting ROC curves