Skip to content

Commit

Permalink
Merge pull request #441 from rest-for-physics/jgalan-restRoot
Browse files Browse the repository at this point in the history
Updating restRoot behaviour
  • Loading branch information
jgalan authored Jun 19, 2023
2 parents 09688f7 + e28688a commit 29c2c59
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 220 deletions.
2 changes: 1 addition & 1 deletion cmake/thisREST.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export LIBRARY_PATH=\\\$REST_PATH/lib:\\\$LIBRARY_PATH
export PYTHONPATH=${PYTHON_BINDINGS_INSTALL_DIR}:\\\$PYTHONPATH
alias restRoot=\\\"restRoot -l\\\"
alias restRootMacros=\\\"restRoot -l --m 1\\\"
alias restRootMacros=\\\"restRoot -l --m\\\"
if [ \\\$(rest-config --flags | grep \\\"REST_WELCOME=ON\\\") ]; then
rest-config --welcome
Expand Down
41 changes: 41 additions & 0 deletions macros/REST_OpenInputFile.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
TRestRun* run0 = nullptr;
TRestAnalysisTree* ana_tree0 = nullptr;
TTree* ev_tree0 = nullptr;
TRestDataSet* dSet0 = nullptr;
std::map<std::string, TRestMetadata*> metadata0;

void REST_OpenInputFile(const std::string& fileName) {
if (TRestTools::isRunFile(fileName)) {
printf("\n%s\n", "REST processed file identified. It contains a valid TRestRun.");
run0 = new TRestRun(fileName);
printf("\nAttaching TRestRun %s as run0...\n", fileName.c_str());
ana_tree0 = run0->GetAnalysisTree();
printf("\nAttaching TRestAnalysisTree as ana_tree0...\n");
ev_tree0 = run0->GetEventTree();
printf("\nAttaching event tree as ev_tree0...\n");
std::map<std::string, int> metanames;
for (auto& [name, meta] : metadata0) delete meta;
metadata0.clear();
for (int n = 0; n < run0->GetNumberOfMetadata(); n++) {
std::string metaName = run0->GetMetadataNames()[n];
if (metaName.find("Historic") != string::npos) continue;
TRestMetadata* md = run0->GetMetadata(metaName);
metadata0[metaName] = md;
printf("\nAttaching Metadata class %s as metadata0[\"%s\"]...\n", md->ClassName(),
metaName.c_str());
}

} else if (TRestTools::isDataSet(fileName)) {
printf("\n%s\n", "REST dataset file identified. It contains a valid TRestDataSet.");
printf("\nImporting dataset %s as `dSet0`\n", fileName.c_str());
printf("\n%s\n", "The dataset is ready. You may now access the dataset using:");
printf("\n%s\n", " - dSet0->PrintMetadata()");
printf("%s\n", " - dSet0->GetDataFrame().GetColumnNames()");
printf("%s\n\n", " - dSet0->GetTree()->GetEntries()");
if (dSet0) delete dSet0;
dSet0 = new TRestDataSet();
dSet0->Import(fileName);
} else {
printf("\n%s is not a valid TRestRun or TRestDataSet\n", fileName.c_str());
}
}
117 changes: 26 additions & 91 deletions source/bin/restRoot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <TRint.h>
#include <TSystem.h>

#include "TRestMetadata.h"
#include "TRestRun.h"
#include "TRestStringHelper.h"
#include "TRestStringOutput.h"
#include "TRestTools.h"
#include "TRestVersion.h"

Expand All @@ -25,7 +25,7 @@ int main(int argc, char* argv[]) {
// set the env and debug status
setenv("REST_VERSION", REST_RELEASE, 1);

Int_t loadMacros = 0;
Bool_t loadMacros = false;
for (int i = 1; i < argc; i++) {
char* c = &argv[i][0];
if (*c == '-') {
Expand All @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
gVerbose = StringToVerboseLevel(argv[i + 1]);
break;
case 'm':
loadMacros = StringToInteger(argv[i + 1]);
loadMacros = true;
break;
case 'h':
// We use cout here since we will just exit afterwards
Expand Down Expand Up @@ -95,96 +95,31 @@ int main(int argc, char* argv[]) {
}
}

// load input root file with TRestRun, initialize input event, analysis tree and metadata structures
int nFile = 0;
for (int i = 1; i < argc; i++) {
string opt = (string)argv[i];
if (opt.find("http") != string::npos ||
(TRestTools::fileExists(opt) && TRestTools::isRootFile(opt))) {
printf("\nAttaching file %s as run%i...\n", opt.c_str(), nFile);

TRestRun* runTmp = new TRestRun(opt);
string runcmd =
Form("TRestRun* run%i = (TRestRun*)%s;", nFile, (PTR_ADDR_PREFIX + ToString(runTmp)).c_str());
if (debug) printf("%s\n", runcmd.c_str());
gROOT->ProcessLine(runcmd.c_str());
if (runTmp->GetInputEvent() != nullptr) {
string eventType = runTmp->GetInputEvent()->ClassName();

printf("Attaching event %s as ev%i...\n", eventType.c_str(), nFile);
string evcmd = Form("%s* ev%i = (%s*)%s;", eventType.c_str(), nFile, eventType.c_str(),
(PTR_ADDR_PREFIX + ToString(runTmp->GetInputEvent())).c_str());
if (debug) printf("%s\n", evcmd.c_str());
gROOT->ProcessLine(evcmd.c_str());
runTmp->GetEntry(0);
}

// command line AnalysisTree object
if (runTmp->GetAnalysisTree() != nullptr) {
// if (runTmp->GetAnalysisTree()->GetChain() != nullptr) {
// printf("Attaching ana_tree%i...\n", nFile);
// string evcmd = Form("TChain* ana_tree%i = (TChain*)%s;", nFile,
// ToString(runTmp->GetAnalysisTree()->GetChain()).c_str());
// if (debug) printf("%s\n", evcmd.c_str());
// gROOT->ProcessLine(evcmd.c_str());
//}
// else
//{
printf("Attaching ana_tree%i...\n", nFile);
string evcmd = Form("TRestAnalysisTree* ana_tree%i = (TRestAnalysisTree*)%s;", nFile,
(PTR_ADDR_PREFIX + ToString(runTmp->GetAnalysisTree())).c_str());
if (debug) printf("%s\n", evcmd.c_str());
gROOT->ProcessLine(evcmd.c_str());
// runTmp->GetEntry(0);
//}
}

// command line EventTree object
if (runTmp->GetEventTree() != nullptr) {
printf("Attaching ev_tree%i...\n", nFile);
string evcmd = Form("TTree* ev_tree%i = (TTree*)%s;", nFile,
(PTR_ADDR_PREFIX + ToString(runTmp->GetEventTree())).c_str());
if (debug) printf("%s\n", evcmd.c_str());
gROOT->ProcessLine(evcmd.c_str());
}

printf("\n%s\n", "Attaching metadata structures...");
Int_t numberOfMetadata = runTmp->GetNumberOfMetadata();
map<string, int> metanames;
for (int n = 0; n < numberOfMetadata; n++) {
string metaName = runTmp->GetMetadataNames()[n];
if (metaName.find("Historic") != string::npos) {
continue;
}
std::string runName = "";
std::string dSName = "";

TRestMetadata* md = runTmp->GetMetadata(metaName);
string metaType = md->ClassName();

string metaFixed = Replace(metaName, "-", "_");
metaFixed = Replace(metaFixed, " ", "");
metaFixed = Replace(metaFixed, ".", "_");
metaFixed = "md" + ToString(nFile) + "_" + metaFixed;
if (metanames.count(metaFixed) != 0) continue;
metanames[metaFixed] = 0;
printf("- %s (%s)\n", metaFixed.c_str(), metaType.c_str());

string mdcmd = Form("%s* %s = (%s*)%s;", metaType.c_str(), metaFixed.c_str(),
metaType.c_str(), (PTR_ADDR_PREFIX + ToString(md)).c_str());

if (debug) printf("%s\n", mdcmd.c_str());

gROOT->ProcessLine(mdcmd.c_str());

// if (metaType == "TRestGas") {
// string gascmd = Form("%s->LoadGasFile();", metaFixed.c_str());
// gROOT->ProcessLine(gascmd.c_str());
//}
}
for (int i = 1; i < argc; i++) {
const string opt = (string)argv[i];
if (opt.at(0) == ('-')) continue;

if (opt.find("http") == string::npos && !TRestTools::fileExists(opt)) {
printf("\nFile %s not compatible ... !!\n", opt.c_str());
continue;
}
if (TRestTools::isRunFile(opt) && runName.empty()) {
runName = opt;
string cmd = ".L " + REST_PATH + "/macros/REST_OpenInputFile.C";
if (!loadMacros && dSName.empty()) gROOT->ProcessLine(cmd.c_str());
cmd = "REST_OpenInputFile(\"" + runName + "\")";
gROOT->ProcessLine(cmd.c_str());
argv[i] = (char*)"";
} else if (TRestTools::isDataSet(opt) && dSName.empty()) {
dSName = opt;
string cmd = ".L " + REST_PATH + "/macros/REST_OpenInputFile.C";
if (!loadMacros && runName.empty()) gROOT->ProcessLine(cmd.c_str());
cmd = "REST_OpenInputFile(\"" + dSName + "\")";
gROOT->ProcessLine(cmd.c_str());
argv[i] = (char*)"";
nFile++;
} else if (TRestTools::isRootFile(opt)) {
printf("\nFile %s not found ... !!\n", opt.c_str());
}
}

Expand Down
14 changes: 0 additions & 14 deletions source/framework/core/src/TRestAnalysisPlot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ void TRestAnalysisPlot::InitFromConfigFile() {
// exit(1);
}

#pragma region ReadLabels
RESTDebug << "TRestAnalysisPlot: Reading canvas settings" << RESTendl;
TiXmlElement* formatDefinition = GetElement("labels");
if (formatDefinition != nullptr) {
Expand Down Expand Up @@ -121,9 +120,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme) GetChar();
}
}
#pragma endregion

#pragma region ReadLegend
TiXmlElement* legendDefinition = GetElement("legendPosition");
if (legendDefinition != nullptr) {
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
Expand Down Expand Up @@ -151,9 +148,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme) GetChar();
}
}
#pragma endregion

#pragma region ReadCanvas
TiXmlElement* canvasdef = fElement->FirstChildElement("canvas");
if (canvasdef == nullptr) {
canvasdef = fElement;
Expand All @@ -170,9 +165,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
fCanvasSave = GetDataPath() + save;

fPaletteStyle = StringToInteger(GetParameter("paletteStyle", canvasdef, "57"));
#pragma endregion

#pragma region ReadGlobalCuts
RESTDebug << "TRestAnalysisPlot: Reading global cuts" << RESTendl;
vector<string> globalCuts;
TiXmlElement* gCutele = GetElement("globalCut");
Expand Down Expand Up @@ -201,9 +194,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {

gCutele = GetNextElement(gCutele);
}
#pragma endregion

#pragma region ReadGlobalCutStrings
RESTDebug << "TRestAnalysisPlot: Reading global cut strings" << RESTendl;
TiXmlElement* gCutStrele = GetElement("globalCutString");
while (gCutStrele != nullptr) // general cuts
Expand All @@ -220,9 +211,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {

gCutStrele = GetNextElement(gCutStrele);
}
#pragma endregion

#pragma region ReadPlot
RESTDebug << "TRestAnalysisPlot: Reading plot sections" << RESTendl;
Int_t maxPlots = (Int_t)fCanvasDivisions.X() * (Int_t)fCanvasDivisions.Y();
TiXmlElement* plotele = GetElement("plot");
Expand Down Expand Up @@ -295,9 +284,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
plotele = GetNextElement(plotele);
}
}
#pragma endregion

#pragma region ReadPanel
RESTDebug << "TRestAnalysisPlot: Reading panel sections" << RESTendl;
maxPlots -= fPlots.size(); // remaining spaces on canvas
TiXmlElement* panelele = GetElement("panel");
Expand Down Expand Up @@ -339,7 +326,6 @@ void TRestAnalysisPlot::InitFromConfigFile() {
}
}
}
#pragma endregion

TRestAnalysisPlot::HistoInfoSet TRestAnalysisPlot::SetupHistogramFromConfigFile(TiXmlElement* histele,
PlotInfoSet plot) {
Expand Down
2 changes: 1 addition & 1 deletion source/framework/core/src/TRestMetadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ string TRestMetadata::GetParameter(string parName, size_t& pos, string inputStri
}

///////////////////////////////////////////////
/// \brief Identifies enviromental variable replacing marks in the input buffer,
/// \brief Identifies environmental variable replacing marks in the input buffer,
/// and replace them with corresponding value.
///
/// Replacing marks is like ${VARIABLE_NAME}. "variables" include system env, values
Expand Down
9 changes: 0 additions & 9 deletions source/framework/core/src/TRestMetadataPlot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ void TRestMetadataPlot::InitFromConfigFile() {
fRun = fHostmgr->GetRunInfo();
}

#pragma region ReadLabels
RESTDebug << "TRestMetadataPlot: Reading canvas settings" << RESTendl;
TiXmlElement* formatDefinition = GetElement("labels");
if (formatDefinition != nullptr) {
Expand Down Expand Up @@ -341,9 +340,7 @@ void TRestMetadataPlot::InitFromConfigFile() {
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme) GetChar();
}
}
#pragma endregion

#pragma region ReadLegend
TiXmlElement* legendDefinition = GetElement("legendPosition");
if (legendDefinition != nullptr) {
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
Expand Down Expand Up @@ -374,9 +371,7 @@ void TRestMetadataPlot::InitFromConfigFile() {
fLegendOption = GetFieldValue("option", legendDefinition);
if (fLegendOption == "Not defined") fLegendOption = "lp";
}
#pragma endregion

#pragma region ReadCanvas
TiXmlElement* canvasDefinition = GetElement("canvas");
if (canvasDefinition != nullptr) {
fCanvasSize = StringTo2DVector(GetFieldValue("size", canvasDefinition));
Expand All @@ -386,9 +381,7 @@ void TRestMetadataPlot::InitFromConfigFile() {
fCanvasSave = GetParameter("pdfFilename", REST_TMP_PATH + "restplot.pdf");
}
}
#pragma endregion

#pragma region ReadPlot
RESTDebug << "TRestMetadataPlot: Reading plot sections" << RESTendl;
Int_t maxPlots = (Int_t)fCanvasDivisions.X() * (Int_t)fCanvasDivisions.Y();
TiXmlElement* plotele = fElement->FirstChildElement("plot");
Expand Down Expand Up @@ -453,9 +446,7 @@ void TRestMetadataPlot::InitFromConfigFile() {
plotele = plotele->NextSiblingElement("plot");
}
}
#pragma endregion

#pragma region ReadPanel
RESTDebug << "TRestMetadataPlot: Reading panel sections" << RESTendl;
maxPlots -= fPlots.size(); // remaining spaces on canvas
TiXmlElement* panelele = fElement->FirstChildElement("panel");
Expand Down
Loading

0 comments on commit 29c2c59

Please sign in to comment.