MutCalibStrip.cc
//
// MutCalibStrip.cc - calibration data class to retrieve from the database,
// store, and provide gain and pedestal calibration data.
//
#include "PdbCalBank.hh"
#include "PdbObjyBankManager.hh"
#include "PdbApplication.hh"
#include <fstream.h>
#include "MutCalibStrip.hh"
// Constructor
MutCalibStrip::MutCalibStrip()
{
s.clear(); // just clear the set, it's now an empty one
}
// Destructor
MutCalibStrip::~MutCalibStrip()
{
}
// get info from Objectivity database
int
MutCalibStrip::dbGetAll(PHTimeStamp tsearch)
{
// Access the database, pull data into the internal structure.
PdbBankManager *bankManager = PdbObjyBankManager::instance();
PdbApplication *application = bankManager->getApplication();
PHString calibname="calib.mut.strip";
PHString classname="PdbMutCalibStripBank";
PdbBankID bankid("mutcalibstrip");
bankid.setInternalValue(2);
// Open the Objectivity database for reading and pull all the values for
// a given bank (i.e. a strip) and put it into our set
if ( application->startRead() )
{
PdbCalBank *mutBank =
bankManager->fetchBank(classname.getString(),
bankid, calibname.getString(), tsearch);
if (mutBank)
{
mutBank->print();
int length = mutBank->getLength();
#ifdef DEBUG
cout << PHWHERE << " - reading from Objy DB "
<< endl << "First 10 entries will be printed " << endl;
#endif
for (int i=0; i<length; i++)
{
p = (PdbMutCalibStrip&)(mutBank->getEntry(i));
#ifdef DEBUG
// echo first ten entries
if (i < 10)
{
p.print();
}
#endif
// add the read strip to the set
// if some other strip, with the same indices are already
// in the set, a warning will be issued
iter = s.find(p);
if ( iter == s.end() )
{ // no Doppelganger, insert this one
s.insert(p);
}
else
{
cout << PHWHERE << " - insert WARNING " << endl
<< "A strip with the same indices has already "
<< "been inserted" << endl;
p.print();
}
}
}
else
{
cerr << PHWHERE <<"\tError:"
<<"\tbankManager returned zero-pointer"<<endl;
}
application->commit();
}
else
{
application->abort();
cerr << PHWHERE << "Transaction aborted." <<endl;
}
}
// method for putting stuff into the database also
int
MutCalibStrip::dbPutAll( PHTimeStamp start,
PHTimeStamp stop,
PHString descriptor)
{
// do we have something to put into the database?
int length = s.size();
if (length < 1)
{
cerr << PHWHERE << " Nothing to put into DB " << endl;
cerr << "length = " << length << endl;
return -1;
}
//timestamp values: sanity check
if (start>stop)
{
cerr << PHWHERE << "invalid start and stop:" << start << stop << endl;
cerr << "ignored" << endl;
return -1;
}
PdbBankManager *bankManager = PdbObjyBankManager::instance();
PdbApplication *application = bankManager->getApplication();
PHString calibname="calib.mut.strip";
PHString classname="PdbMutCalibStripBank";
PdbBankID bankid("mutcalibstrip");
bankid.setInternalValue(2);
#ifdef DEBUG
cout << PHWHERE << "Opening FD in update mode.." << endl;
#endif
if (application->startUpdate())
{
PdbCalBank *prevBank =
bankManager->fetchBank(classname.getString(),
bankid,calibname.getString(),start);
if ( prevBank )
{
cout << PHWHERE << " overlapping bank found."
<< " Change the EndValTime of it " <<endl;
prevBank->setEndValTime(start);
}
PdbCalBank *mutBank =
bankManager->createBank( classname.getString(),
bankid, descriptor.getString(),
start, stop, calibname.getString());
cout << PHWHERE << "new bank created" << endl;
mutBank->setLength(length);
mutBank->print();
PdbMutCalibStrip *ppointer;
int i = 0;
for(iter = s.begin(); iter != s.end(); iter++)
{
ppointer = (PdbMutCalibStrip*)&(mutBank->getEntry(i));
*ppointer = (*iter);
i++;
}
application->commit();
}
else
{
cerr << PHWHERE << "failed to start application for update" << endl;
}
}
// get all the gain and pedestal data
// from a text file into the set
int
MutCalibStrip::txtGetAll(char* infile)
{
ifstream fin(infile);
if (!fin)
{
cerr << PHWHERE << " - can't open " << infile
<< "for input" << endl;
return 1;
}
//setup
s.clear();
iter = s.begin();
// read from the file as long as we can
// add strips to set as we go along
int nlines = 0;
#ifdef DEBUG
cout << PHWHERE << " - reading from " << infile
<< endl << "First 10 lines on file will be printed " << endl;
#endif
while ( fin.good() )
{
p.read(fin);
if (! fin.good() )
{
break;
}
#ifdef DEBUG
// echo first ten lines of file
if (nlines < 10)
{
p.print();
}
#endif
// add the read strip to the set
// if some other strip, with the same indices are already
// in the set, a warning will be issued
iter = s.find(p);
if ( iter == s.end() )
{ // no Doppelganger, insert this one
s.insert(p);
}
else
{
cout << PHWHERE << " - insert WARNING " << endl
<< "A strip with the same indices has already "
<< "been inserted" << endl;
p.print();
}
nlines++;
}
fin.close();
#ifdef DEBUG
cout << PHWHERE << " size of set: " << s.size() << endl
<< "First 10 lines in set will be printed " << endl;
// print the first ten members again
nlines = 0;
for(iter = s.begin(); iter != s.end(); iter++)
{
if (nlines<10)
{
(*iter).print();
}
nlines++;
}
#endif
return 0;
}
// put all the gain and pedestal data
// from the set into a text file.
int
MutCalibStrip::txtPutAll(char* outfile)
{
ofstream fout(outfile);
if (!fout)
{
cerr << PHWHERE << " - can't open " << outfile
<< "for output" << endl;
return 1;
}
// go thru the set and print all to fout
#ifdef DEBUG
cout << PHWHERE << " - writing to " << outfile
<< endl << "size of set: " << s.size() << endl;
#endif
for(iter = s.begin(); iter != s.end(); iter++)
{
(*iter).write(fout);
}
fout.close();
return 0;
}
PdbMutCalibStrip*
MutCalibStrip::getPdbMutCalibStrip(const int arm, const int station,
const int octant, const int halfoctant,
const int gap, const int plane,
const int strip)
{
// find object in the set and return pointer to it (NULL, if not found)
p.set_indices(arm,station,octant,halfoctant,gap,plane,strip);
iter = s.find(p);
if ( iter == s.end() )
{
cout << PHWHERE << " - no such strip stored "
<< endl;
return NULL;
}
// else: we did find the strip we were looking for. Return ref. to it.
p = *iter;
return &p;
}
void
MutCalibStrip::putPdbMutCalibStrip(const PdbMutCalibStrip *newp)
{
p = *newp;
// erase object with the same indices, if it is there
// and then insert the new strip
s.erase(p);
s.insert(p);
}