ROOT logo

From $ROOTSYS/tutorials/tree/cernbuild.C

// Read data (CERN staff) from an ascii file and create a root file with a Tree.
// see also a variant in staff.C
// Author: Rene Brun
   
TFile *cernbuild(Int_t get=0, Int_t print=1) {

   Int_t           Category;
   UInt_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];

   //The input file cern.dat is a copy of the CERN staff data base
   //from 1988
   TString filename = "cernstaff.root";
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("cernbuild.C","");
   dir.ReplaceAll("/./","/");
   FILE *fp = fopen(Form("%scernstaff.dat",dir.Data()),"r");

   TFile *hfile = 0;
   if (get) {
      // if the argument get =1 return the file "cernstaff.root"
      // if the file does not exist, it is created
      if (!gSystem->AccessPathName(dir+"cernstaff.root",kFileExists)) {
         hfile = TFile::Open(dir+"cernstaff.root"); //in $ROOTSYS/tutorials/tree
         if (hfile) return hfile;
      }
      //otherwise try $PWD/cernstaff.root
      if (!gSystem->AccessPathName("cernstaff.root",kFileExists)) {
         hfile = TFile::Open("cernstaff.root"); //in current dir
         if (hfile) return hfile;
      }
   }
   //no cernstaff.root file found. Must generate it !
   //generate cernstaff.root in $ROOTSYS/tutorials/tree if we have write access
   if (!gSystem->AccessPathName(dir,kWritePermission)) {
      filename = dir+"cernstaff.root";
   } else if (!gSystem->AccessPathName(".",kWritePermission)) {
      //otherwise generate cernstaff.root in the current directory
   } else {
      printf("you must run the script in a directory with write access\n");
      return 0;
   }
   hfile = TFile::Open(filename,"RECREATE");
   TTree *tree = new TTree("T","CERN 1988 staff data");
   tree->Branch("Category",&Category,"Category/I");
   tree->Branch("Flag",&Flag,"Flag/i");
   tree->Branch("Age",&Age,"Age/I");
   tree->Branch("Service",&Service,"Service/I");
   tree->Branch("Children",&Children,"Children/I");
   tree->Branch("Grade",&Grade,"Grade/I");
   tree->Branch("Step",&Step,"Step/I");
   tree->Branch("Hrweek",&Hrweek,"Hrweek/I");
   tree->Branch("Cost",&Cost,"Cost/I");
   tree->Branch("Division",Division,"Division/C");
   tree->Branch("Nation",Nation,"Nation/C");
   char line[80];
   while (fgets(&line,80,fp)) {
      sscanf(&line[0],"%d %d %d %d %d",&Category,&Flag,&Age,&Service,&Children);
      sscanf(&line[32],"%d %d  %d %d %s %s",&Grade,&Step,&Hrweek,&Cost,Division,Nation);
      tree->Fill();
   }
   if (print) tree->Print();
   tree->Write();

   fclose(fp);
   delete hfile;
   if (get) {
      //we come here when the script is executed outside $ROOTSYS/tutorials/tree
      hfile = TFile::Open(filename);
      return hfile;
   }
   return 0;
}
 cernbuild.C:1
 cernbuild.C:2
 cernbuild.C:3
 cernbuild.C:4
 cernbuild.C:5
 cernbuild.C:6
 cernbuild.C:7
 cernbuild.C:8
 cernbuild.C:9
 cernbuild.C:10
 cernbuild.C:11
 cernbuild.C:12
 cernbuild.C:13
 cernbuild.C:14
 cernbuild.C:15
 cernbuild.C:16
 cernbuild.C:17
 cernbuild.C:18
 cernbuild.C:19
 cernbuild.C:20
 cernbuild.C:21
 cernbuild.C:22
 cernbuild.C:23
 cernbuild.C:24
 cernbuild.C:25
 cernbuild.C:26
 cernbuild.C:27
 cernbuild.C:28
 cernbuild.C:29
 cernbuild.C:30
 cernbuild.C:31
 cernbuild.C:32
 cernbuild.C:33
 cernbuild.C:34
 cernbuild.C:35
 cernbuild.C:36
 cernbuild.C:37
 cernbuild.C:38
 cernbuild.C:39
 cernbuild.C:40
 cernbuild.C:41
 cernbuild.C:42
 cernbuild.C:43
 cernbuild.C:44
 cernbuild.C:45
 cernbuild.C:46
 cernbuild.C:47
 cernbuild.C:48
 cernbuild.C:49
 cernbuild.C:50
 cernbuild.C:51
 cernbuild.C:52
 cernbuild.C:53
 cernbuild.C:54
 cernbuild.C:55
 cernbuild.C:56
 cernbuild.C:57
 cernbuild.C:58
 cernbuild.C:59
 cernbuild.C:60
 cernbuild.C:61
 cernbuild.C:62
 cernbuild.C:63
 cernbuild.C:64
 cernbuild.C:65
 cernbuild.C:66
 cernbuild.C:67
 cernbuild.C:68
 cernbuild.C:69
 cernbuild.C:70
 cernbuild.C:71
 cernbuild.C:72
 cernbuild.C:73
 cernbuild.C:74
 cernbuild.C:75
 cernbuild.C:76
 cernbuild.C:77
 cernbuild.C:78
 cernbuild.C:79
 cernbuild.C:80
 cernbuild.C:81
 cernbuild.C:82