<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Brauche Hilfe! Will ASCII-File in ROOT einlesen!]]></title><description><![CDATA[<p>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:</p>
<pre><code>{
//   example of macro to read data from an ascii file and
//   create a root file with an histogram and an ntuple.
   gROOT-&gt;Reset();
#include &quot;Riostream.h&quot;

   ifstream in;
// we assume a file basic.dat in the current directory
// this file has 3 columns of float data
   in.open(&quot;basic.dat&quot;);

   Float_t x,y,z;
   Int_t nlines = 0;
   TFile *f = new TFile(&quot;basic.root&quot;,&quot;RECREATE&quot;);
   TH1F *h1 = new TH1F(&quot;h1&quot;,&quot;x distribution&quot;,100,-4,4);
   TNtuple *ntuple = new TNtuple(&quot;ntuple&quot;,&quot;data from ascii file&quot;,&quot;x:y:z&quot;);

   while (1) {
      in &gt;&gt; x &gt;&gt; y &gt;&gt; z;
      if (!in.good()) break;
      if (nlines &lt; 5) printf(&quot;x=%8f, y=%8f, z=%8fn&quot;,x,y,z);
      h1-&gt;Fill(x);
      ntuple-&gt;Fill(x,y,z);
      nlines++;
   }
   printf(&quot; found %d pointsn&quot;,nlines);

   in.close();

   f-&gt;Write();
}
</code></pre>
<p>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:</p>
<pre><code>#include &quot;Riostream.h&quot;
#include &lt;math.h&gt;
#include &lt;TSystem.h&gt;
#include &lt;TStopwatch.h&gt;

//void fitting();

{

   /* INPUT File */

  char c_input_path[256] = &quot;Datei.txt&quot; ;  //path to datafile

  /* OUTPUT Files*/

  char c_output_path[256] = &quot;Datei.root&quot; ;  //path to datafile

   TH1F *TH_h1 						=NULL;

     	sprintf(c_name_histo, &quot;h1&quot;);
	sprintf(c_name_title, &quot;h1;counts;bin&quot;,&quot;RECREATE&quot;);
	TH_h1  = new TH1F(c_name_histo, c_name_title, 100, -4, 4);

   TNtuple *ntuple = new TNtuple(&quot;ntuple&quot;,&quot;data from ascii file&quot;,&quot;x&quot;);

   ifstream in;

   in.open(&quot;Datei.txt&quot;);

   Float_t x;
   Int_t nlines = 0;

   while (1) {
      in &gt;&gt; x;
      if (!in.good()) break;
      if (nlines &lt; 5) printf(&quot;x=%8fn&quot;,x);
      TH_h1-&gt;Fill(x);
      ntuple-&gt;Fill(x);
      nlines++;
   }

   TOA_TH_list.Add(TH_h1);

   printf(&quot; found %d pointsn&quot;,nlines);

   in.close();

   TOA_TH_list.Write();
   f_output.Map();
   f_output.Close();
}
</code></pre>
<p>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!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/222288/brauche-hilfe-will-ascii-file-in-root-einlesen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 05 Oct 2026 04:09:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/222288.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Sep 2008 10:13:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Brauche Hilfe! Will ASCII-File in ROOT einlesen! on Wed, 10 Sep 2008 10:13:35 GMT]]></title><description><![CDATA[<p>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:</p>
<pre><code>{
//   example of macro to read data from an ascii file and
//   create a root file with an histogram and an ntuple.
   gROOT-&gt;Reset();
#include &quot;Riostream.h&quot;

   ifstream in;
// we assume a file basic.dat in the current directory
// this file has 3 columns of float data
   in.open(&quot;basic.dat&quot;);

   Float_t x,y,z;
   Int_t nlines = 0;
   TFile *f = new TFile(&quot;basic.root&quot;,&quot;RECREATE&quot;);
   TH1F *h1 = new TH1F(&quot;h1&quot;,&quot;x distribution&quot;,100,-4,4);
   TNtuple *ntuple = new TNtuple(&quot;ntuple&quot;,&quot;data from ascii file&quot;,&quot;x:y:z&quot;);

   while (1) {
      in &gt;&gt; x &gt;&gt; y &gt;&gt; z;
      if (!in.good()) break;
      if (nlines &lt; 5) printf(&quot;x=%8f, y=%8f, z=%8fn&quot;,x,y,z);
      h1-&gt;Fill(x);
      ntuple-&gt;Fill(x,y,z);
      nlines++;
   }
   printf(&quot; found %d pointsn&quot;,nlines);

   in.close();

   f-&gt;Write();
}
</code></pre>
<p>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:</p>
<pre><code>#include &quot;Riostream.h&quot;
#include &lt;math.h&gt;
#include &lt;TSystem.h&gt;
#include &lt;TStopwatch.h&gt;

//void fitting();

{

   /* INPUT File */

  char c_input_path[256] = &quot;Datei.txt&quot; ;  //path to datafile

  /* OUTPUT Files*/

  char c_output_path[256] = &quot;Datei.root&quot; ;  //path to datafile

   TH1F *TH_h1 						=NULL;

     	sprintf(c_name_histo, &quot;h1&quot;);
	sprintf(c_name_title, &quot;h1;counts;bin&quot;,&quot;RECREATE&quot;);
	TH_h1  = new TH1F(c_name_histo, c_name_title, 100, -4, 4);

   TNtuple *ntuple = new TNtuple(&quot;ntuple&quot;,&quot;data from ascii file&quot;,&quot;x&quot;);

   ifstream in;

   in.open(&quot;Datei.txt&quot;);

   Float_t x;
   Int_t nlines = 0;

   while (1) {
      in &gt;&gt; x;
      if (!in.good()) break;
      if (nlines &lt; 5) printf(&quot;x=%8fn&quot;,x);
      TH_h1-&gt;Fill(x);
      ntuple-&gt;Fill(x);
      nlines++;
   }

   TOA_TH_list.Add(TH_h1);

   printf(&quot; found %d pointsn&quot;,nlines);

   in.close();

   TOA_TH_list.Write();
   f_output.Map();
   f_output.Close();
}
</code></pre>
<p>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!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1579658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1579658</guid><dc:creator><![CDATA[DerKaiser]]></dc:creator><pubDate>Wed, 10 Sep 2008 10:13:35 GMT</pubDate></item></channel></rss>