cernbuild.C

Go to the documentation of this file.
00001 // Read data (CERN staff) from an ascii file and create a root file with a Tree.
00002 // see also a variant in staff.C
00003 // Author: Rene Brun
00004    
00005 TFile *cernbuild(Int_t get=0) {
00006 
00007    Int_t           Category;
00008    UInt_t          Flag;
00009    Int_t           Age;
00010    Int_t           Service;
00011    Int_t           Children;
00012    Int_t           Grade;
00013    Int_t           Step;
00014    Int_t           Hrweek;
00015    Int_t           Cost;
00016    Char_t          Division[4];
00017    Char_t          Nation[3];
00018 
00019    //The input file cern.dat is a copy of the CERN staff data base
00020    //from 1988
00021    TString filename = "cernstaff.root";
00022    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
00023    dir.ReplaceAll("cernbuild.C","");
00024    dir.ReplaceAll("/./","/");
00025    FILE *fp = fopen(Form("%scernstaff.dat",dir.Data()),"r");
00026 
00027    TFile *hfile = 0;
00028    if (get) {
00029       // if the argument get =1 return the file "cernstaff.root"
00030       // if the file does not exist, it is created
00031       if (!gSystem->AccessPathName(dir+"cernstaff.root",kFileExists)) {
00032          hfile = TFile::Open(dir+"cernstaff.root"); //in $ROOTSYS/tutorials/tree
00033          if (hfile) return hfile;
00034       }
00035       //otherwise try $PWD/cernstaff.root
00036       if (!gSystem->AccessPathName("cernstaff.root",kFileExists)) {
00037          hfile = TFile::Open("cernstaff.root"); //in current dir
00038          if (hfile) return hfile;
00039       }
00040    }
00041    //no cernstaff.root file found. Must generate it !
00042    //generate cernstaff.root in $ROOTSYS/tutorials/tree if we have write access
00043    if (!gSystem->AccessPathName(dir,kWritePermission)) {
00044       filename = dir+"cernstaff.root";
00045    } else if (!gSystem->AccessPathName(".",kWritePermission)) {
00046       //otherwise generate cernstaff.root in the current directory
00047    } else {
00048       printf("you must run the script in a directory with write access\n");
00049       return 0;
00050    }
00051    hfile = TFile::Open(filename,"RECREATE");
00052    TTree *tree = new TTree("T","CERN 1988 staff data");
00053    tree->Branch("Category",&Category,"Category/I");
00054    tree->Branch("Flag",&Flag,"Flag/i");
00055    tree->Branch("Age",&Age,"Age/I");
00056    tree->Branch("Service",&Service,"Service/I");
00057    tree->Branch("Children",&Children,"Children/I");
00058    tree->Branch("Grade",&Grade,"Grade/I");
00059    tree->Branch("Step",&Step,"Step/I");
00060    tree->Branch("Hrweek",&Hrweek,"Hrweek/I");
00061    tree->Branch("Cost",&Cost,"Cost/I");
00062    tree->Branch("Division",Division,"Division/C");
00063    tree->Branch("Nation",Nation,"Nation/C");
00064    char line[80];
00065    while (fgets(&line,80,fp)) {
00066       sscanf(&line[0],"%d %d %d %d %d",&Category,&Flag,&Age,&Service,&Children);
00067       sscanf(&line[32],"%d %d  %d %d %s %s",&Grade,&Step,&Hrweek,&Cost,Division,Nation);
00068       tree->Fill();
00069    }
00070    tree->Print();
00071    tree->Write();
00072 
00073    fclose(fp);
00074    delete hfile;
00075    if (get) {
00076       //we come here when the script is executed outside $ROOTSYS/tutorials/tree
00077       hfile = TFile::Open(filename);
00078       return hfile;
00079    }
00080    return 0;
00081 }

Generated on Tue Jul 5 15:45:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1