Problem converting an Asci File

Hi,

I am trying to convert an Ascii File to root using basically the basic.C Example.

My code looks like this:

#include "Riostream.h"
void convert(char *name) 

{

    
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   //dir.ReplaceAll("basic.C","");
   dir.ReplaceAll("/./","/");
   //dir.ReplaceAll("/");
   ifstream in;

in.open(Form(name));

   Int_t nlines = 0;
   char treename[80];
   sprintf(treename,"%s.root",name);
   TFile *f = new TFile(treename,"RECREATE");
Float_t event_number,ch00_12bit,ch00_15bit,ch01_12bit,ch01_15bit,ch02_12bit,ch02_15bit,ch03_12bit,ch03_15bit,ch04_12bit,ch04_15bit,ch05_12bit,ch05_15bit,ch06_12bit,ch06_15bit,ch07_12bit,ch07_15bit,unix_seconds;
  
TNtuple *ntuple = new TNtuple("Datensatz","title","event_number:ch00_12bit:ch00_15bit:ch01_12bit:ch01_15bit:ch02_12bit:ch02_15bit:ch03_12bit:ch03_15bit:ch04_12bit:ch04_15bit:ch05_12bit:ch05_15bit:ch06_12bit:ch06_15bit:ch07_12bit:ch07_15bit:unix_seconds");

ntuple->ReadFile(name);


   in.close();

   f->Write();
}

A typical Line in my Asci File looks like this:

13 1528 4095 1517 4095 1505 4095 1491 4095 1508 4095 1538 4095 1526 4095 1514 4095 1288956844.4079007 2010-11-05 12:34:04.4079007 (UTC+01:00h)

I want to read in the line including this float 1288956844.4079007 being the unix_seconds

Now all channels are read in nicely and I can plot them using the root Viewer as expected.
The problem is the unix_seconds leaf.
I measured 3600 seconds with around 500Hz, read it into root and wanted to histogram this hour using a modulo. As far as I know this should do the trick tv__tree->Draw("unix_seconds % 3600 >> HIST(3600, 0, 3600)")

What I get is this:


What am I doing wrong?

Thank you in advance

a float (6 to 7 digits precisin) is not enough to store your numbers. You need minimum a double (15 to 16 digits).
Your number seems to have 17 digits.

Rene

Hi,

I declared them as long doubles now.

The result is a bit better, but not as expected.
In this example I measured about hours and recorded 1812181 Events.

I used the Make Class to create an Analyzing Macro.
And excerpt of the code goes like this:

TH1* timeHist = new TH1D("unix_seconds","Zeit",10000,1288966221,1288997999); timeHist->GetXaxis()->SetTitle("Time"); timeHist->GetXaxis()->SetTimeDisplay(1); // X axis is a time axis timeHist->GetXaxis()->SetTimeFormat("%H\:%M\:%S");

I noticed that in my corresponding .h file the leaf unix_seconds is declared like this:

// Declaration of leaf types Float_t event_number; Float_t ch00_12bit; Float_t ch00_15bit; Float_t ch01_12bit; Float_t ch01_15bit; Float_t ch02_12bit; Float_t ch02_15bit; Float_t ch03_12bit; Float_t ch03_15bit; Float_t ch04_12bit; Float_t ch04_15bit; Float_t ch05_12bit; Float_t ch05_15bit; Float_t ch06_12bit; Float_t ch06_15bit; Float_t ch07_12bit; Float_t ch07_15bit; Float_t unix_seconds;

Still my resulting Hitsogram shows not enough bins.
It should be 10000 and I estimate these are about 200


Thank you

[quote]I declared them as long doubles now.[/quote]humm … long double are not supported …

Philippe.

Thank you,

i tried different file types now: long64_t, double, and some else.

My “Time Histogram” always looks the the same.

The MakeClass always generates an Analyze.h file which declares the leafs as float_t

Any more hints?

MakeClass uses the data type declared inside your Tree.
You have to rewrite your Tree using double instead of float and you will see a better precision.

Rene

Thank you.

So I thought, yes.

My first post shows the excerpt of my code where I declared the leafs as Float_t. This file converts my Ascii to a root Tree.

No I tried to declare them in this file as double, etc (as I told before).
It got better when I declared it differently (Post 3) but it is still very chunky.

I can only repeat what I said in my previous mail. Your file contains branches declared as Float_t, not Double_t,
so it is not surprising if MakeClass generates code with a declaration of your variable as Float_t.

Rene

I think there is a misunderstanding.

Here is an excerpt of my code which converts the Asci to root right after you told me to change the data type (Post 2)

int event_number,ch00_12bit,ch00_15bit,ch01_12bit,ch01_15bit,ch02_12bit,ch02_15bit,ch03_12bit,ch03_15bit,ch04_12bit,ch04_15bit,ch05_12bit,ch05_15bit,ch06_12bit,ch06_15bit,ch07_12bit,ch07_15bit; Double_t unix_seconds;
You see, I already did this and when I plot directly from the TBrowser it still has gaps of seconds (after I rebinn).
It doesn’t seem to accept Double_t correctly when it converts.

Well, you made probably a cut/paste error. Your 2 lines do not tell me anything how you converted your data.

Rene

Ok then here is the whole deal:

#include "Riostream.h"
void convert(char *name) 

{

    
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   //dir.ReplaceAll("basic.C","");
   dir.ReplaceAll("/./","/");
   //dir.ReplaceAll("/");
   ifstream in;

in.open(Form(name));

   Int_t nlines = 0;
   char treename[80];
   sprintf(treename,"%s.root",name);
   TFile *f = new TFile(treename,"RECREATE");

int event_number,ch00_12bit,ch00_15bit,ch01_12bit,ch01_15bit,ch02_12bit,ch02_15bit,ch03_12bit,ch03_15bit,ch04_12bit,ch04_15bit,ch05_12bit,ch05_15bit,ch06_12bit,ch06_15bit,ch07_12bit,ch07_15bit;

Double_t unix_seconds;
  
TNtuple *ntuple = new TNtuple("Datensatz","title","event_number:ch00_12bit:ch00_15bit:ch01_12bit:ch01_15bit:ch02_12bit:ch02_15bit:ch03_12bit:ch03_15bit:ch04_12bit:ch04_15bit:ch05_12bit:ch05_15bit:ch06_12bit:ch06_15bit:ch07_12bit:ch07_15bit:unix_seconds");

ntuple->ReadFile(name);


   in.close();

   f->Write();
}

This is my converting code. is there an error?

The declaration

TNtuple("Datensatz","title","event_number:ch00_12bit:ch00_15bit:ch01_12bit:ch01_15bit:ch02_12bit:ch02_15bit:ch03_12bit:ch03_15bit:ch04_12bit:ch04_15bit:ch05_12bit:ch05_15bit:ch06_12bit:ch06_15bit:ch07_12bit:ch07_15bit:unix_seconds"); is wrong, it should be:

TNtuple("Datensatz","title","event_number/i:ch00_12bit:ch00_15bit:ch01_12bit:ch01_15bit:ch02_12bit:ch02_15bit:ch03_12bit:ch03_15bit:ch04_12bit:ch04_15bit:ch05_12bit:ch05_15bit:ch06_12bit:ch06_15bit:ch07_12bit:ch07_15bit:unix_seconds/D");
Note the “/i” and “/D”

Rene

Allright thank you.

I corrected the line to

After I convert and try to plot directly from the TBrowser, I get the following error in the command line:
Error in TTreeFormula::Compile: Bad numerical expression : “D.unix_seconds”

and

Error in TTreeFormula::Compile: Bad numerical expression : “i.event_number”

Suggestions?

Two requests:
1-could you post the result of ntuple->Print() at the end of the filling process.
2-could you post your ntuple->Draw command generating the error message?

Rene

I hope this what you’ve asked for.

I hung ntuple->printf() at the end of the file and I got this in the command line:

[code]******************************************************************************
*Tree :Datensatz : title *
*Entries : 242622 : Total = 18509203 bytes File Size = 3210123 *

  •    :          : Tree compression factor =   5.68                       *
    

*Br 0 :event_number/i : *
*Entries : 242622 : Total Size= 974340 bytes File Size = 272669 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 3.52 *

*Br 1 :ch00_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 530177 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 1.81 *

*Br 2 :ch00_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 3 :ch01_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 521249 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 1.84 *

*Br 4 :ch01_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 5 :ch02_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 537924 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 1.78 *

*Br 6 :ch02_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 7 :ch03_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 545689 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 1.76 *

*Br 8 :ch03_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 9 :ch04_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 184214 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 5.21 *

*Br 10 :ch04_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 11 :ch05_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 171977 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 5.58 *

*Br 12 :ch05_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 13 :ch06_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 194298 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 4.94 *

*Br 14 :ch06_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 15 :ch07_12bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 167372 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 5.74 *

*Br 16 :ch07_15bit : *
*Entries : 242622 : Total Size= 974200 bytes File Size = 8280 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 115.93 *

*Br 17 :unix_seconds/D : *
*Entries : 242622 : Total Size= 1948336 bytes File Size = 18314 *
*Baskets : 60 : Basket Size= 32000 bytes Compression= 104.83 *

[/code]

I didn’t use any command to draw it. I just opend the Tree in the browser and double clicked the lea “unix_seconds” which usually does a histogram. However now it produces the error mentioned before.

Edit:
I just played around a bit more.
I ran the MakeClass again over the root file (generated using your suggestions) and in the Analyze.h file the code goes like this.

[code] // Declaration of leaf types

Float_t ch07_12bit;
Float_t ch07_15bit;
Double_t unix_seconds/D;

// List of branches

TBranch *b_ch07_12bit; //!
TBranch *b_ch07_15bit; //!
TBranch *b_unix_seconds; //!


…[/code]

So it actually seems to declare the unix_seconds as Double (which is good I guess).
But when I try to Analyze it running Analyz a a using this code:

[code]#define Analyze_cxx
#include “Analyze.h”
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

void Analyze::Loop()
{

if (fChain == 0) return;

TH1* timeHist = new TH1D("unix_seconds/D","Zeit",10000,1288966221,1288997999);
timeHist->GetXaxis()->SetTitle("Time");
timeHist->GetXaxis()->SetTimeDisplay(1); // X axis is a time axis
timeHist->GetXaxis()->SetTimeFormat("%H\:%M\:%S");


timeHist->GetYaxis()->SetTitle("number of events");

Long64_t nentries = fChain->GetEntriesFast();

Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;

   timeHist->Fill(unix_seconds);

}
timeHist->Draw("");
}
[/code]

I get the following error message:

Error: Symbol unix_seconds is not defined in current scope Analyze.h:149: Error: Symbol D is not defined in current scope Analyze.h:149: Error: operator '/' divided by zero Analyze.h:149:
which points to this line in the Analyze.h file:

Maybe this helps locate the error.

I did not realize that you were creating a TNtuple instead of a TTree. A TNtuple supports only floats.
Try the following to create your file instead:

[code]void conv()

{
TFile *f = new TFile(“conv.root”,“RECREATE”);
TTree *ntuple = new TTree(“Datensatz”,“title”);

ntuple->ReadFile(“conv.dat”,“event_number/i:ch00_12bit:ch00_15bit:ch01_12bit:ch01_15bit:ch02_12bit:ch02_15bit:ch03_12bit:ch03_15bit:ch04_12bit:ch04_15bit:ch05_12bit:ch05_15bit:ch06_12bit:ch06_15bit:ch07_12bit:ch07_15bit:unix_seconds/D”);
ntuple->Print();

f->Write();
}
[/code]

Rene

IT WORKS!!!

Thank you very much. Where do I send that box of wine to now? :slight_smile:
Seriously though, thank you very much.

well! that’s very kind from your side, but I should have seen earlier that you were creating a TNuple and not a TTree. Too many mails ::slight_smile:

Rene