简短的问题: 如果您必须有许多(甚至超过 200 个)成员变量(它们中的每一个都将是我们分析中感兴趣的一些物理量的图)。声明这些变量的最佳位置是什么?
长解释: 我用于分析的框架创建了一个循环事件的类,它可以缩小到
constructor()
initialize()
execute()
finalize()
在标题中,您可以声明如下指针(这显然是ROOT
我们必须使用的包的要求):
std::vector<double> *m_jet_pt;
和指向直方图类的指针:
TH1F *h_jet_pt;
然后constructor
必须初始化指向某个明确内存地址的指针(据我所知,这是为了以后从文件中读取数据)
constructor()
{
// this is data, will be associated to a TTree later
m_jet_pt = 0;
// this is a histogram
h_jet_pt = new TH1F("name", "title", nbins, min_bin, max_bin);
}
然后在initialize
函数中打开一个包含先前存储的数据的文件,并将类成员的地址设置为指向文件中包含的对象(我不知道这是如何工作的,或者我的陈述是否准确):
TFile *file = new TFile("filename.root");
// The tree is where the data is (for some reason ROOT uses C style casts)
TTree *tree = (TTree*)file->Get("MyTree");
// this is how to set the address of the member class pointer
// to point to the data in the tree:
tree->SetBranchAddress("m_jet_pt", &jet_pt);
然后在execute
循环中调用函数,每个事件一次,你会做你想要的物理选择
execute()
{
// I omit here where the `i` index comes from as not relevant
if(m_jet_pt->at(i) > 30)
h_jet_pt->Fill();
}
最后在finalize
函数中,您将在选择完成后的每个事件中执行所有您需要做的事情,例如将直方图存储在另一个文件中
finalize()
{
h_jet_pt->Write();
}
现在想象一下,我有 20 个 quantites jets
,30 个electrons
,30 个muons
等等,你可以看到成员变量的数量是如何变得巨大的!很快代码就变得一团糟,那么您的专业程序员将如何处理这种情况?希望这足够清楚!