// Read data from an ascii file and create a root file with a Tree. // Author: Sorina Popescu, August 4, 2015 #include "TString.h" #include "TTree.h" #include "TFile.h" #include "TSystemDirectory.h" #include "TSystemFile.h" #include #include #include #include TFile *TestBuild(Int_t get=0, Int_t print=1) { Int_t Index; Float_t FHS; Float_t Spe; Float_t STI; Char_t Date[100]; Char_t WDir[100]; Float_t WSpe; Float_t Slip; Float_t WHgh; Int_t SSta; Float_t Drfw; Float_t Draf; //The input dat file is a download from data base in TEXT(TAB limited) .txt format TString filename = "TestBuild.root"; TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName()); dir.ReplaceAll("TestBuild.C",""); dir.ReplaceAll("/./","/"); FILE *fp = fopen(Form("%sTestBuild-new.dat",dir.Data()),"r"); TFile *hfile = 0; if (get) { // if the argument get =1 return the file "TestBuild.root" // if the file does not exist, it is created if (!gSystem->AccessPathName(dir+"TestBuild.root",kFileExists)) { hfile = TFile::Open(dir+"TestBuild.root"); if (hfile) return hfile; } if (!gSystem->AccessPathName("TestBuild.root",kFileExists)) { hfile = TFile::Open("TestBuild.root"); //in current dir if (hfile) return hfile; } } //no TestBuild.root file found. Must generate it ! if (!gSystem->AccessPathName(dir,kWritePermission)) { filename = dir+"TestBuild.root"; } else if (!gSystem->AccessPathName(".",kWritePermission)) { //otherwise generate TestBuild.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 *qa = new TTree("qa","TestBuilt"); qa->Branch("Index", &Index, "Index/I"); qa->Branch("FHS", &FHS, "FHS/F"); qa->Branch("Spe", &Spe, "Spe/F"); qa->Branch("STI", &STI, "STI/F"); qa->Branch("Dateandtime", &Date, "Date/C"); qa->Branch("WDir", &WDir, "WDir/C"); qa->Branch("WSpe", &WSpe, "WSpe/F"); qa->Branch("Slip", &Slip, "Slip/F"); qa->Branch("WHgh", &WHgh, "WHgh/F"); qa->Branch("SSta", &SSta, "SSta/I"); qa->Branch("Drfw", &Drfw, "Drfw/F"); qa->Branch("Draf", &Draf, "Draf/F"); std::ifstream ifs(fp, std::ifstream::in); while (ifs >> Index) { ifs >> FHS >> Spe >> STI >> Date >> WDir >> WSpe >> WSpe >> Slip >> WHgh >> SSta >> Drfw >> Draf; qa->Fill(); } ifs.close(); /* char line[84]; while (fgets(&line, 84, fp)) { sscanf(&line[0], "%d %f %f %f %s %s", &Index, &FHS, &Spe, &STI, Date, WDir); sscanf(&line[38], "%f %f %f %d %f %f", &WSpe, &Slip, &WHgh, &SSta, &Drfw, &Draf); qa->Fill(); }*/ /* char line[100]; while (fgets(&line,100,fp)) { /// sscanf(&line[0], "%d %f %f %f %s %s %f %f %f %d %f %f", &Index, &FHS, &Spe, &STI, Date, WDir, &WSpe, &Slip, &WHgh, &SSta, &Drfw, &Draf); sscanf(&line[0], "%d %f %f %f %s %s", &Index, &FHS, &Spe, &STI, Date, WDir); sscanf(&line[50], "%f %f %f %f %f %f", &WSpe, &Slip, &WHgh, &SSta, &Drfw, &Draf); qa->Fill(); }*/ if (print) qa->Print(); qa->Write(); fclose(fp); delete hfile; if (get) { hfile = TFile::Open(filename); return hfile; } return 0; }