Logo ROOT   6.12/06
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Namespaces
parse_CSV_file_with_TTree_ReadStream.py File Reference

Namespaces

 parse_CSV_file_with_TTree_ReadStream
 

Detailed Description

This function provides an example of how one might massage a csv data file to read into a ROOT TTree via TTree::ReadStream.

This could be useful if the data read out from some DAQ program doesn't 'quite' match the formatting expected by ROOT (e.g. comma- separated, tab-separated with white-space strings, headers not matching the expected format, etc.)

This example is shipped with a data file that looks like:

Date/Time Synchro Capacity Temp.Cold Head Temp. Electrode HV Supply Voltage Electrode 1 Electrode 2 Electrode 3 Electrode 4
# Example data to read out. Some data have oddities that might need to
# dealt with, including the 'NaN' in Electrode 4 and the empty string in Date/Time (last row)
08112010.160622 7 5.719000E-10 8.790500 24.237700 -0.008332 0 0 0 0
8112010.160626 7 5.710000E-10 8.828400 24.237500 -0.008818 0 0 0 0
08112010.160626 7 5.719000E-10 8.828400 24.237500 -0.008818 0 0 0 0
08112010.160627 7 5.719000E-10 9.014300 24.237400 -0.028564 0 0 0 NaN
08112010.160627 7 5.711000E-10 8.786000 24.237400 -0.008818 0 0 0 0
08112010.160628 7 5.702000E-10 8.786000 24.237400 -0.009141 0 0 0 0
08112010.160633 7 5.710000E-10 9.016200 24.237200 -0.008818 0 0 0 0
7 5.710000E-10 8.903400 24.237200 -0.008818 0 0 0 0

These data require some massaging, including:

1 
2 import ROOT
3 import sys
4 import os
5 
6 def parse_CSV_file_with_TTree_ReadStream(tree_name, afile):
7 
8 
9  ROOT.gROOT.SetBatch()
10  # The mapping dictionary defines the proper branch names and types given a header name.
11  header_mapping_dictionary = {
12  'Date/Time' : ('Datetime' , str) ,
13  'Synchro' : ('Synchro' , int) ,
14  'Capacity' : ('Capacitance' , float) ,
15  'Temp.Cold Head' : ('TempColdHead' , float) ,
16  'Temp. Electrode' : ('TempElectrode' , float) ,
17  'HV Supply Voltage' : ('HVSupplyVoltage', float) ,
18  'Electrode 1' : ('Electrode1' , int) ,
19  'Electrode 2' : ('Electrode2' , int) ,
20  'Electrode 3' : ('Electrode3' , int) ,
21  'Electrode 4' : ('Electrode4' , int) ,
22  }
23 
24  type_mapping_dictionary = {
25  str : 'C',
26  int : 'I',
27  float : 'F'
28  }
29 
30 
31 
32  # Grab the header row of the file. In this particular example,
33  # the data are separated using tabs, but some of the header names
34  # include spaces and are not generally in the ROOT expected format, e.g.
35  #
36  # FloatData/F:StringData/C:IntData/I
37  #
38  # etc. Therefore, we grab the header_row of the file, and use
39  # a python dictionary to set up the appropriate branch descriptor
40  # line.
41 
42  # Open a file, grab the first line, strip the new lines
43  # and split it into a list along 'tab' boundaries
44  header_row = open(afile).readline().strip().split('\t')
45  # Create the branch descriptor
46  branch_descriptor = ':'.join([header_mapping_dictionary[row][0]+'/'+
47  type_mapping_dictionary[header_mapping_dictionary[row][1]]
48  for row in header_row])
49  #print branch_descriptor
50 
51  # Handling the input and output names. Using the same
52  # base name for the ROOT output file.
53  output_ROOT_file_name = os.path.splitext(afile)[0] + '.root'
54  output_file = ROOT.TFile(output_ROOT_file_name, 'recreate')
55  print "Outputting %s -> %s" % (afile, output_ROOT_file_name)
56 
57  output_tree = ROOT.TTree(tree_name, tree_name)
58  file_lines = open(afile).readlines()
59 
60  # Clean the data entries: remove the first (header) row.
61  # Ensure empty strings are tagged as such since
62  # ROOT doesn't differentiate between different types
63  # of white space. Therefore, we change all of these
64  # entries to 'empty'. Also, avoiding any lines that begin
65  # with '#'
66  file_lines = ['\t'.join([val if (val.find(' ') == -1 and val != '')
67  else 'empty' for val in line.split('\t')])
68  for line in file_lines[1:] if line[0] != '#' ]
69 
70  # Removing NaN, setting these entries to 0.0.
71  # Also joining the list of strings into one large string.
72  file_as_string = ('\n'.join(file_lines)).replace('NaN', str(0.0))
73  #print file_as_string
74 
75  # creating an istringstream to pass into ReadStream
76  istring = ROOT.istringstream(file_as_string)
77 
78  # Now read the stream
79  output_tree.ReadStream(istring, branch_descriptor)
80 
81  output_file.cd()
82  output_tree.Write()
83 
84 
85 if __name__ == '__main__':
86  if len(sys.argv) < 2:
87  print "Usage: %s file_to_parse.dat" % sys.argv[0]
88  sys.exit(1)
89  parse_CSV_file_with_TTree_ReadStream("example_tree", sys.argv[1])
90 
Author
Michael Marino

Definition in file parse_CSV_file_with_TTree_ReadStream.py.