a mockup PSI Run Header without custom class objects in the root file

This commit is contained in:
nemu 2011-07-05 15:58:45 +00:00
parent a9ceae63a0
commit 2951d2fad2
3 changed files with 162 additions and 359 deletions

View File

@ -59,7 +59,7 @@ ClassImp(TPsiRunProperty)
*/
TPsiRunProperty::TPsiRunProperty()
{
fName = "n/a";
fLabel = "n/a";
fValue = 0.0;
fError = 0.0;
fUnit = "n/a";
@ -76,23 +76,8 @@ TPsiRunProperty::TPsiRunProperty()
* \param error
* \param unit
*/
TPsiRunProperty::TPsiRunProperty(TObjString &name, Double_t value, Double_t error, TObjString &unit) :
fName(name), fValue(value), fError(error), fUnit(unit)
{
}
//--------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------
/**
* <p>Constructor.
*
* \param name
* \param value
* \param error
* \param unit
*/
TPsiRunProperty::TPsiRunProperty(TString &name, Double_t value, Double_t error, TString &unit) : fName(name), fValue(value), fError(error), fUnit(unit)
TPsiRunProperty::TPsiRunProperty(TString &label, Double_t value, Double_t error, TString &unit) :
fLabel(label), fValue(value), fError(error), fUnit(unit)
{
}
@ -118,16 +103,16 @@ ClassImp(TPsiRunHeader)
*/
TPsiRunHeader::TPsiRunHeader()
{
fVersion.SetString("00 - version: $Id$");
fRunTitle.SetString("01 - run title: n/a");
fRunNumber.SetString("02 - run number: -1");
fLaboratory.SetString("03 - laboratory: n/a");
fInstrument.SetString("04 - instrument: n/a");
fSetup.SetString("05 - setup: n/a");
fSample.SetString("06 - sample: n/a");
fOrientation.SetString("07 - orientation: n/a");
fPropertiesList.Expand(0); // init to size 0
fProperties.Expand(0); // init to size 0
fVersion = TString("$Id$");
fRunTitle = TString("n/a");
fRunNumber = -1;
fLaboratory = TString("n/a");
fInstrument = TString("n/a");
fSetup = TString("n/a");
fSample = TString("n/a");
fOrientation = TString("n/a");
fHeader.Expand(0); // init to size 0
}
//--------------------------------------------------------------------------
@ -138,238 +123,83 @@ TPsiRunHeader::TPsiRunHeader()
*/
TPsiRunHeader::~TPsiRunHeader()
{
fPropertiesList.Delete();
fProperties.Delete();
fProperties.clear();
fHeader.Delete();
}
//--------------------------------------------------------------------------
// GetVersion (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetVersion() const
{
TString str("??");
Int_t idx = fVersion.GetString().First(":");
if ((idx < 0) || (idx+2 > fVersion.GetString().Length()))
return str;
str = fVersion.GetString();
str.Replace(0, idx+2, 0, 0);
return str;
}
//--------------------------------------------------------------------------
// GetRunTitle (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetRunTitle() const
{
return GetInfoString(fRunTitle);
}
//--------------------------------------------------------------------------
// SetRunTitle (public)
// GetHeader (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param runTitle
*/
void TPsiRunHeader::SetRunTitle(TString runTitle)
TObjArray* TPsiRunHeader::GetHeader()
{
char str[1024];
sprintf(str, "%02d - run title: %s", TPRH_RUN_TITLE_IDX, runTitle.Data());
// make sure that previous header is removed
fHeader.Expand(0);
fRunTitle.SetString(str);
}
char str[1024], fmt[1024];
TObjString *tostr;
//--------------------------------------------------------------------------
// GetRunNumber (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
Int_t TPsiRunHeader::GetRunNumber() const
{
TObjArray *tokens = 0;
TObjString *ostr = 0;
TString str("");
Int_t ival=-999;
// add version
sprintf(str, "%02d - Version: %s", TPRH_VERSION_IDX+1, fVersion.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
tokens = fRunNumber.GetString().Tokenize(":");
if (!tokens) { // couldn't get tokens
return ival;
}
if (tokens->GetEntries() != 2) { // wrong number of tokens
delete tokens;
return ival;
// add run title
sprintf(str, "%02d - Run Title: %s", TPRH_RUN_TITLE_IDX+1, fRunTitle.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add run number
sprintf(str, "%02d - Run Number: %d", TPRH_RUN_NUMBER_IDX+1, fRunNumber);
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add laboratory
sprintf(str, "%02d - Laboratory: %s", TPRH_LABORATORY_IDX+1, fLaboratory.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add instrument
sprintf(str, "%02d - Instrument: %s", TPRH_INSTRUMENT_IDX+1, fInstrument.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add setup
sprintf(str, "%02d - Setup: %s", TPRH_SETUP_IDX+1, fSetup.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add sample
sprintf(str, "%02d - Sample: %s", TPRH_SAMPLE_IDX+1, fSample.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add orientation
sprintf(str, "%02d - Orientation: %s", TPRH_ORIENTATION_IDX+1, fOrientation.Data());
tostr = new TObjString(str);
fHeader.AddLast(tostr);
// add properties
UInt_t digit=0;
for (UInt_t i=0; i<fProperties.size(); i++) {
digit = GetDecimalPlace(fProperties[i].GetError());
if (fProperties[i].GetUnit().CompareTo("n/a", TString::kIgnoreCase)) {
sprintf(fmt, "%%02d - %%s: %%.%dlf +- (%%.%dlf) %%s", digit, digit);
sprintf(str, fmt, TPRH_OFFSET+i, fProperties[i].GetLabel().Data(), fProperties[i].GetValue(), fProperties[i].GetError(), fProperties[i].GetUnit().Data());
} else {
sprintf(fmt, "%%02d - %%s: %%.%dlf +- (%%.%dlf)", digit, digit);
sprintf(str, fmt, TPRH_OFFSET+i, fProperties[i].GetLabel().Data(), fProperties[i].GetValue(), fProperties[i].GetError());
}
tostr = new TObjString(str);
fHeader.AddLast(tostr);
}
ostr = dynamic_cast<TObjString*>(tokens->At(1));
str = ostr->GetString();
if (str.IsDigit())
ival = str.Atoi();
fHeader.SetName("RunHeader");
delete tokens;
return ival;
}
//--------------------------------------------------------------------------
// SetRunNumber (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param runNumber
*/
void TPsiRunHeader::SetRunNumber(Int_t runNumber)
{
char str[1024];
sprintf(str, "%02d - run number: %06d", TPRH_RUN_NUMBER_IDX, runNumber);
fRunNumber.SetString(str);
}
//--------------------------------------------------------------------------
// GetLab (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetLab() const
{
return GetInfoString(fLaboratory);
}
//--------------------------------------------------------------------------
// SetLab (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param lab
*/
void TPsiRunHeader::SetLab(TString lab)
{
char str[1024];
sprintf(str, "%02d - laboratory: %s", TPRH_LABORATORY_IDX, lab.Data());
fLaboratory.SetString(str);
}
//--------------------------------------------------------------------------
// GetInstrument (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetInstrument() const
{
return GetInfoString(fInstrument);
}
//--------------------------------------------------------------------------
// SetInstrument (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param instrument
*/
void TPsiRunHeader::SetInstrument(TString instrument)
{
char str[1024];
sprintf(str, "%02d - instrument: %s", TPRH_INSTRUMENT_IDX, instrument.Data());
fInstrument.SetString(str);
}
//--------------------------------------------------------------------------
// GetSetup (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetSetup() const
{
return GetInfoString(fSetup);
}
//--------------------------------------------------------------------------
// SetSetup (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param setup
*/
void TPsiRunHeader::SetSetup(TString setup)
{
char str[1024];
sprintf(str, "%02d - setup: %s", TPRH_SETUP_IDX, setup.Data());
fSetup.SetString(str);
}
//--------------------------------------------------------------------------
// GetSample (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetSample() const
{
return GetInfoString(fSample);
}
//--------------------------------------------------------------------------
// SetSample (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param sample
*/
void TPsiRunHeader::SetSample(TString sample)
{
char str[1024];
sprintf(str, "%02d - sample: %s", TPRH_SAMPLE_IDX, sample.Data());
fSample.SetString(str);
}
//--------------------------------------------------------------------------
// GetOrientation (public)
//--------------------------------------------------------------------------
/**
* <p>
*/
TString TPsiRunHeader::GetOrientation() const
{
return GetInfoString(fOrientation);
}
//--------------------------------------------------------------------------
// SetOrientation (public)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param orientation
*/
void TPsiRunHeader::SetOrientation(TString orientation)
{
char str[1024];
sprintf(str, "%02d - orientation: %s", TPRH_ORIENTATION_IDX, orientation.Data());
fOrientation.SetString(str);
return &fHeader;
}
//--------------------------------------------------------------------------
@ -382,15 +212,7 @@ void TPsiRunHeader::SetOrientation(TString orientation)
*/
void TPsiRunHeader::AddProperty(TPsiRunProperty &property)
{
TPsiRunProperty *prop = (TPsiRunProperty*) property.Clone();
fProperties.AddLast(prop);
char str[1024];
TObjString *tostr;
sprintf(str, "%02d - %s: %lf +- %lf (%s)", TPRH_OFFSET+fPropertiesList.GetLast(),
prop->GetName().GetString().Data(), prop->GetValue(), prop->GetError(), prop->GetUnit().GetString().Data());
tostr = new TObjString(str);
fPropertiesList.AddLast(tostr);
fProperties.push_back(property);
}
//--------------------------------------------------------------------------
@ -406,17 +228,8 @@ void TPsiRunHeader::AddProperty(TPsiRunProperty &property)
*/
void TPsiRunHeader::AddProperty(TString name, Double_t value, Double_t error, TString unit)
{
TPsiRunProperty prop(name, value, error, unit);
TPsiRunProperty *p_prop = (TPsiRunProperty*) prop.Clone();
fProperties.AddLast(p_prop);
char str[1024];
TObjString *tostr;
sprintf(str, "%02d - %s: %lf +- %lf (%s)", TPRH_OFFSET+fPropertiesList.GetLast(),
name.Data(), value, error, unit.Data());
tostr = new TObjString(str);
fPropertiesList.AddLast(tostr);
TPsiRunProperty property(name, value, error, unit);
fProperties.push_back(property);
}
//--------------------------------------------------------------------------
@ -428,46 +241,43 @@ void TPsiRunHeader::AddProperty(TString name, Double_t value, Double_t error, TS
void TPsiRunHeader::DumpHeader() const
{
int old_width = cout.width();
TPsiRunProperty *prop;
// get maximal length of the property names
int name_width = 8; // init to max. length of fixed names like: version, etc.
for (Int_t i=0; i<=fProperties.GetLast(); i++) {
prop = (TPsiRunProperty*)(fProperties[i]);
if (prop->GetName().GetString().Length() > name_width)
name_width = prop->GetName().GetString().Length();
for (UInt_t i=0; i<fProperties.size(); i++) {
if (fProperties[i].GetLabel().Length() > name_width)
name_width = fProperties[i].GetLabel().Length();
}
name_width++;
// write SVN versions
cout << endl << setw(name_width) << left << "version" << setw(old_width) << ": " << GetVersion().Data();
cout << endl << setw(name_width) << left << "Version" << setw(old_width) << ": " << GetVersion().Data();
// write run title
cout << endl << setw(name_width) << left << "run title" << setw(old_width) << ": " << GetRunTitle().Data();
cout << endl << setw(name_width) << left << "Run Title" << setw(old_width) << ": " << GetRunTitle().Data();
// write run number
cout << endl << setw(name_width) << left << "run number" << setw(old_width) << ": " << GetRunNumber();
cout << endl << setw(name_width) << left << "Run Number" << setw(old_width) << ": " << GetRunNumber();
// write laboratory
cout << endl << setw(name_width) << left << "laboratory" << setw(old_width) << ": " << GetLab().Data();
cout << endl << setw(name_width) << left << "Laboratory" << setw(old_width) << ": " << GetLab().Data();
// write instrument
cout << endl << setw(name_width) << left << "instrument" << setw(old_width) << ": " << GetInstrument().Data();
cout << endl << setw(name_width) << left << "Instrument" << setw(old_width) << ": " << GetInstrument().Data();
// write setup
cout << endl << setw(name_width) << left << "setup" << setw(old_width) << ": " << GetSetup().Data();
cout << endl << setw(name_width) << left << "Setup" << setw(old_width) << ": " << GetSetup().Data();
// write sample
cout << endl << setw(name_width) << left << "sample" << setw(old_width) << ": " << GetSample().Data();
cout << endl << setw(name_width) << left << "Sample" << setw(old_width) << ": " << GetSample().Data();
// write orientation
cout << endl << setw(name_width) << left << "orientation" << setw(old_width) << ": " << GetOrientation().Data();
cout << endl << setw(name_width) << left << "Orientation" << setw(old_width) << ": " << GetOrientation().Data();
for (Int_t i=0; i<=fProperties.GetLast(); i++) {
prop = (TPsiRunProperty*)(fProperties[i]);
cout << endl << setw(name_width) << left << prop->GetName().GetString().Data() << setw(old_width) << ": " << prop->GetValue() << " +- " << prop->GetError();
if (prop->GetUnit().GetString().CompareTo("n/a", TString::kIgnoreCase)) {
cout << " " << prop->GetUnit().GetString().Data();
for (UInt_t i=0; i<fProperties.size(); i++) {
cout << endl << setw(name_width) << left << fProperties[i].GetLabel().Data() << setw(old_width) << ": " << fProperties[i].GetValue() << " +- " << fProperties[i].GetError();
if (fProperties[i].GetUnit().CompareTo("n/a", TString::kIgnoreCase)) {
cout << " " << fProperties[i].GetUnit().Data();
}
}
cout << endl << endl;
@ -497,33 +307,27 @@ void TPsiRunHeader::DrawHeader() const
}
//--------------------------------------------------------------------------
// GetInfoString (private)
// GetDecimalPlace (private)
//--------------------------------------------------------------------------
/**
* <p>
* <p>Check decimal place of val. If val > 1.0, the function will return 0, otherwise
* the first decimal place found will be returned.
*
* \param tostr a TObjString with a content structure "%s: %s".
* \param val value from which the first significant digit shall be determined
*/
TString TPsiRunHeader::GetInfoString(const TObjString &tostr) const
UInt_t TPsiRunHeader::GetDecimalPlace(Double_t val)
{
TObjArray *tokens = 0;
TObjString *ostr = 0;
TString str("??");
UInt_t digit = 0;
tokens = tostr.GetString().Tokenize(":");
if (!tokens) { // couldn't get tokens
return str;
}
if (tokens->GetEntries() != 2) { // wrong number of tokens
delete tokens;
return str;
if (val < 1.0) {
UInt_t count=1;
do {
val *= 10.0;
if (val > 1.0)
digit = count;
count++;
} while ((digit == 0) || (count > 20));
}
ostr = dynamic_cast<TObjString*>(tokens->At(1));
str = ostr->GetString();
str.Remove(TString::kLeading, ' ');
delete tokens;
return str;
return digit;
}

View File

@ -40,29 +40,26 @@ class TPsiRunProperty : public TObject
{
public:
TPsiRunProperty();
TPsiRunProperty(TObjString &name, Double_t value, Double_t error, TObjString &unit);
TPsiRunProperty(TString &name, Double_t value, Double_t error, TString &unit);
virtual ~TPsiRunProperty();
virtual TObjString GetName() { return fName; }
virtual Double_t GetValue() { return fValue; }
virtual Double_t GetError() { return fError; }
virtual TObjString GetUnit() { return fUnit; }
virtual TString GetLabel() const { return fLabel; }
virtual Double_t GetValue() const { return fValue; }
virtual Double_t GetError() const { return fError; }
virtual TString GetUnit() const { return fUnit; }
virtual void SetName(TObjString &name) { fName = name; }
virtual void SetName(TString &name) { fName = name.Data(); }
virtual void SetName(const char *name) { fName = name; }
virtual void SetLabel(TString &label) { fLabel = label; }
virtual void SetLabel(const char *label) { fLabel = label; }
virtual void SetValue(Double_t val) { fValue = val; }
virtual void SetError(Double_t err) { fError = err; }
virtual void SetUnit(TObjString &unit) { fUnit = unit; }
virtual void SetUnit(TString &unit) { fUnit = unit.Data(); }
virtual void SetUnit(const char *unit) { fUnit = unit; }
private:
TObjString fName;
Double_t fValue;
Double_t fError;
TObjString fUnit;
TString fLabel;
Double_t fValue;
Double_t fError;
TString fUnit;
ClassDef(TPsiRunProperty, 1)
};
@ -73,42 +70,46 @@ public:
TPsiRunHeader();
virtual ~TPsiRunHeader();
virtual TString GetVersion() const;
virtual TString GetRunTitle() const;
virtual Int_t GetRunNumber() const;
virtual TString GetLab() const;
virtual TString GetInstrument() const;
virtual TString GetSetup() const;
virtual TString GetSample() const;
virtual TString GetOrientation() const;
virtual TObjArray *GetProperties() { return &fProperties; }
virtual TString GetVersion() const { return fVersion; }
virtual TString GetRunTitle() const { return fRunTitle; }
virtual Int_t GetRunNumber() const { return fRunNumber; }
virtual TString GetLab() const { return fLaboratory; }
virtual TString GetInstrument() const { return fInstrument; }
virtual TString GetSetup() const { return fSetup; }
virtual TString GetSample() const { return fSample; }
virtual TString GetOrientation() const { return fOrientation; }
virtual vector<TPsiRunProperty> *GetProperties() { return &fProperties; }
virtual void SetRunTitle(TString runTitle);
virtual void SetRunNumber(Int_t runNumber);
virtual void SetLab(TString lab);
virtual void SetInstrument(TString insturment);
virtual void SetSetup(TString setup);
virtual void SetSample(TString sample);
virtual void SetOrientation(TString setup);
virtual TObjArray *GetHeader();
virtual void SetRunTitle(TString runTitle) { fRunTitle = runTitle; }
virtual void SetRunNumber(Int_t runNumber) { fRunNumber = runNumber; }
virtual void SetLab(TString lab) { fLaboratory = lab; }
virtual void SetInstrument(TString insturment) { fInstrument = insturment; }
virtual void SetSetup(TString setup) { fSetup = setup; }
virtual void SetSample(TString sample) { fSample = sample; }
virtual void SetOrientation(TString orientation) { fOrientation = orientation; }
virtual void AddProperty(TPsiRunProperty &property);
virtual void AddProperty(TString name, Double_t value, Double_t error, TString unit);
virtual void DumpHeader() const;
virtual void DrawHeader() const;
private:
TObjString fVersion; /// SVN version of the TPsiRunHeader
TObjString fRunTitle; /// run title
TObjString fRunNumber; /// run number
TObjString fLaboratory; /// laboratory: PSI
TObjString fInstrument; /// instrument name like: GPS, LEM, ....
TObjString fSetup; /// setup
TObjString fSample; /// sample name
TObjString fOrientation; /// sample orientation
TObjArray fPropertiesList;
TObjArray fProperties;
TString fVersion; /// SVN version of the TPsiRunHeader
TString fRunTitle; /// run title
Int_t fRunNumber; /// run number
TString fLaboratory; /// laboratory: PSI
TString fInstrument; /// instrument name like: GPS, LEM, ....
TString fSetup; /// setup
TString fSample; /// sample name
TString fOrientation; /// sample orientation
vector<TPsiRunProperty> fProperties;
virtual TString GetInfoString(const TObjString &tostr) const;
TObjArray fHeader; /// header as TObjString array for dumping into a ROOT file
UInt_t GetDecimalPlace(Double_t val);
ClassDef(TPsiRunHeader, 1)
};

View File

@ -52,21 +52,10 @@ int main(int argc, char *argv[])
return 1;
}
TFile *f = new TFile(argv[1], "RECREATE", "psi_runHeader_test");
if (f->IsZombie()) {
delete f;
return -1;
}
// root file header related things
TFolder *runInfo = gROOT->GetRootFolder()->AddFolder("RunInfo", "PSI RunInfo");
gROOT->GetListOfBrowsables()->Add(runInfo, "RunInfo");
// PSI Run Header object
TPsiRunHeader *header = new TPsiRunHeader();
runInfo->Add(header); // add header to RunInfo folder
header->SetRunTitle("This is a run title");
header->SetRunTitle("This is a run title");
header->SetRunNumber(12345);
header->SetLab("PSI");
header->SetInstrument("LEM");
@ -76,12 +65,21 @@ int main(int argc, char *argv[])
header->AddProperty("T0", 30.01, 0.05, "K");
header->AddProperty("T1", 30.03, 0.03, "K");
header->AddProperty("Field", 3.03, 0.03, "T");
header->AddProperty("Field", 3.00003, 0.0003, "T");
header->AddProperty("BigError", 13.2, 1.2, "Something");
header->AddProperty("ThisIsAVeryLongPropertyName", 3.03, 0.03, "SI-Unit");
header->DumpHeader();
runInfo->Write();
TFile *f = new TFile(argv[1], "RECREATE", "psi_runHeader_test");
if (f->IsZombie()) {
delete f;
return -1;
}
f->mkdir("RunHeader");
f->cd("RunHeader");
header->GetHeader()->Write();
f->Close();