Hilfe! Will ASCII-File auslesen!



  • Bitte den Beitrag hier löschen, hab ihn schon selbst in den richtigen Thread verschoben!

    Hi, ich versuch jetzt schon seit längerem ein ASCII-File auszulesen und es als Histogramm bzw. ntuple darzustellen, allerdings hab ich ein kleines Problem. Ich benutze ROOT, also eine erweiterung zu C++, auf der ROOT-Seite vom CERN gibt es dazu ein paar Tuts, auch eines zum ASCII-FIle auslesen. Hier der Code:

    {
    //   example of macro to read data from an ascii file and
    //   create a root file with an histogram and an ntuple.
       gROOT->Reset();
    #include "Riostream.h"
    
       ifstream in;
    // we assume a file basic.dat in the current directory
    // this file has 3 columns of float data
       in.open("basic.dat");
    
       Float_t x,y,z;
       Int_t nlines = 0;
       TFile *f = new TFile("basic.root","RECREATE");
       TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
       TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z");
    
       while (1) {
          in >> x >> y >> z;
          if (!in.good()) break;
          if (nlines < 5) printf("x=%8f, y=%8f, z=%8fn",x,y,z);
          h1->Fill(x);
          ntuple->Fill(x,y,z);
          nlines++;
       }
       printf(" found %d pointsn",nlines);
    
       in.close();
    
       f->Write();
    }
    

    jetzt will ich aber kein 3 Spaltiges ASCII-File auslesen sondern nur eine Spalte und die Counts gegenüber den Bins auftragen, hier ma ein teil meines Versuches:

    #include "Riostream.h"
    #include <math.h>
    #include <TSystem.h>
    #include <TStopwatch.h>
    
    //void fitting();
    
    {
    
       /* INPUT File */
    
      char c_input_path[256] = "Datei.txt" ;  //path to datafile
    
      /* OUTPUT Files*/
    
      char c_output_path[256] = "Datei.root" ;  //path to datafile
    
       TH1F *TH_h1 						=NULL;
    
         	sprintf(c_name_histo, "h1");
    	sprintf(c_name_title, "h1;counts;bin","RECREATE");
    	TH_h1  = new TH1F(c_name_histo, c_name_title, 100, -4, 4);
    
       TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x");
    
       ifstream in;
    
       in.open("Datei.txt");
    
       Float_t x;
       Int_t nlines = 0;
    
       while (1) {
          in >> x;
          if (!in.good()) break;
          if (nlines < 5) printf("x=%8fn",x);
          TH_h1->Fill(x);
          ntuple->Fill(x);
          nlines++;
       }
    
       TOA_TH_list.Add(TH_h1);
    
       printf(" found %d pointsn",nlines);
    
       in.close();
    
       TOA_TH_list.Write();
       f_output.Map();
       f_output.Close();
    }
    

    Ich bin mir nicht ganz sicher was die in befehle machen und vorallem bei dem in.good() hab ich den Zweck nich ganz kapiert, finde dazu auch nichts im Internet. Wäre nett wenn mir jemand helfen könnte!


Anmelden zum Antworten