关于ROOT中Tree的简单练手

Tree的简单练手

起因

  • ROOTUserGuide里Tree的官方教程中,fgets对数组的取用出现错误。
  • 结构体中的变量全都是整形,但我们平时对于变量的定义大多是不同类型的,比如有的是Int_t的,有的是Char_t类型的,所以写法会与教程中有所不同。

对象

这里针对$HOME/root/tutorials/tree/cernstaff.dat,用tree结构保存.dat文件中的数据,写了三个版本,读者可以将程序复制到macros中直接测试使用。

方法

  • 把cernstaff.dat文件中的每一行数据作为tree的一个entry。读入一行里的每个数据Fill一个entry。
  • 前9个变量是Int_t类型的数据,最后两个是Char_t类型的。对于Int_t类型的数据,分着挂,或者一起挂到Branch上都可以。对于后两个Char_t类型的数据,要使用Char_t数组格式来保存数据。
  • 两种类型的数据在Branch上,需要分开分配位置。
  • 直到将数据文件读完。

cernstaff1.C

{
   gROOT->Reset();

   // the structure to hold the variables for the branch

   struct staff_t {
      Int_t cat;
      
      Int_t flag;
      Int_t age;
      Int_t service;
      Int_t children;
      Int_t grade;
      Int_t step;
      
      Int_t hrweek;
      Int_t cost;

      Char_t division[4];
      Char_t nation[3];
   };
   staff_t staff;
   
   //use FILE to open the data file
   FILE   *fp = fopen("cernstaff.dat","r");//what is r??
   char line[81];

   //use TFile to create a ROOT File
   TFile *f = new TFile("staff.root","RECREATE");

   //use TTree to create a tree
   TTree *tree = new TTree("T","staff date from ascii file");
   
   //create branches with all information from the structure
   tree->Branch("cat",&staff.cat,"cat/I");
   tree->Branch("flag",&staff.flag,"flag/I");
   tree->Branch("age",&staff.age,"age/I");
   tree->Branch("service",&staff.service,"service/I");
   tree->Branch("children",&staff.children,"children/I");
   tree->Branch("grade",&staff.grade,"grade/I");
   tree->Branch("step",&staff.step,"step/I");
   tree->Branch("hrweek",&staff.hrweek,"hrweek/I");
   tree->Branch("cost",&staff.cost,"cost/I");
   tree->Branch("division",staff.division,"division/C");
   tree->Branch("nation",staff.nation,"nation/C");

   // Fill 
   while (fgets(line,80,fp))
   {
       sscanf(&line[0],"%d %d %d %d %d %d %d %d %d %s %s",&staff.cat,&staff.flag,&staff.age,&staff.service,&staff.children,&staff.grade,&staff.step,&staff.hrweek,&staff.cost,staff.division,staff.nation);

       tree->Fill();
   
   }
   
   tree->Print();
   fclose(fp);
   f->Write();
}

cernstaff2.C

{
   gROOT->Reset();

   // the structure to hold the variables for the branch

   struct staff_t {
      Int_t cat;
      
      Int_t flag;
      Int_t age;
      Int_t service;
      Int_t children;
      Int_t grade;
      Int_t step;
      
      Int_t hrweek;
      Int_t cost;

      Char_t division[4];
      Char_t nation[3];
   };
   staff_t staff;
   
   //use FILE to open the data file
   FILE   *fp = fopen("cernstaff.dat","r");//what is r??
   char line[81];

   //use TFile to create a ROOT File
   TFile *f = new TFile("staff.root","RECREATE");

   //use TTree to create a tree
   TTree *tree = new TTree("T","staff date from ascii file");
   
   //create branches with all information from the structure
   tree->Branch("cat",&staff.cat,"cat/I");
   tree->Branch("flag",&staff.flag,"flag/I");
   tree->Branch("age",&staff.age,"age/I");
   tree->Branch("service",&staff.service,"service/I");
   tree->Branch("children",&staff.children,"children/I");
   tree->Branch("grade",&staff.grade,"grade/I");
   tree->Branch("step",&staff.step,"step/I");
   tree->Branch("hrweek",&staff.hrweek,"hrweek/I");
   tree->Branch("cost",&staff.cost,"cost/I");
   tree->Branch("division",staff.division,"division/C");
   tree->Branch("nation",staff.nation,"nation/C");

   // Fill 
   while (fgets(line,80,fp))
   {
       sscanf(&line[0],"%d %d %d %d",&staff.cat,&staff.flag,&staff.age,&staff.service);
       sscanf(&line[25],"%d %d %d %d",&staff.children,&staff.grade,&staff.step,&staff.hrweek);
       sscanf(&line[50],"%d %s %s",&staff.cost,staff.division,staff.nation);
       tree->Fill();
   
   }
   
   tree->Print();
   fclose(fp);
   f->Write();
}

cernstaff3.C

{
   gROOT->Reset();

   // the structure to hold the variables for the branch

   struct staff_t {
      Int_t cat;
      
      Int_t flag;
      Int_t age;
      Int_t service;
      Int_t children;
      Int_t grade;
      Int_t step;
      
      Int_t hrweek;
      Int_t cost;

      Char_t division[4];
      Char_t nation[3];
   };
   staff_t staff;
   
   //use FILE to open the data file
   FILE   *fp = fopen("cernstaff.dat","r");//what is r??
   char line[81];

   //use TFile to create a ROOT File
   TFile *f = new TFile("staff.root","RECREATE");

   //use TTree to create a tree
   TTree *tree = new TTree("T","staff date from ascii file");
   
   //create branches with all information from the structure
   tree->Branch("cat",&staff.cat,"cat/I:flag:age:service:children:grade:step:hrweek:cost");
   tree->Branch("division",staff.division,"division/C");
   tree->Branch("nation",staff.nation,"nation/C");

   // Fill 
   while (fgets(line,80,fp))
   {
       sscanf(&line[0],"%d %d %d %d",&staff.cat,&staff.flag,&staff.age,&staff.service);
       sscanf(&line[25],"%d %d %d %d",&staff.children,&staff.grade,&staff.step,&staff.hrweek);
       sscanf(&line[50],"%d %s %s",&staff.cost,staff.division,staff.nation);
       tree->Fill();
   
   }
   
   tree->Print();
   fclose(fp);
   f->Write();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值