CERN ROOT例子解析——学习向(candledecay.C)兼直方图绘图选项说明

本文详细介绍了CERNROOT中的一个示例代码,用于学习和理解如何使用TH2I对象创建二维直方图,并通过不同的绘图选项如violiny和candley2展示直方图。代码展示了如何填充随机数据,设置直方图属性以及使用不同的绘图风格在画板上同时显示两个直方图。此外,文章还列举了THistPainter类提供的各种绘图选项,包括1D和2D直方图的绘图风格,帮助用户更好地理解和应用这些选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要是为了让自己更好学习CERN ROOT所写,在ROOT的安装路径文件夹中,有一个tutorials的文件夹,里面有许多供用户学习的例子,为了让自己学完不会忘,故而在此对这些例子加以注释,方便日后使用ROOT处理数据时能方便查找使用案例。

void candledecay()
{
   auto c1 = new TCanvas("c1","Candle Decay",800,600); //新建一个画板
   c1->Divide(2,1); //画板一分为二,左右各一
   auto rng = new TRandom();  //新建一个随机变量
   auto h1 = new TH2I("h1","Decay",1000,0,1000,20,0,20);  //新建一个二维直方图对象
	
	//生成1000000个服从高斯分布的随机数,填充到h1对象中
   float myRand;
   for (int i = 0; i < 19; i++) {
      for (int j = 0; j < 1000000; j++) {
         myRand = rng->Gaus(350+i*8,20+2*i);
         h1->Fill(myRand,i);
      }
   }
   //设置h1的画图参数
   h1->SetBarWidth(3); //现款
   h1->SetFillStyle(0);  //填充类型
   h1->SetFillColor(kGray);  //填充颜色
   h1->SetLineColor(kBlue);  //线条颜色
   h1->GetYaxis()->SetTitle("time");  //Y轴标题
   h1->GetXaxis()->SetTitle("probability density");  //X轴标题

   c1->cd(1);  //h1画左边
   h1->Draw("violiny(112000000)");
   //复制h1出来在右边画h2
   c1->cd(2);  
   auto h2 = (TH2I*)h1->Clone("h2");
   h2->SetBarWidth(0.8);
   h2->DrawCopy("candley2");
}

在这里插入图片描述
在上述代码中,

h1->Draw("violiny(112000000)");
h2->DrawCopy("candley2");

Draw或DrawCopy函数中的参数指绘图选择,由THistPainter类提供该直方图的选项进行绘制,不同维度的直方图绘图选项众多,可通过https://root.cern.ch/doc/master/classTHistPainter.html进行查询。
其中,主要选项及其绘图描述如下:
1D和2D直方图支持的绘图选项:

“E” Draw error bars.
“AXIS” Draw only axis.
“AXIG” Draw only grid (if the grid is requested).
“HIST” When an histogram has errors it is visualized by default with error bars. To visualize it without errors use the option “HIST” together with the required option (eg “hist same c”). The “HIST” option can also be used to plot only the histogram and not the associated function(s).
“FUNC” When an histogram has a fitted function, this option allows to draw the fit result only.
“SAME” Superimpose on previous picture in the same pad.
“SAMES” Same as “SAME” and draw the statistics box
“PFC” Palette Fill Color: histogram’s fill color is taken in the current palette.
“PLC” Palette Line Color: histogram’s line color is taken in the current palette.
“PMC” Palette Marker Color: histogram’s marker color is taken in the current palette.
“LEGO” Draw a lego plot with hidden line removal.
“LEGO1” Draw a lego plot with hidden surface removal.
“LEGO2” Draw a lego plot using colors to show the cell contents When the option “0” is used with any LEGO option, the empty bins are not drawn.
“LEGO3” Draw a lego plot with hidden surface removal, like LEGO1 but the border lines of each lego-bar are not drawn.
“LEGO4” Draw a lego plot with hidden surface removal, like LEGO1 but without the shadow effect on each lego-bar.
“TEXT” Draw bin contents as text (format set via gStyle->SetPaintTextFormat).
“TEXTnn” Draw bin contents as text at angle nn (0 < nn <= 90).
“X+” The X-axis is drawn on the top side of the plot.
“Y+” The Y-axis is drawn on the right side of the plot.
“MIN0” Set minimum value for the Y axis to 0, equivalent to gStyle->SetHistMinimumZero().

1D直方图支持的选项:
" " Default.
“AH” Draw histogram without axis. “A” can be combined with any drawing option. For instance, “AC” draws the histogram as a smooth Curve without axis.
“][” When this option is selected the first and last vertical lines of the histogram are not drawn.
“B” Bar chart option.
“BAR” Like option “B”, but bars can be drawn with a 3D effect.
“HBAR” Like option “BAR”, but bars are drawn horizontally.
“C” Draw a smooth Curve through the histogram bins.
“E0” Draw error bars. Markers are drawn for bins with 0 contents. Combined with E1 or E2 it avoids error bars clipping
“E1” Draw error bars with perpendicular lines at the edges.
“E2” Draw error bars with rectangles.
“E3” Draw a fill area through the end points of the vertical error bars.
“E4” Draw a smoothed filled area through the end points of the error bars.
“E5” Like E3 but ignore the bins with 0 contents.
“E6” Like E4 but ignore the bins with 0 contents.
“X0” When used with one of the “E” option, it suppress the error bar along X as gStyle->SetErrorX(0) would do.
“L” Draw a line through the bin contents.
“P” Draw current marker at each bin except empty bins.
"P
" Draw a star marker at each bin except empty bins.
“P0” Draw current marker at each bin including empty bins.
“PIE” Draw histogram as a Pie Chart.
"H" Draw histogram with a * at each bin.
“LF2” Draw histogram like with option “L” but with a fill area. Note that “L” draws also a fill area if the hist fill color is set but the fill area corresponds to the histogram contour.

2D直方图支持的选项:
" " Default (scatter plot).
“ARR” Arrow mode. Shows gradient between adjacent cells.
“BOX” A box is drawn for each cell with surface proportional to the content’s absolute value. A negative content is marked with a X.
“BOX1” A button is drawn for each cell with surface proportional to content’s absolute value. A sunken button is drawn for negative values a raised one for positive.
“COL” A box is drawn for each cell with a color scale varying with contents. All the none empty bins are painted. Empty bins are not painted unless some bins have a negative content because in that case the null bins might be not empty. TProfile2D histograms are handled differently because, for this type of 2D histograms, it is possible to know if an empty bin has been filled or not. So even if all the bins’ contents are positive some empty bins might be painted. And vice versa, if some bins have a negative content some empty bins might be not painted.
“COLZ” Same as “COL”. In addition the color palette is also drawn.
“COL2” Alternative rendering algorithm to “COL”. Can significantly improve rendering performance for large, non-sparse 2-D histograms.
“COLZ2” Same as “COL2”. In addition the color palette is also drawn.
“Z CJUST” In combination with colored options “COL”,“CONT0” etc: Justify labels in the color palette at color boudaries. For more details see TPaletteAxis
“CANDLE” Draw a candle plot along X axis.
“CANDLEX” Same as “CANDLE”.
“CANDLEY” Draw a candle plot along Y axis.
“CANDLEXn” Draw a candle plot along X axis. Different candle-styles with n from 1 to 6.
“CANDLEYn” Draw a candle plot along Y axis. Different candle-styles with n from 1 to 6.
“VIOLIN” Draw a violin plot along X axis.
“VIOLINX” Same as “VIOLIN”.
“VIOLINY” Draw a violin plot along Y axis.
“VIOLINXn” Draw a violin plot along X axis. Different violin-styles with n being 1 or 2.
“VIOLINYn” Draw a violin plot along Y axis. Different violin-styles with n being 1 or 2.
“CONT” Draw a contour plot (same as CONT0).
“CONT0” Draw a contour plot using surface colors to distinguish contours.
“CONT1” Draw a contour plot using line styles to distinguish contours.
“CONT2” Draw a contour plot using the same line style for all contours.
“CONT3” Draw a contour plot using fill area colors.
“CONT4” Draw a contour plot using surface colors (SURF option at theta = 0).
“LIST” Generate a list of TGraph objects for each contour.
“SAME0” Same as “SAME” but do not use the z-axis range of the first plot.
“SAMES0” Same as “SAMES” but do not use the z-axis range of the first plot.
“CYL” Use Cylindrical coordinates. The X coordinate is mapped on the angle and the Y coordinate on the cylinder length.
“POL” Use Polar coordinates. The X coordinate is mapped on the angle and the Y coordinate on the radius.
“SPH” Use Spherical coordinates. The X coordinate is mapped on the latitude and the Y coordinate on the longitude.
“PSR” Use PseudoRapidity/Phi coordinates. The X coordinate is mapped on Phi.
“SURF” Draw a surface plot with hidden line removal.
“SURF1” Draw a surface plot with hidden surface removal.
“SURF2” Draw a surface plot using colors to show the cell contents.
“SURF3” Same as SURF with in addition a contour view drawn on the top.
“SURF4” Draw a surface using Gouraud shading.
“SURF5” Same as SURF3 but only the colored contour is drawn. Used with option CYL, SPH or PSR it allows to draw colored contours on a sphere, a cylinder or a in pseudo rapidity space. In cartesian or polar coordinates, option SURF3 is used.
“AITOFF” Draw a contour via an AITOFF projection.
“MERCATOR” Draw a contour via an Mercator projection.
“SINUSOIDAL” Draw a contour via an Sinusoidal projection.
“PARABOLIC” Draw a contour via an Parabolic projection.
“LEGO9” Draw the 3D axis only. Mainly needed for internal use
“FB” With LEGO or SURFACE, suppress the Front-Box.
“BB” With LEGO or SURFACE, suppress the Back-Box.
“A” With LEGO or SURFACE, suppress the axis.
“SCAT” Draw a scatter-plot (default).
“[cutg]” Draw only the sub-range selected by the TCutG named “cutg”.

3D直方图支持的选项:
" " Default (scatter plot).
“ISO” Draw a Gouraud shaded 3d iso surface through a 3d histogram. It paints one surface at the value computed as follow: SumOfWeights/(NbinsX
NbinsYNbinsZ).
“BOX” Draw a for each cell with volume proportional to the content’s absolute value. An hidden line removal algorithm is used
“BOX1” Same as BOX but an hidden surface removal algorithm is used
“BOX2” The boxes’ colors are picked in the current palette according to the bins’ contents
“BOX2Z” Same as “BOX2”. In addition the color palette is also drawn.
“BOX3” Same as BOX1, but the border lines of each lego-bar are not drawn.
“LEGO” Same as BOX.

直方图堆栈支持的选项(Options supported for histograms’ stacks (THStack))
" " Default, the histograms are drawn on top of each other (as lego plots for 2D histograms).
“NOSTACK” Histograms in the stack are all paint in the same pad as if the option SAME had been specified.
“NOSTACKB” Histograms are drawn next to each other as bar charts.
“PADS” The current pad/canvas is subdivided into a number of pads equal to the number of histograms in the stack and each histogram is paint into a separate pad.
“PFC” Palette Fill Color: stack’s fill color is taken in the current palette.
“PLC” Palette Line Color: stack’s line color is taken in the current palette.
“PMC” Palette Marker Color: stack’s marker color is taken in the current palette.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值