12下一页
返回列表 发布新帖
查看: 191|回复: 10

[其它] CERN ROOT入门基础

16

帖子

38

积分

10

金币

新手上路

积分
38
发表于 2025-8-7 21:55:39 | 查看全部 |阅读模式
本帖最后由 pengdayu 于 2025-8-7 22:02 编辑

以下将为C/C++零基础人群系统介绍CERN ROOT框架及学习路径的指南,包括ROOT的核心特性和初学者友好的编程工具箱:

一、ROOT框架概述​定位​​:由CERN开发的开源数据分析框架,专为高能物理PB级数据处理设计,现扩展至通用科学计算领域。
​核心模块​​:
  • ​数据处理​
    • ​TTree/TChain​​:树形结构存储事件数据,支持高效查询。TChain可跨多个文件合并数据。
    • ​特有文件格式(.root)​​:二进制压缩格式,比CSV/JSON节省90%存储空间(EB级数据处理能力)。
  • ​统计与拟合​
    • ​TH1F/TH2F​​:一维/二维直方图类,支持填充、统计、拟合(如高斯分布)。
    • ​Fit模块​​:内置最小二乘法、最大似然估计等算法,支持自定义拟合函数。
  • ​可视化​
    • ​TCanvas​​:画布容器,管理绘图区域。
    • ​TGraph/THist​​:绘制散点图、直方图,支持自定义坐标轴/颜色/标记。
  • ​并行计算​
    • ​PROOF​​:分布式计算框架,支持集群任务拆分。
    • ​TThreadedTree​​:多线程读取数据。
​跨语言支持​​:
  • ​PyROOT​​:ROOT的Python绑定,可直接调用C++类(如ROOT.TH1F())。
  • ​uproot​​:纯Python库,无需C++环境即可读取.root文件(适合轻量级分析)。

二、Cling:ROOT的交互式C++解释器​核心作用​​:无需编译即可实时执行C++代码,适合快速验证代码逻辑和调试。
​技术原理​​:基于Clang/LLVM构建,支持现代C++标准(C++11及以上)。
操作示例(Ubuntu终端,安装参考安装ROOT - 粒子与天体(Elementary Particle and Celestial Body ) - 物质结构社区):
# 启动Cling
在bash下运行:root -l
# 实时执行代码
root [0] #include <iostream>
root [1] std::cout << "Hello, ROOT!" << std::endl;  
// 输出:
Hello, ROOT!
root [2] .q  // 退出​
优势​​:
  • ​教学友好​​:即时反馈帮助理解语法(如循环、函数)。
  • ​调试便捷​​:动态修改变量值,快速定位逻辑错误。

三、ROOT文件结构:高效存储科学数据1. ​​核心组件​
  • ​TFile​​:管理.root文件的读写入口,类似C++的文件流对象。
  • ​TTree​​:列式存储结构,优化海量事件数据的读取(如粒子碰撞事件)。
    • 每个分支(TBranch)存储独立变量,支持快速查询。
  • ​TH1F/TH2F​​:一维/二维直方图对象,存储统计分布数据。
2. ​​文件读写实战​
// 写入TTree数据到文件
#include <TFile.h>
#include <TTree.h>
#include <TRandom.h>
void write_data() {
    TFile f("data.root", "RECREATE");       // 创建文件
    TTree tree("tree", "Example Data");    // 创建TTree
    Float_t x, y;    tree.Branch("x", &x, "x/F");           // 定义浮点型分支x
    tree.Branch("y", &y, "y/F");           // 定义浮点型分支y
    TRandom rand;    for (int i = 0; i < 10000; i++) {
        x = rand.Gaus(0, 1);               // 生成高斯分布随机数
        y = rand.Uniform(-1, 1);
        tree.Fill();                       // 填充数据
    }
    f.Write();                             // 写入磁盘
}​
关键点​​:
  • Branch() 绑定变量与数据分支,Fill() 将内存数据写入缓冲区。
  • ROOT二进制格式比CSV节省90%存储空间。
3. ​​读取数据示例​
// 从文件读取直方图
void read_hist() {
    TFile f("data.root", "READ");
    TH1F *hist = (TH1F*)f.Get("my_hist");  // 获取直方图对象
    Int_t bins = hist->GetNbinsX();        // 获取直方图区间数
    for (int i = 1; i <= bins; i++) {
        Double_t value = hist->GetBinContent(i);  // 读取第i个区间统计值
    }
    hist->Draw();                          // 绘制直方图
}
四、绘图基础:TCanvas与直方图1. ​​核心类​
  • ​TCanvas​​:画布容器,管理绘图区域(尺寸可定制)。
  • ​TH1F​​:一维直方图,支持填充、统计、拟合。
2. ​​绘图流程​
#include <TCanvas.h>
#include <TH1F.h>
#include <TApplication.h>  // 必需!用于图形界面初始化
int main() {
    TApplication app("app", 0, 0);         // 初始化图形应用(不一定需要,主要针对窗口闪退时启用)
    TCanvas *c = new TCanvas("c", "Canvas", 800, 600);
    TH1F *hist = new TH1F("h", "Gaussian Distribution", 100, -5, 5);    // 填充高斯分布数据
    for (int i = 0; i < 10000; i++) {
        hist->Fill(gRandom->Gaus(0, 1));  // gRandom为ROOT全局随机数生成器
    }   
// 定制化绘图   
    hist->GetXaxis()->SetTitle("X Value");  // 设置X轴标题
    hist->SetFillColor(kBlue);              // 设置填充色(kBlue为ROOT**义颜色)
    hist->Draw("HIST");                     // "HIST"表示绘制为柱状图
    c->SaveAs("plot.png");                  // 保存为图片
    app.Run();                              // 运行应用
    return 0;
}​
避坑指南​​:
  • 缺失 TApplication 或 app.Run() 会导致图形窗口闪退。
  • 颜色常量:kRed, kGreen, kBlack 等可直接使用。
3. ​​进阶功能​
  • ​多图层叠加​​:在同一Canvas绘制多个图形(如 hist->Draw("SAME"))。
  • ​拟合函数​​:hist->Fit("gaus") 可对数据做高斯拟合。

五、学习路径与资源
  • ​官方教程​​:
    • 安装后访问 $ROOTSYS/tutorials,含200+示例(如 hist/hist1.C)。
  • ​开发工具配置​​:
    • ​CLion + CMake​​:自动补全ROOT类成员(参考的CMake配置)。
    • ​VS Code​​:安装C++插件支持语法高亮。
  • ​中文社区​​:
    • 微信博客“每天总结一丢丢ROOT绘图方法”系列(常见问题解析)。
​入门要诀​​:
  • 从Cling交互式练习起步,熟悉C++语法与ROOT对象;
  • 掌握TTree/TH1F的数据读写逻辑;

后续本贴将逐步讨论CERN ROOT的直方图、散点图、拟合等常见功能的使用。



16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-10 21:37:47 | 查看全部
显示“直方图类”用法的示例。

直方图是定量数据分布的可视化表示。TH1 类引入了根目录中使用的基本数据结构。

简而言之:
// Open the file to write the histogram to
auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));

// Create the histogram object
// There are several constructors you can use (\see TH1). In this example we use the
// simplest one, accepting a number of bins and a range.
int nBins = 30;
double rangeMin = 0.0;
double rangeMax = 10.0;
TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);

// Fill the histogram. In this simple example we use a fake set of data.
// The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
// In general you should prefer TH1D over TH1F unless you have a very specific reason
// to do otherwise.
const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
for (double val : values) {
    histogram.Fill(val);
}

// Write the histogram to `outFile`.
outFile->WriteObject(&histogram, histogram.GetName());

// When the TFile goes out of scope it will close itself and write its contents to disk.
TH1 的 Hello World 示例。

演示如何创建、填充直方图并将其写入 ROOT 文件。
void hist000_TH1_first()
{
   // Open the file to write the histogram to
   auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));

   // Create the histogram object
   // There are several constructors you can use (\see TH1). In this example we use the
   // simplest one, accepting a number of bins and a range.
   int nBins = 30;
   double rangeMin = 0.0;
   double rangeMax = 10.0;
   TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);

   // Fill the histogram. In this simple example we use a fake set of data.
   // The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
   // In general you should prefer TH1D over TH1F unless you have a very specific reason
   // to do otherwise.
   const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
   for (double val : values) {
      histogram.Fill(val);
   }

   // Write the histogram to `outFile`.
   outFile->WriteObject(&histogram, histogram.GetName());

   // When the TFile goes out of scope it will close itself and write its contents to disk.
}

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-12 11:19:56 | 查看全部
TH2类直方图
显示“直方图类”用法的示例。

直方图是定量数据分布的可视化表示。TH1 类引入了根中用于直方图的基本数据结构。

// Open the file to write the histogram to
auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));

// Create the histogram object
// There are several constructors you can use (\see TH1). In this example we use the
// simplest one, accepting a number of bins and a range.
int nBins = 30;
double rangeMin = 0.0;
double rangeMax = 10.0;
TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);

// Fill the histogram. In this simple example we use a fake set of data.
// The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
// In general you should prefer TH1D over TH1F unless you have a very specific reason
// to do otherwise.
const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
for (double val : values) {
    histogram.Fill(val);
}

// Write the histogram to `outFile`.
outFile->WriteObject(&histogram, histogram.GetName());

// When the TFile goes out of scope it will close itself and write its contents to disk

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-13 21:46:57 | 查看全部
演示如何创建、填充直方图并将其写入 ROOT 文件。
import ROOT

# Open the file to write the histogram to
with ROOT.TFile.Open("outfile.root", "RECREATE") as outFile:
   # Create the histogram object
   # There are several constructors you can use (see TH1). In this example we use the
   # simplest one, accepting a number of bins and a range.
   histogram = ROOT.TH1D("histogram", "My first ROOT histogram", nbinsx = 30, xlow = 0.0, xup = 10.0)

   # Fill the histogram. In this simple example we use a fake set of data.
   # The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
   # In general you should prefer TH1D over TH1F unless you have a very specific reason
   # to do otherwise.
   values = [1, 2, 3, 3, 3, 4, 3, 2, 1, 0]
   for val in values:
      histogram.Fill(val)

   # Write the histogram to `outFile`.
   outFile.WriteObject(histogram, histogram.GetName())

   # When `with` block exits, `outFile` will close itself and write its contents to disk.

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-14 20:50:47 | 查看全部

import numpy as np
import ROOT

# Open the file to write the histogram to
with ROOT.TFile.Open("outfile_uhi.root", "RECREATE") as outFile:
    # Create the histogram object
    # There are several constructors you can use (see TH1). In this example we use the
    # simplest one, accepting a number of bins and a range.
    histogram = ROOT.TH1D("histogram", "My first ROOT histogram", nbinsx=30, xlow=0.0, xup=10.0)

    # Fill the histogram by passing a NumPy array. In this simple example we use a fake set of data.
    # The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
    # In general you should prefer TH1D over TH1F unless you have a very specific reason
    # to do otherwise.
    values = np.array([1, 2, 3, 3, 3, 4, 3, 2, 1, 0])
    counts, edges = np.histogram(values, bins=30, range=(0.0, 10.0))
    histogram[...] = counts

    # Write the histogram to `outFile`.
    outFile.WriteObject(histogram, histogram.GetName())

    # When `with` block exits, `outFile` will close itself and write its contents to disk.

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-15 20:01:08 | 查看全部
使用**义函数用随机值填充一维直方图。
void hist001_TH1_fillrandom()
{
   // Create a one dimensional histogram and fill it with a gaussian distribution
   int nBins = 200;
   double rangeMin = 0.0;
   double rangeMax = 10.0;
   TH1D h1d("h1d", "Test random numbers", nBins, rangeMin, rangeMax);

   // "gaus" is a predefined ROOT function. Here we are filling the histogram with
   // 10000 values sampled from that distribution.
   h1d.FillRandom("gaus", 10000);

   // Open a ROOT file and save the histogram
   auto myfile = std::unique_ptr<TFile>(TFile::Open("fillrandom.root", "RECREATE"));
   myfile->WriteObject(&h1d, h1d.GetName());
}
使用**义函数用随机值填充一维直方图。
import ROOT

# Create a one dimensional histogram and fill it with a gaussian distribution
h1d = ROOT.TH1D("h1d", "Test random numbers", nbinsx = 200, xlow = 0.0, xup = 10.0)

# "gaus" is a predefined ROOT function. Here we are filling the histogram with
# 10000 values sampled from that distribution.
h1d.FillRandom("gaus", 10000)

# Open a ROOT file and save the histogram
with ROOT.TFile.Open("fillrandom_py.root", "RECREATE") as myfile:
   myfile.WriteObject(h1d, h1d.GetName())

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-17 20:25:39 | 查看全部
使用**义函数用随机值填充一维直方图。
import numpy as np
import ROOT

np.random.seed(0)

# Create a one dimensional histogram and fill it with a gaussian distribution
h1d = ROOT.TH1D("h1d", "Test random numbers", nbinsx=200, xlow=0.0, xup=10.0)

# "gaus" is a predefined ROOT function. Here we are filling the histogram with
# 10000 values sampled from that distribution.
values = np.random.normal(0.0, 1.0, 10000)
h1d.Fill(values)

# Open a ROOT file and save the histogram
with ROOT.TFile.Open("fillrandom_py_uhi.root", "RECREATE") as myfile:
    myfile.WriteObject(h1d, h1d.GetName())
从用户定义的参数函数填充一维直方图。
void hist002_TH1_fillrandom_userfunc()
{
   // Create a user-defined formula.
   // A function (any dimension) or a formula may reference an already defined formula
   TFormula form1("form1", "abs(sin(x)/x)");

   // Create a 1D function using the formula defined above and the predefined "gaus" formula.
   double rangeMin = 0.0;
   double rangeMax = 10.0;
   TF1 sqroot("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax);
   sqroot.SetLineColor(4);
   sqroot.SetLineWidth(6);
   // Set parameters to the functions "gaus" and "form1".
   double gausScale = 10.0;  // [0]
   double gausMean = 4.0;    // [1]
   double gausVar = 1.0;     // [2]
   double form1Scale = 20.0; // [3]
   sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale);

   // Create a one dimensional histogram and fill it following the distribution in function sqroot.
   int nBins = 200;
   TH1D h1d("h1d", "Test random numbers", nBins, rangeMin, rangeMax);

   // Use our user-defined function to fill the histogram with random values sampled from it.
   h1d.FillRandom("sqroot", 10000);

   // Open a ROOT file and save the formula, function and histogram
   auto myFile = std::unique_ptr<TFile>(TFile::Open("fillrandom_userfunc.root", "RECREATE"));
   myFile->WriteObject(&form1, form1.GetName());
   myFile->WriteObject(&sqroot, sqroot.GetName());
   myFile->WriteObject(&h1d, h1d.GetName());
}

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-18 22:18:36 | 查看全部
从用户定义的参数函数填充一维直方图。
import ROOT

# Create a user-defined formula.
# A function (any dimension) or a formula may reference an already defined formula
form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")

# Create a 1D function using the formula defined above and the predefined "gaus" formula.
rangeMin = 0.0
rangeMax = 10.0
sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
sqroot.SetLineColor(4)
sqroot.SetLineWidth(6)
# Set parameters to the functions "gaus" and "form1".
gausScale = 10.0  # [0]
gausMean = 4.0    # [1]
gausVar = 1.0     # [2]
form1Scale = 20.0 # [3]
sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale)

# Create a one dimensional histogram and fill it following the distribution in function sqroot.
h1d = ROOT.TH1D("h1d", "Test random numbers", 200, rangeMin, rangeMax)

# Use our user-defined function to fill the histogram with random values sampled from it.
h1d.FillRandom("sqroot", 10000)

# Open a ROOT file and save the formula, function and histogram
with ROOT.TFile.Open("fillrandom_userfunc_py.root", "RECREATE") as myFile:
   myFile.WriteObject(form1, form1.GetName())
   myFile.WriteObject(sqroot, sqroot.GetName())
   myFile.WriteObject(h1d, h1d.GetName())
从用户定义的参数函数填充一维直方图。
import numpy as np
import ROOT

# Create a user-defined formula.
# A function (any dimension) or a formula may reference an already defined formula
form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")

# Create a 1D function using the formula defined above and the predefined "gaus" formula.
rangeMin = 0.0
rangeMax = 10.0
sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
# Set parameters to the functions "gaus" and "form1".
gausScale = 10.0  # [0]
gausMean = 4.0  # [1]
gausVar = 1.0  # [2]
form1Scale = 20.0  # [3]
sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale)

# Create a one dimensional histogram and fill it following the distribution in function sqroot.
h1d = ROOT.TH1D("h1d", "Test random numbers", 200, rangeMin, rangeMax)

# Use our user-defined function to fill the histogram with random values sampled from it.
h1d.Fill(np.array([sqroot.GetRandom() for _ in range(10000)]))

# Open a ROOT file and save the formula, function and histogram
with ROOT.TFile.Open("fillrandom_userfunc_py_uhi.root", "RECREATE") as myFile:
    myFile.WriteObject(form1, form1.GetName())
    myFile.WriteObject(sqroot, sqroot.GetName())
    myFile.WriteObject(h1d, h1d.GetName())

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-21 19:59:31 | 查看全部
从用户定义的参数函数填充一维直方图。
import numpy as np
import ROOT

# Create a user-defined formula.
# A function (any dimension) or a formula may reference an already defined formula
form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")

# Create a 1D function using the formula defined above and the predefined "gaus" formula.
rangeMin = 0.0
rangeMax = 10.0
sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
# Set parameters to the functions "gaus" and "form1".
gausScale = 10.0  # [0]
gausMean = 4.0  # [1]
gausVar = 1.0  # [2]
form1Scale = 20.0  # [3]
sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale)

# Create a one dimensional histogram and fill it following the distribution in function sqroot.
h1d = ROOT.TH1D("h1d", "Test random numbers", 200, rangeMin, rangeMax)

# Use our user-defined function to fill the histogram with random values sampled from it.
h1d.Fill(np.array([sqroot.GetRandom() for _ in range(10000)]))

# Open a ROOT file and save the formula, function and histogram
with ROOT.TFile.Open("fillrandom_userfunc_py_uhi.root", "RECREATE") as myFile:
    myFile.WriteObject(form1, form1.GetName())
    myFile.WriteObject(sqroot, sqroot.GetName())
    myFile.WriteObject(h1d, h1d.GetName())
在画布上绘制一维直方图。
void hist003_TH1_draw()
{
   // Create and fill the histogram.
   // See hist002_TH1_fillrandom_userfunc.C for more information about this section.
   auto *form1 = new TFormula("form1", "abs(sin(x)/x)");
   double rangeMin = 0.0;
   double rangeMax = 10.0;
   auto *sqroot = new TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax);
   sqroot->SetLineColor(4);
   sqroot->SetLineWidth(6);
   sqroot->SetParameters(10.0, 4.0, 1.0, 20.0);

   int nBins = 200;
   auto *h1d = new TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax);

   h1d->FillRandom("sqroot", 10000);

   // Create a canvas and draw the histogram
   int topX = 200;
   int topY = 10;
   int width = 700;
   int height = 900;
   auto *c1 = new TCanvas("c1", "The FillRandom example", topX, topY, width, height);

   // Split the canvas into two sections to plot both the function and the histogram
   // The TPad's constructor accepts the relative coordinates (0 to 1) of the pad's boundaries
   auto *pad1 = new TPad("pad1", "The pad with the function", 0.05, 0.50, 0.95, 0.95);
   auto *pad2 = new TPad("pad2", "The pad with the histogram", 0.05, 0.05, 0.95, 0.45);

   // Draw the two pads
   pad1->Draw();
   pad2->Draw();

   // Select pad1 to draw the next objects into
   pad1->cd();
   pad1->SetGridx();
   pad1->SetGridy();
   pad1->GetFrame()->SetBorderMode(-1);
   pad1->GetFrame()->SetBorderSize(5);

   // Draw the function in pad1
   sqroot->Draw();
   // Add a label to the function.
   // TPaveLabel's constructor accepts the pixel coordinates and the label string.
   auto *lfunction = new TPaveLabel(5, 39, 9.8, 46, "The sqroot function");
   lfunction->Draw();
   c1->Update();

   // Select pad2 to draw the next objects into
   pad2->cd();
   pad2->GetFrame()->SetBorderMode(-1);
   pad2->GetFrame()->SetBorderSize(5);

   h1d->SetFillColor(45);
   h1d->Draw();
   c1->Update();
}

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-22 19:40:09 | 查看全部
在画布上绘制一维直方图
import matplotlib.pyplot as plt
import mplhep as hep
import numpy as np
import ROOT

# Create and fill the histogram.
# See hist002_TH1_fillrandom_userfunc.C for more information about this section.
form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")
rangeMin = 0.0
rangeMax = 10.0
sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
sqroot.SetParameters(10.0, 4.0, 1.0, 20.0)

nBins = 200
h1d = ROOT.TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax)

random_numbers = np.array([sqroot.GetRandom() for _ in range(10000)])
h1d[...] = np.histogram(np.array([sqroot.GetRandom() for _ in range(10000)]), bins=nBins, range=(rangeMin, rangeMax))[0]

# Create a canvas and draw the histogram
plt.figure(figsize=(7, 9))

# Split the canvas into two sections to plot both the function and the histogram
plt.axes([0.05, 0.55, 0.90, 0.40])
x = np.linspace(rangeMin, rangeMax, 500)
plt.plot(x, [sqroot.Eval(xi) for xi in x], "b-", lw=5)
plt.grid()
plt.title("x*gaus(0) + [3]*form1")
plt.text(5, 40, "The sqroot function", fontsize=18, weight="bold", bbox=dict(facecolor="white", edgecolor="black"))
plt.xlim(rangeMin, rangeMax)
plt.ylim(bottom=0)

plt.axes([0.05, 0.05, 0.90, 0.40])
hep.histplot(h1d, yerr=False, histtype="fill", color="brown", alpha=0.7, edgecolor="blue", linewidth=1)

plt.title("Test random numbers")
plt.xlabel("x")
plt.ylabel("Entries")
plt.xlim(rangeMin, rangeMax)
plt.ylim(bottom=0)

stats_text = (
    f"Entries = {len(random_numbers)}\nMean = {(np.mean(random_numbers)):.3f}\nStd Dev = {(np.std(random_numbers)):.2f}"
)
plt.text(0.90, 0.90, stats_text, transform=plt.gca().transAxes, ha="right", va="top", bbox=dict(facecolor="white"))
plt.show()
您需要登录后才可以回帖 登录 | 注册

本版积分规则

  • 微信小程序
  • 公众号
  • 微信客服

关于我们|Archiver|APP客户端|小黑屋|物质结构社区 ( 闽ICP备2024081439号-1 )

GMT+8, 2025-9-7 14:41 , Processed in 0.019492 second(s), 5 queries , Redis On.

Powered by Discuz! X5.0

© 2001-2025 Discuz! Team.

在本版发帖
科研需求联系客服
添加微信客服
返回顶部
快速回复 返回顶部 返回列表