// ----------------------------------------------------------------------------------- // /* ROOT macro for reading text (ASCII) files and writing ROOT Ntuples. The program also shows how to print (Latex table) into a file! Author: Troels C. Petersen (NBI/CERN) Email: Troels.Petersen@cern.ch Date: 19th of September 2011 */ // ----------------------------------------------------------------------------------- // #include #include #include #include using namespace std; // ----------------------------------------------------------------------------------- // void AsciiToNtuple() { // ----------------------------------------------------------------------------------- // // Reading from: ifstream in("DataSet_ElectronPion_2GeV.txt"); // Writing to: TNtuple* ntuple = new TNtuple("ntuple", "Ntuple from Ascii", "Cher:nTRT:nHT:ECal0:ECal1:ECal2:ECal3:EHad0:EHad1:EHad2:Mu"); FILE* textfile = fopen("LatexTable.tex", "w"); fprintf(textfile, " Event & Cher & fTRT & Ecalo0 \\\\ \n"); fprintf(textfile, " \\hline \\\\ \n"); int NeventsMax = 33000; // Maximum number of events is 33120 (2 GeV) and 120800 (9 GeV). int Nevents = 0; double x[11]; if (in.is_open()) { while ((!in.eof()) && (Nevents < NeventsMax)) { in >> x[0] >> x[1] >> x[2] >> x[3] >> x[4] >> x[5] >> x[6] >> x[7] >> x[8] >> x[9] >> x[10]; ntuple->Fill(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10]); Nevents++; if (Nevents%1000 == 0) printf(" Read %6d events. \n", Nevents); if (Nevents < 10) fprintf(textfile, " %3d & %6.1f & %5.3f & %6.3f \\\\ \n", Nevents, x[0], x[2]/x[1], x[3]); } } in.close(); fprintf(textfile, " \\hline \\\\ \n"); fclose(textfile); printf(" Number of entries in total: %6d \n", Nevents); //---------------------------------------------------------------------------------- // Write histograms to file: //---------------------------------------------------------------------------------- TFile *file = new TFile("DataSet_ElectronPion_2GeV.root","RECREATE","Histograms"); ntuple->Write(); file->Write(); file->Close(); } // ----------------------------------------------------------------------------------- // /* The Ntuple can be easily and quickly analysed by simple commands. An example can be found below. Examples of commands: --------------------- [top:AsciiToNtuple] > root -l DataSet_ElectronPion_2GeV.root root [0] Attaching file DataSet_ElectronPion_2GeV.root as _file0... root [1] TBrowser a root [2] ntuple->Draw("Cher") : created default TCanvas with name c1 root [3] ntuple->Draw("Cher","nHT/nTRT > 0.20") (Long64_t)8212 root [4] ntuple->Draw("Cher","nHT/nTRT < 0.05") (Long64_t)4111 root [5] ntuple->Draw("Cher","nHT/nTRT < 0.05 && EHad0+EHad1+EHad2 > 0.2") (Long64_t)780 root [6] ntuple->Draw("Cher","EHad0+EHad1+EHad2 > 0.2") (Long64_t)1493 ... root [12] ntuple->Draw("ECal1","EHad0+EHad1+EHad2 > 0.2") (Long64_t)1493 root [13] ntuple->Draw("ECal1","Cher > 700 && nHT/nTRT > 0.25", "same") */ // ----------------------------------------------------------------------------------- //