// %sample:codesample1:cpp%
#include "xtcdata/xtc/ShapesData.hh"
#include "xtcdata/xtc/DescData.hh"
#include "xtcdata/xtc/Dgram.hh"
#include "xtcdata/xtc/TypeId.hh"
#include "xtcdata/xtc/XtcIterator.hh"
#include "xtcdata/xtc/VarDef.hh"
// various std library includes omitted...
using namespace XtcData;
#define BUFSIZE 0x4000000
HSDXtcWriter::HSDXtcWriter()
{
// Define the "signature" of the data in
// our feature extraction data set
class FexDef:public VarDef
{
public:
enum index
{
floatFex,
arrayFex,
intFex
};
FexDef()
{
NameVec.push_back({"floatFex",Name::DOUBLE});
// "arrayFex" has rank 2; 0 is the default, for scalar data
NameVec.push_back({"arrayFex",Name::FLOAT,2});
NameVec.push_back({"intFex",Name::INT64});
}
};
// Define some instance variables.
FILE *this->xtcoutfile;
// Declare an Xtc::Dgram to store metadata about the output
Dgram& this->metadataDgram;
// Store the node ID of this node
unsigned this->nodeid1 = 1;
// Declare an Xtc:Dgram to store readout data
Dgram& this->readoutDgram;
std::vector<NameIndex>& this->namesVec;
this->FexDef = new FexDef();
// Now initialize various things
_initializeMetadataDgram();
}
void HSDXtcWriter::_initializeMetadataDgram()
{
TypeId tid(TypeId::Parent, 0);
void* configbuf = malloc(BUFSIZE);
this->metadataDgram = *(Dgram*)configbuf;
this->metadataDgram.xtc.contains = tid;
this->metadataDgram.xtc.damage = 0;
this->metadataDgram.xtc.extent = sizeof(Xtc);
}
void HSDXtcWriter::_initializeReadoutDataDgram()
{
TypeId tid(TypeId::Parent, 0);
void* buf = malloc(BUFSIZE);
this->readoutDgram = *(Dgram*)buf;
this->readoutDgram.xtc.contains = tid;
this->readoutDgram.xtc.damage = 0;
this->readoutDgram.xtc.extent = sizeof(Xtc);
}
void HSDXtcWriter::_addNames(Xtc& parent, std::vector<NameIndex>& aNamesVec)
{
// Instantiate an Alg to define metadata for
// a feature extraction ("fex") algorithm,
// version 4.5.6
Alg hsdFexAlg("fex",4,5,6);
// Create a Names structure that identifies
// the location, algorithm, detector type
// and detectorID.
Names& fexNames = *new(parent) Names("xpphsd", hsdFexAlg, "hsd", "detnum1234")
// And now we add the Names structure to the
// parent Xtc metadata container along with
// the FexDef data signature.
fexNames.add(parent, this->FexDef);
// add this Names structure to the vector
aNamesVec.push_back(NameIndex(this->fexNames));
}
void HSDXtcWriter::setup()
{
// Add a Names structure to the configuration Dgram
_addNames(this->metadataDgram.xtc, this->namesVec);
}
void HSDXtcWriter::openXtcFile(xtcoutfilename)
{
this->xtcoutfile = fopen(xtcoutfilename, "w");
}
void HSDXtcWriter::writeFeature()
{
// Need some makebelieve code here to show how this
// callback method unpacks data from DAQ
// to cram into these Xtc structures...
// Talk to Chris. Is this event by event? Would you want
// to destruct this CreateData after each event?
CreateData fex(parent, NamesVec, nameId);
// Need stub code here to represent getting values from DAQ
// and using set_value and allocate with something real-ish
// set_value() lets you set scalar values in the data record.
// The 'floatFex' ID is defined in our custom FexDef class.
fex.set_value(FexDef::floatFex, (double)41.0);
// Use allocate() to set up data that's better represented as
// vectors or matrices.
// The 'arrayFex' ID is defined in our custom FexDef class.
unsigned shape[MaxRank] = {2,3}; //MaxRank is an Xtc library
//global upper limit on data complexity.
Array<float> arrayT = fex.allocate<float>(this->FexDef::arrayFex, shape);
for(unsigned i=0; i<shape[0]; i++){
for (unsigned j=0; j<shape[1]; j++) {
arrayT(i,j) = 142.0+i*shape[1]+j;
}
};
// Another scalar value setter.
// The 'intFex' ID is defined in our custom FexDef class.
fex.set_value(FexDef::intFex, (int64_t) 42);
}
void HSDXtcWriter::closeXtcFile()
{
fclose(this->xtcoutfile);
}
// ...
// %endsample% |