From b66ac0da54ff7f92c7479ae93a2dc1e93ca680bf Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Mon, 29 Sep 2025 10:47:58 +0200 Subject: [PATCH] added the reader part for MusrRoot if written with TDirectory rather than TFolder. --- src/external/MusrRoot/TMusrRunHeader.cpp | 421 +++++++++++++---------- src/external/MusrRoot/TMusrRunHeader.h | 1 + 2 files changed, 235 insertions(+), 187 deletions(-) diff --git a/src/external/MusrRoot/TMusrRunHeader.cpp b/src/external/MusrRoot/TMusrRunHeader.cpp index 2c9728d04..8ecb661ff 100644 --- a/src/external/MusrRoot/TMusrRunHeader.cpp +++ b/src/external/MusrRoot/TMusrRunHeader.cpp @@ -42,6 +42,7 @@ #include #include #include +#include ClassImp(TMusrRunPhysicalQuantity) @@ -447,15 +448,14 @@ Bool_t TMusrRunHeader::FillDirectory(TDirectory *dir) p_ostr = dynamic_cast(oarray->At(j)); if (p_ostr->GetString().BeginsWith(str)) { // present hence replace oarray->AddAt(ostr.Clone(), j); - oarray->Write(); found = true; break; } } if (!found) { oarray->AddLast(ostr.Clone()); - oarray->Write(); } + oarray->Write(); } return true; @@ -936,6 +936,7 @@ Bool_t TMusrRunHeader::ExtractAll(TFolder *folder) while ((entry = dynamic_cast(next()))) { ExtractHeaderInformation(entry, entry->GetName()); } + return true; } @@ -950,7 +951,235 @@ Bool_t TMusrRunHeader::ExtractAll(TFolder *folder) */ Bool_t TMusrRunHeader::ExtractAll(TDirectory *dir) { - // NOT YET IMPLEMENTED + TIter next(dir->GetListOfKeys()); + TKey *key; + TObjString *entry; + + // clean up all internal structures - just in case this is called multiple times + if (dir->GetName() == "RunHeader") { +std::cout << "as35> dir->GetName()=" << dir->GetName() << std::endl; + CleanUp(); + } + + while ((key = (TKey*)next())) { + if (strcmp(key->GetClassName(), "TDirectoryFile") == 0) { + TDirectory *subdir = (TDirectory*)key->ReadObj(); + ExtractAll(subdir); + } else if (strcmp(key->GetClassName(), "TObjString") == 0) { + entry = (TObjString*)key->ReadObj(); + TString str = dir->GetPath(); + Ssiz_t start_idx = str.Index("RunHeader/"); + start_idx += 10; // got to end of 'RunHeader/' + ExtractHeaderInformationData(entry, str(start_idx, str.Length()-start_idx)); + } + } + + return true; +} + + +//-------------------------------------------------------------------------- +// ExtractHeaderInformationData (public) +//-------------------------------------------------------------------------- +/** + *

+ * + * \param headerData + * \param requestedPath + */ +Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath) +{ + TString label(""), pathName(""), str(""), strValue(""), type(""); + TObjString *ostr = nullptr; + TObjArray *tokens = nullptr; + Ssiz_t idx1; + Int_t intValue; + Double_t dval; + + ostr = dynamic_cast(headerData); + str = ostr->GetString(); + + // get the run header label + label = GetLabel(str); + if (label == "n/a") // not a TMusrRunHeader object, hence ignore it + return true; + + // get the run header 'value' + strValue = GetStrValue(str); + + // construct path name + pathName = requestedPath + TString("/") + label; + + // get type from map + type = GetType(str); + if (type == "n/a") // not a TMusrRunHeader object, hence ignore it + return true; + + if (type == "TString") { + Set(pathName, strValue); + } else if (type == "Int_t") { + intValue = strValue.Atoi(); + Set(pathName, intValue); + } else if (type == "Double_t") { + dval = strValue.Atof(); + Set(pathName, dval); + } else if (type == "TMusrRunPhysicalQuantity") { + TMusrRunPhysicalQuantity prop; + prop.SetLabel(label); + + // 1st get the description if present + tokens = strValue.Tokenize(";"); + if (tokens == nullptr) { + std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath)" << std::endl; + return false; + } + + switch (tokens->GetEntries()) { + case 2: + ostr = dynamic_cast(tokens->At(1)); + str = ostr->GetString(); + if (!str.Contains("SP:")) { // make sure that it is not a demand value token + prop.SetDescription(str); + } + break; + case 3: + ostr = dynamic_cast(tokens->At(2)); + str = ostr->GetString(); + break; + default: + break; + } + + if (tokens) { + delete tokens; + tokens = nullptr; + } + + // 2nd collect all the other properties, this is easier when first a potential description is removed + idx1 = strValue.Last(';'); + if (idx1 > 0) { + TString last(""); + for (Int_t i=idx1+2; i or SP: + if (!last.Contains("SP:")) { + str = ""; + for (Int_t i=0; i> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath)" << std::endl; + return false; + } + + switch (tokens->GetEntries()) { + case 2: // + ostr = dynamic_cast(tokens->At(0)); + str = ostr->GetString(); + prop.SetValue(str.Atof()); + ostr = dynamic_cast(tokens->At(1)); + str = ostr->GetString(); + prop.SetUnit(str); + break; + case 4: // +- , or ; SP: + ostr = dynamic_cast(tokens->At(0)); + str = ostr->GetString(); + prop.SetValue(str.Atof()); + ostr = dynamic_cast(tokens->At(1)); + str = ostr->GetString(); + if (str == "-") { // +- + ostr = dynamic_cast(tokens->At(2)); + str = ostr->GetString(); + prop.SetError(str.Atof()); + ostr = dynamic_cast(tokens->At(3)); + str = ostr->GetString(); + prop.SetUnit(str); + } else { // ; SP: + prop.SetUnit(str); + ostr = dynamic_cast(tokens->At(3)); + str = ostr->GetString(); + prop.SetDemand(str.Atof()); + } + break; + case 6: // +- ; SP: + ostr = dynamic_cast(tokens->At(0)); + str = ostr->GetString(); + prop.SetValue(str.Atof()); + ostr = dynamic_cast(tokens->At(2)); + str = ostr->GetString(); + prop.SetError(str.Atof()); + ostr = dynamic_cast(tokens->At(3)); + str = ostr->GetString(); + prop.SetUnit(str); + ostr = dynamic_cast(tokens->At(5)); + str = ostr->GetString(); + prop.SetDemand(str.Atof()); + break; + default: + break; + } + + if (tokens) { + delete tokens; + tokens = nullptr; + } + + Set(pathName, prop); + } else if (type == "TStringVector") { + TStringVector svec; + tokens = strValue.Tokenize(";"); + if (tokens == nullptr) { + std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath)" << std::endl; + return false; + } + for (Int_t i=0; iGetEntries(); i++) { + ostr = dynamic_cast(tokens->At(i)); + str = ostr->GetString(); + str.Remove(TString::kBoth, ' '); + svec.push_back(str); + } + if (tokens) { + delete tokens; + tokens = nullptr; + } + Set(pathName, svec); + } else if (type == "TIntVector") { + TIntVector ivec; + tokens = strValue.Tokenize(";"); + if (tokens == nullptr) { + std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath)" << std::endl; + return false; + } + for (Int_t i=0; iGetEntries(); i++) { + ostr = dynamic_cast(tokens->At(i)); + ivec.push_back(ostr->GetString().Atoi()); + } + if (tokens) { + delete tokens; + tokens = nullptr; + } + Set(pathName, ivec); + } else if (type == "TDoubleVector") { + TDoubleVector dvec; + tokens = strValue.Tokenize(";"); + if (tokens == nullptr) { + std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformationData(TObjString *headerData, TString requestedPath)" << std::endl; + return false; + } + for (Int_t i=0; iGetEntries(); i++) { + ostr = dynamic_cast(tokens->At(i)); + dvec.push_back(ostr->GetString().Atof()); + } + if (tokens) { + delete tokens; + tokens = nullptr; + } + Set(pathName, dvec); + } return true; } @@ -981,191 +1210,9 @@ Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString r path = requestedPath + "/" + TString(headerInfo->At(i)->GetName()); ExtractHeaderInformation(dynamic_cast(headerInfo->At(i)), path); } else { // handle all the rest, i.e. already data - ostr = dynamic_cast(headerInfo->At(i)); - str = ostr->GetString(); - - // get the run header label - label = GetLabel(str); - if (label == "n/a") // not a TMusrRunHeader object, hence ignore it - continue; - - // get the run header 'value' - strValue = GetStrValue(str); - - // construct path name - pathName = requestedPath + TString("/") + label; - - // get type from map - type = GetType(str); - if (type == "n/a") // not a TMusrRunHeader object, hence ignore it - continue; - - if (type == "TString") { - Set(pathName, strValue); - } else if (type == "Int_t") { - intValue = strValue.Atoi(); - Set(pathName, intValue); - } else if (type == "Double_t") { - dval = strValue.Atof(); - Set(pathName, dval); - } else if (type == "TMusrRunPhysicalQuantity") { - TMusrRunPhysicalQuantity prop; - prop.SetLabel(label); - - // 1st get the description if present - tokens = strValue.Tokenize(";"); - if (tokens == nullptr) { - std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString requestedPath)" << std::endl; - return false; - } - - switch (tokens->GetEntries()) { - case 2: - ostr = dynamic_cast(tokens->At(1)); - str = ostr->GetString(); - if (!str.Contains("SP:")) { // make sure that it is not a demand value token - prop.SetDescription(str); - } - break; - case 3: - ostr = dynamic_cast(tokens->At(2)); - str = ostr->GetString(); - break; - default: - break; - } - - if (tokens) { - delete tokens; - tokens = nullptr; - } - - // 2nd collect all the other properties, this is easier when first a potential description is removed - idx1 = strValue.Last(';'); - if (idx1 > 0) { - TString last(""); - for (Int_t i=idx1+2; i or SP: - if (!last.Contains("SP:")) { - str = ""; - for (Int_t i=0; i> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString requestedPath)" << std::endl; - return false; - } - - switch (tokens->GetEntries()) { - case 2: // - ostr = dynamic_cast(tokens->At(0)); - str = ostr->GetString(); - prop.SetValue(str.Atof()); - ostr = dynamic_cast(tokens->At(1)); - str = ostr->GetString(); - prop.SetUnit(str); - break; - case 4: // +- , or ; SP: - ostr = dynamic_cast(tokens->At(0)); - str = ostr->GetString(); - prop.SetValue(str.Atof()); - ostr = dynamic_cast(tokens->At(1)); - str = ostr->GetString(); - if (str == "-") { // +- - ostr = dynamic_cast(tokens->At(2)); - str = ostr->GetString(); - prop.SetError(str.Atof()); - ostr = dynamic_cast(tokens->At(3)); - str = ostr->GetString(); - prop.SetUnit(str); - } else { // ; SP: - prop.SetUnit(str); - ostr = dynamic_cast(tokens->At(3)); - str = ostr->GetString(); - prop.SetDemand(str.Atof()); - } - break; - case 6: // +- ; SP: - ostr = dynamic_cast(tokens->At(0)); - str = ostr->GetString(); - prop.SetValue(str.Atof()); - ostr = dynamic_cast(tokens->At(2)); - str = ostr->GetString(); - prop.SetError(str.Atof()); - ostr = dynamic_cast(tokens->At(3)); - str = ostr->GetString(); - prop.SetUnit(str); - ostr = dynamic_cast(tokens->At(5)); - str = ostr->GetString(); - prop.SetDemand(str.Atof()); - break; - default: - break; - } - - if (tokens) { - delete tokens; - tokens = nullptr; - } - - Set(pathName, prop); - } else if (type == "TStringVector") { - TStringVector svec; - tokens = strValue.Tokenize(";"); - if (tokens == nullptr) { - std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString requestedPath)" << std::endl; - return false; - } - for (Int_t i=0; iGetEntries(); i++) { - ostr = dynamic_cast(tokens->At(i)); - str = ostr->GetString(); - str.Remove(TString::kBoth, ' '); - svec.push_back(str); - } - if (tokens) { - delete tokens; - tokens = nullptr; - } - Set(pathName, svec); - } else if (type == "TIntVector") { - TIntVector ivec; - tokens = strValue.Tokenize(";"); - if (tokens == nullptr) { - std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString requestedPath)" << std::endl; - return false; - } - for (Int_t i=0; iGetEntries(); i++) { - ostr = dynamic_cast(tokens->At(i)); - ivec.push_back(ostr->GetString().Atoi()); - } - if (tokens) { - delete tokens; - tokens = nullptr; - } - Set(pathName, ivec); - } else if (type == "TDoubleVector") { - TDoubleVector dvec; - tokens = strValue.Tokenize(";"); - if (tokens == nullptr) { - std::cerr << std::endl << ">> **ERROR** Couldn't tokenize entry in Bool_t TMusrRunHeader::ExtractHeaderInformation(TObjArray *headerInfo, TString requestedPath)" << std::endl; - return false; - } - for (Int_t i=0; iGetEntries(); i++) { - ostr = dynamic_cast(tokens->At(i)); - dvec.push_back(ostr->GetString().Atof()); - } - if (tokens) { - delete tokens; - tokens = nullptr; - } - Set(pathName, dvec); - } + if (!ExtractHeaderInformationData(ostr, requestedPath)) + return false; } } diff --git a/src/external/MusrRoot/TMusrRunHeader.h b/src/external/MusrRoot/TMusrRunHeader.h index 999122e0f..ccabff36e 100644 --- a/src/external/MusrRoot/TMusrRunHeader.h +++ b/src/external/MusrRoot/TMusrRunHeader.h @@ -134,6 +134,7 @@ public: virtual Bool_t ExtractAll(TFolder *folder); virtual Bool_t ExtractAll(TDirectory *dir); + virtual Bool_t ExtractHeaderInformationData(TObjString *headerData, TString path); virtual Bool_t ExtractHeaderInformation(TObjArray *headerInfo, TString path); virtual TString GetTypeOfPath(TString pathName);