独立读取多堆文件

Reading multipile files independently

本文关键字:文件 读取 独立      更新时间:2023-10-16

我有一个相当大的数据分析软件根(CERN)的代码,我有一连串的数据,我想通过这些数据来查找糟糕的运行。我将它们全部放在一个目录中,但想编写一段代码来一次从此文件夹中取出一个文件,运行代码,输出结果图形,然后获取下一个文件。等。我正在使用宏像现在一样运行此代码。我希望只是向该宏添加一些内容。我对编程有点新手。

gSystem->Load("AlgoCompSelector_C.so");
// make the chains
std::string filekey;
TChain tree1 = new TChain("tree");
filekey = std::string("data/run715604.EEmcTree_Part1.root");
tree1->Add( filekey.data() );

要在单个根宏中执行此操作,您可以尝试类似于下面的代码片段。 在这里,我将文件添加到 TChain 中,但您当然可以用您想要的任何内容替换TChain::Add

int addfiles(TChain *ch, const char *dirname=".", const char *ext=".root")
{
   int added = 0;
   TSystemDirectory dir(dirname, dirname);
   TList *files = dir.GetListOfFiles();
   if (files) {
      TSystemFile *file;
      TString fname;
      TIter next(files);
      while ((file=(TSystemFile*)next())) {
         fname = file->GetName();
         if (!file->IsDirectory() && fname.EndsWith(ext)) {
         ch->Add(fname); // or call your function on this one file
         ++added;
         }
     }
   }
   return added;
}

(改编自这篇根话帖子:http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=13666)

话虽如此,我认为@m0skit0每次都启动一个较小的脚本的建议比执行您上面建议的操作更好。根是挑剔的,工作越小越好。