Skip to content

Commit

Permalink
Merge pull request #267 from rest-for-physics/jgalan_replace_metadata
Browse files Browse the repository at this point in the history
Few string and TRestAnalysisPlot enhancements
  • Loading branch information
jgalan authored Jul 15, 2022
2 parents f6874df + 259eabe commit f506f68
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 83 deletions.
16 changes: 9 additions & 7 deletions source/framework/core/inc/TRestAnalysisPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ const std::map<std::string, int> FillStyleMap{

class TRestAnalysisPlot : public TRestMetadata {
public:
struct Histo_Info_Set {
struct HistoInfoSet {
std::string name; // will be shown in the legend
std::string range; // output histo std::string for TTree::Draw(), e.g. name+range = htemp(100,0,1000)
Bool_t status;

std::string plotString; // draw std::string for TTree::Draw()
std::string cutString; // cut std::string for TTree::Draw()
std::string weight;
std::map<std::string, std::string>
classifyMap; // select the input files to draw the histogram, if their
// TRestRun::Get() returns the assumed std::string
Expand All @@ -66,7 +67,7 @@ class TRestAnalysisPlot : public TRestMetadata {
TH3F* operator->() { return ptr; }
};

struct Plot_Info_Set {
struct PlotInfoSet {
std::string name;
std::string title;

Expand Down Expand Up @@ -102,11 +103,12 @@ class TRestAnalysisPlot : public TRestMetadata {

std::string save;

std::vector<Histo_Info_Set> histos;
std::vector<HistoInfoSet> histos;
};

struct Panel_Info {
struct PanelInfo {
Float_t font_size;
Int_t precision;

std::vector<Float_t> posX;
std::vector<Float_t> posY;
Expand All @@ -116,7 +118,7 @@ class TRestAnalysisPlot : public TRestMetadata {

private:
void InitFromConfigFile() override;
Histo_Info_Set SetupHistogramFromConfigFile(TiXmlElement* ele, Plot_Info_Set info);
HistoInfoSet SetupHistogramFromConfigFile(TiXmlElement* ele, PlotInfoSet info);

Int_t fNFiles;
// canvas option
Expand All @@ -140,8 +142,8 @@ class TRestAnalysisPlot : public TRestMetadata {
Double_t fLegendY2 = 0.88;

// plots information
std::vector<Plot_Info_Set> fPlots;
std::vector<Panel_Info> fPanels;
std::vector<PlotInfoSet> fPlots;
std::vector<PanelInfo> fPanels;

std::vector<std::string> fPlotNamesCheck; //!

Expand Down
2 changes: 1 addition & 1 deletion source/framework/core/inc/TRestMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class TRestMetadata : public TNamed {

std::string GetDataMemberValue(std::string memberName);

std::vector<std::string> GetDataMemberValues(std::string memberName);
std::vector<std::string> GetDataMemberValues(std::string memberName, Int_t precision = 0);

TString GetVersion(); // *MENU*

Expand Down
4 changes: 2 additions & 2 deletions source/framework/core/inc/TRestRun.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TRestRun : public TRestMetadata {
void InitFromConfigFile() override;

private:
std::string ReplaceMetadataMember(const std::string& instr);
std::string ReplaceMetadataMember(const std::string& instr, Int_t precision = 0);

public:
/// REST run class
Expand Down Expand Up @@ -172,7 +172,7 @@ class TRestRun : public TRestMetadata {
inline int GetNumberOfMetadataStructures() const { return fMetadata.size(); }

inline std::string GetMetadataMember(const std::string& instr) { return ReplaceMetadataMember(instr); }
std::string ReplaceMetadataMembers(const std::string& instr);
std::string ReplaceMetadataMembers(const std::string& instr, Int_t precision = 2);

Bool_t EvaluateMetadataMember(const std::string& instr);

Expand Down
51 changes: 30 additions & 21 deletions source/framework/core/src/TRestAnalysisPlot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
<< RESTendl;
exit(1);
}
Plot_Info_Set plot;
PlotInfoSet plot;
plot.name = RemoveWhiteSpaces(GetParameter("name", plotele, "plot_" + ToString(N)));
plot.title = GetParameter("title", plotele, plot.name);
plot.logY = StringToBool(GetParameter("logscale", plotele, "false"));
Expand All @@ -259,7 +259,7 @@ void TRestAnalysisPlot::InitFromConfigFile() {
plot.ticksY = StringToInteger(GetParameter("yticks", plotele, "510"));
plot.marginBottom = StringToDouble(GetParameter("marginBottom", plotele, "0.15"));
plot.marginTop = StringToDouble(GetParameter("marginTop", plotele, "0.07"));
plot.marginLeft = StringToDouble(GetParameter("marginLeft", plotele, "0.18"));
plot.marginLeft = StringToDouble(GetParameter("marginLeft", plotele, "0.25"));
plot.marginRight = StringToDouble(GetParameter("marginRight", plotele, "0.1"));
plot.legendOn = StringToBool(GetParameter("legend", plotele, "OFF"));
plot.staticsOn = StringToBool(GetParameter("stats", plotele, "OFF"));
Expand All @@ -275,18 +275,15 @@ void TRestAnalysisPlot::InitFromConfigFile() {
histele = plotele;
}
while (histele != nullptr) {
Histo_Info_Set hist = SetupHistogramFromConfigFile(histele, plot);
HistoInfoSet hist = SetupHistogramFromConfigFile(histele, plot);
// add global cut
for (unsigned int i = 0; i < globalCuts.size(); i++) {
if (i > 0 || hist.cutString != "") hist.cutString += " && ";
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug)
cout << "Adding global cut : " << globalCuts[i] << endl;
hist.cutString += globalCuts[i];
}
//// add "SAME" option
// if (plot.histos.size() > 0) {
// hist.drawOption += " SAME";
//}
hist.weight = GetParameter("weight", histele, "");

if (hist.plotString == "") {
RESTWarning << "No variables or histograms defined in the plot, skipping!" << RESTendl;
Expand Down Expand Up @@ -321,8 +318,9 @@ void TRestAnalysisPlot::InitFromConfigFile() {
exit(1);
}

Panel_Info panel;
PanelInfo panel;
panel.font_size = StringToDouble(GetParameter("font_size", panelele, "0.1"));
panel.precision = StringToInteger(GetParameter("precision", panelele, "2"));

TiXmlElement* labelele = GetElement("label", panelele);
while (labelele != nullptr) {
Expand All @@ -349,9 +347,9 @@ void TRestAnalysisPlot::InitFromConfigFile() {
}
#pragma endregion

TRestAnalysisPlot::Histo_Info_Set TRestAnalysisPlot::SetupHistogramFromConfigFile(TiXmlElement* histele,
Plot_Info_Set plot) {
Histo_Info_Set hist;
TRestAnalysisPlot::HistoInfoSet TRestAnalysisPlot::SetupHistogramFromConfigFile(TiXmlElement* histele,
PlotInfoSet plot) {
HistoInfoSet hist;
hist.name = RemoveWhiteSpaces(GetParameter("name", histele, plot.name));
hist.drawOption = GetParameter("option", histele, "colz");

Expand Down Expand Up @@ -686,7 +684,7 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
label = Replace(label, "<<meanRate>>", Form("%5.2lf", meanRate), pos);

auto run = GetRunInfo(fRunInputFileName[0]);
label = run->ReplaceMetadataMembers(label);
label = run->ReplaceMetadataMembers(label, fPanels[n].precision);

TLatex* texxt = new TLatex(fPanels[n].posX[m], fPanels[n].posY[m], label.c_str());
texxt->SetTextColor(1);
Expand All @@ -698,7 +696,7 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
// start drawing plots
vector<TH3F*> histCollectionAll;
for (unsigned int n = 0; n < fPlots.size(); n++) {
Plot_Info_Set& plot = fPlots[n];
PlotInfoSet& plot = fPlots[n];

TPad* targetPad = (TPad*)fCombinedCanvas->cd(n + 1 + fPanels.size());
targetPad->SetLogx(plot.logX);
Expand All @@ -713,7 +711,7 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {

// draw each histogram in the pad
for (unsigned int i = 0; i < plot.histos.size(); i++) {
Histo_Info_Set& hist = plot.histos[i];
HistoInfoSet& hist = plot.histos[i];

TString plotString = hist.plotString;
TString nameString = hist.name;
Expand All @@ -725,6 +723,11 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
rangeString = Replace((string)rangeString, "MIN_TIME", (string)Form("%9f", startTime), pos);
rangeString = Replace((string)rangeString, "MAX_TIME", (string)Form("%9f", endTime), pos);

if (cutString == "")
cutString = hist.weight;
else if (hist.weight != "")
cutString = "(" + cutString + ") * " + hist.weight;

if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
cout << endl;
cout << "--------------------------------------" << endl;
Expand Down Expand Up @@ -816,12 +819,12 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
hTotal->GetXaxis()->SetTitle(plot.labelX.c_str());
hTotal->GetYaxis()->SetTitle(plot.labelY.c_str());

hTotal->GetXaxis()->SetLabelSize(fTicksScaleX * hTotal->GetXaxis()->GetLabelSize());
hTotal->GetYaxis()->SetLabelSize(fTicksScaleY * hTotal->GetYaxis()->GetLabelSize());
hTotal->GetXaxis()->SetTitleSize(fLabelScaleX * hTotal->GetXaxis()->GetTitleSize());
hTotal->GetYaxis()->SetTitleSize(fLabelScaleY * hTotal->GetYaxis()->GetTitleSize());
hTotal->GetXaxis()->SetTitleOffset(fLabelOffsetX * hTotal->GetXaxis()->GetTitleOffset());
hTotal->GetYaxis()->SetTitleOffset(fLabelOffsetY * hTotal->GetYaxis()->GetTitleOffset());
hTotal->GetXaxis()->SetLabelSize(1.1 * hTotal->GetXaxis()->GetLabelSize());
hTotal->GetYaxis()->SetLabelSize(1.1 * hTotal->GetYaxis()->GetLabelSize());
hTotal->GetXaxis()->SetTitleSize(1.1 * hTotal->GetXaxis()->GetTitleSize());
hTotal->GetYaxis()->SetTitleSize(1.1 * hTotal->GetYaxis()->GetTitleSize());
hTotal->GetXaxis()->SetTitleOffset(1 * hTotal->GetXaxis()->GetTitleOffset());
hTotal->GetYaxis()->SetTitleOffset(1 * hTotal->GetYaxis()->GetTitleOffset());
hTotal->GetXaxis()->SetNdivisions(plot.ticksX);
hTotal->GetYaxis()->SetNdivisions(plot.ticksY);

Expand Down Expand Up @@ -903,7 +906,7 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
// draw the remaining histo
if (plot.histos[i].ptr == nullptr) continue;
if (i != maxID) {
plot.histos[i]->Draw((plot.histos[maxID].drawOption + "same").c_str());
plot.histos[i]->Draw((plot.histos[i].drawOption + "same").c_str());
}
}

Expand All @@ -927,6 +930,9 @@ void TRestAnalysisPlot::PlotCombinedCanvas() {
}

// save pad
targetPad->SetRightMargin(plot.marginRight);
targetPad->SetLeftMargin(plot.marginLeft);
targetPad->SetBottomMargin(plot.marginBottom);
targetPad->Update();
if (plot.save != "") targetPad->Print(plot.save.c_str());

Expand Down Expand Up @@ -975,6 +981,9 @@ void TRestAnalysisPlot::SavePlotToPDF(TString fileName, Int_t n) {
}

TPad* pad = (TPad*)fCombinedCanvas->GetPad(n);
pad->SetRightMargin(fPlots[n - 1].marginRight);
pad->SetLeftMargin(fPlots[n - 1].marginLeft);
pad->SetBottomMargin(fPlots[n - 1].marginBottom);

TCanvas* c = new TCanvas(fPlots[n - 1].name.c_str(), fPlots[n - 1].name.c_str(), 800, 600);
pad->DrawClone();
Expand Down
11 changes: 6 additions & 5 deletions source/framework/core/src/TRestEventProcess.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Bool_t TRestEventProcess::OpenInputFiles(vector<string> files) { return false; }
Int_t TRestEventProcess::LoadSectionMetadata() {
TRestMetadata::LoadSectionMetadata();

if (ToUpper(GetParameter("observable", "")) == "ALL") {
if (ToUpper(GetParameter("observable", "")) == "ALL" ||
ToUpper(GetParameter("observables", "")) == "ALL") {
fDynamicObs = true;
}

Expand Down Expand Up @@ -288,11 +289,11 @@ TRestEventProcess* TRestEventProcess::GetFriendLive(string nameortype) {
/// \brief Get a list of parallel processes from this process
///
/// Parallel process means the process in other threads. It differs from "friend process"
/// in another dimension. For example, we set up the process chain with one
/// in another dimension. For example, we set up the process chain with one
/// `TRestRawSignalAnalysisProcess` and one `TRestRawToSignalProcess`, and calls 2 threads to
/// run the data. Then, for `TRestRawSignalAnalysisProcess` in thread 1, it has a parallel
/// process `TRestRawSignalAnalysisProcess` from thread 2, and a friend process
/// `TRestRawToSignalProcess` from thread 1.
/// run the data. Then, for `TRestRawSignalAnalysisProcess` in thread 1, it has a parallel
/// process `TRestRawSignalAnalysisProcess` from thread 2, and a friend process
/// `TRestRawToSignalProcess` from thread 1.
TRestEventProcess* TRestEventProcess::GetParallel(int i) {
if (i >= 0 && i < fParallelProcesses.size()) {
return fParallelProcesses[i];
Expand Down
22 changes: 15 additions & 7 deletions source/framework/core/src/TRestMetadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,10 @@ void TRestMetadata::ReplaceForLoopVars(TiXmlElement* e, map<string, string> forL
}
}

e->SetAttribute(
name, ReplaceMathematicalExpressions(
outputBuffer, "Please, check parameter name: " + parName + " (ReplaceForLoopVars)")
.c_str());
e->SetAttribute(name, ReplaceMathematicalExpressions(
outputBuffer, 0,
"Please, check parameter name: " + parName + " (ReplaceForLoopVars)")
.c_str());
}

attr = attr->Next();
Expand Down Expand Up @@ -1484,7 +1484,7 @@ string TRestMetadata::GetParameter(std::string parName, TiXmlElement* e, TString
}
}

return ReplaceMathematicalExpressions(ReplaceConstants(ReplaceVariables(result)),
return ReplaceMathematicalExpressions(ReplaceConstants(ReplaceVariables(result)), 0,
"Please, check parameter name: " + parName);
}

Expand Down Expand Up @@ -2308,13 +2308,21 @@ string TRestMetadata::GetDataMemberValue(string memberName) {
///
/// All kinds of data member can be found, including non-streamed
/// data member and base-class data member
std::vector<string> TRestMetadata::GetDataMemberValues(string memberName) {
///
/// If precision value is higher than 0, then the resulting values will be
/// truncated after finding ".". This can be used to define a float precision.
///
std::vector<string> TRestMetadata::GetDataMemberValues(string memberName, Int_t precision) {
string result = GetDataMemberValue(memberName);

result = Replace(result, "{", "");
result = Replace(result, "}", "");

return Split(result, ",");
std::vector<std::string> results = REST_StringHelper::Split(result, ",");

for (auto& x : results) x = REST_StringHelper::CropWithPrecision(x, precision);

return results;
}

///////////////////////////////////////////////
Expand Down
Loading

0 comments on commit f506f68

Please sign in to comment.