12
返回列表 发布新帖
楼主: pengdayu

[其它] CERN ROOT入门基础

16

帖子

38

积分

10

金币

新手上路

积分
38
 楼主| 发表于 2025-8-23 20:39:02 | 查看全部
TH1 可以具有填充方法重载 TH1::Fill(const char*, double) 的命名箱
TCanvas *hist004_TH1_labels()
{
   // Create the histogram
   const std::array people{"Jean",    "Pierre", "Marie",    "Odile",   "Sebastien", "Fons",  "Rene",
                           "Nicolas", "Xavier", "Greg",     "Bjarne",  "Anton",     "Otto",  "Eddy",
                           "Peter",   "Pasha",  "Philippe", "Suzanne", "Jeff",      "Valery"};
   // Start with an arbitrary amount of bins and an arbitrary range, but this will be extended thanks to SetCanExtend().
   int nBins = 3;
   double rangeMin = 0.0;
   double rangeMax = 3.0;
   auto *h = new TH1D("h", "test", nBins, rangeMin, rangeMax);
   // Disable the default stats box when drawing this histogram
   h->SetStats(0);
   h->SetFillColor(38);
   // Allow both axes to extend past the initial range we gave in the constructor
   h->SetCanExtend(TH1::kAllAxes);
   // Fill the Y axis with arbitrary values, a random amount per bin
   TRandom3 rng;
   for (int i = 0; i < 5000; i++) {
      int r = rng.Rndm() * 20;
      // `Fill()` called with a const char* as the first argument will add a value to the bin with that name,
      // creating it if it doesn't exist yet.
      h->Fill(people[r], 1);
   }
   // Remove empty bins
   h->Labe**eflate();

   auto *c1 = new TCanvas("c1", "demo bin labels", 10, 10, 900, 500);
   // Enable the grid in the plot
   c1->SetGrid();
   c1->SetTopMargin(0.15);

   // Draw the histogram
   h->Draw();

   // Draw a boxed text
   // "brNDC" = coordinates draw bottom-right shadow and pass the coordinates in normalized device coordinates
   auto *pt = new TPaveText(0.7, 0.85, 0.98, 0.98, "brNDC");
   pt->SetFillColor(18);
   pt->SetTextAlign(12);
   pt->AddText("Use the axis Context Menu LabelsOption");
   pt->AddText(" \"a\"   to sort by alphabetic order");
   pt->AddText(" \">\"   to sort by decreasing values");
   pt->AddText(" \"<\"   to sort by increasing values");
   pt->Draw();

   return c1;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

GMT+8, 2025-9-8 10:29 , Processed in 0.013641 second(s), 5 queries , Redis On.

Powered by Discuz! X5.0

© 2001-2025 Discuz! Team.

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