Using FrameCPP
Short Tutorial
Reference to using the FrameCPP library by Ian Taylor
Reading Frames
This example reads a frame file and prints out the instrument name, run number and frame number for every frame in the file.
#include <framecpp/framereader.hh>
#include <framecpp/frame.hh>
#include <fstream.h>
using namespace FrameCPP;
main()
{
// Open the frame file and initialize the FrameReader
ifstream in( "frame.dat" );
FrameReader reader( in );
// Read a frame
Frame* f = reader.readFrame();
// Loop if a frame was read
while( f != 0 )
{
// Print some information about the frame
cout << f->getName() << " " << f->getRun() << " " << f->getFrame()
<< endl;
// Delete the old frame and read another
delete f;
f = reader.readFrame();
}
// Close the frame file
in.close();
}
Building Frames
This functions creates a simple frame object in memory.
Frame* makeFrame()
{
// Create top-level object -- new Frame
Frame* fr = new Frame(
"LIGO", 10, 1, Time( 100, 1000 ), 20, 2000, 1.5 );
// Add a Detector structure to the Frame
FrameCPP::Detector d(
"LIGO_1", Location( 1, 2, 3.44, 5, 6, 7.8 ), 15.0,
1.75, 3.56 );
fr->setDetectSim( d );
// Add Raw Data structure to the Frame
FrameCPP::RawData rawData( "RD1" );
fr->setRawData( rawData );
// Add one ADC channel to the Frame
FrameCPP::AdcData a( "CH2", 12, 37, 16, 3.8e3 );
fr->getRawData()->refAdc().append( a );
// Return a pointer to new Frame
return fr;
}
Writing Frames
Following example function writes frame to disk (creates frame file) with the
added table of contents. Same frame is written twice. This creates a frame files
with two frame in it. Frames have the same data in this example case.
Second frame is actually added or appended to the existsing frame file.
This action demonstrates the use of the FrameAppendWriterTOC class.
bool readTest()
{
Frame* frame = makeFrame();
// Write first frame
{
ofstream out( "io.test.dat" );
FrameWriterTOC fw( out );
fw.writeFrame( *frame );
fw.close();
out.close();
}
// append second frame
try {
fstream in( "io.test.dat", ios::in | ios::out );
FrameAppendWriterTOC fw(in);
fw.writeFrame( *frame );
fw.close();
} catch (read_failure& e) {
cerr << "appender TOC read failure" << endl;
cerr << e.what() << endl;
exit(1);
} catch (write_failure) {
cerr << "appender write failure" << endl;
exit(1);
}
delete frame;
}
Accessing Data
This example program demonstrates the use of ADC data searching.
ADC channel(s) could be found using just the name or a regular expression,
or a hash search by name.
#include <framecpp/framereader.hh>
#include <framecpp/frame.hh>
#include <fstream.h>
using FrameCPP::Frame;
using FrameCPP::RawData;
using FrameCPP::AdcData;
using FrameCPP::FrameReader;
main(int argc, char *argv[])
{
ifstream in( argv[1] );
FrameReader r( in );
Frame* f( r.readFrame() );
RawData* raw = f->getRawData();
char type;
char q[ 500 ];
RawData::adcData_iterator iter( raw->refAdc().begin() );
RawData::AdcDataContainer& adc( raw->refAdc() );
do
{
for (int i=0;i < raw->refAdc().getSize(); ++i )
{
cout << raw->refAdc()[ i ]->getName() << " ";
}
cout << "\n1: normal\n2: next normal\n3: regex\n4: next regex\n5: hash\n6: Quit\n\nType?";
cin >> type;
if ( type != '6' )
{
cout << "Query?";
cin >> q;
}
adc.rehash();
switch( type )
{
case '1':
iter = adc.find( q, adc.begin() );
if ( iter != adc.end() )
cout << (*iter)->getName() << endl;
else
cout << "NOT FOUND" << endl;
break;
case '2':
if ( iter == adc.end() )
{
cout << "NOT FOUND" << endl;
break;
}
iter = adc.find( q, ++iter );
if ( iter != adc.end() )
cout << (*iter)->getName() << endl;
else
cout << "NOT FOUND" << endl;
break;
case '3':
iter = adc.regexFind( q, adc.begin() );
if ( iter != adc.end() )
cout << (*iter)->getName() << endl;
else
cout << "NOT FOUND" << endl;
break;
case '4':
if ( iter == adc.end() )
{
cout << "NOT FOUND" << endl;
break;
}
iter = adc.regexFind( q, ++iter );
if ( iter != adc.end() )
cout << (*iter)->getName() << endl;
else
cout << "NOT FOUND" << endl;
break;
case '5':
pair< RawData::adcData_hash_iterator,
RawData::adcData_hash_iterator > p( adc.hashFind( q ) );
for ( RawData::adcData_hash_iterator hiter = p.first;
hiter != p.second; ++hiter )
{
cout << hiter->second->getName() << endl;
}
if ( p.first == p.second )
{
cout << "NOT FOUND" << endl;
}
break;
}
cout << endl;
}
while( type != '6' );
in.close();
}
Wed Aug 1 10:02:19 PDT 2001