530 std::vector<std::string> obsList;
533 if (
fCut !=
nullptr) {
534 const auto paramCut =
fCut->GetParamCut();
535 for (
const auto& [param, condition] : paramCut) {
536 obsList.push_back(param);
541 for (
const auto& plots :
fPlots) {
542 for (
const auto& hist : plots.histos) {
543 for (
const auto& var : hist.variable) {
544 obsList.push_back(var);
546 if (hist.histoCut ==
nullptr)
continue;
547 const auto paramCut = hist.histoCut->GetParamCut();
548 for (
const auto& [param, condition] : paramCut) {
549 obsList.push_back(param);
554 std::map<std::string, TRestDataSet::RelevantQuantity> quantity;
558 for (
auto& [key, posLabel] : panel.expPos) {
560 auto&& [exp, label, units, precision] = key;
561 std::string text = exp;
562 while (text.find_last_of(
'[') != std::string::npos) {
563 int squareBracketCorrector = 0;
564 size_t posOpen = text.find_last_of(
'[');
565 size_t posClose = text.find_first_of(
']', posOpen);
567 if (text[posOpen - 1] ==
'[') {
568 squareBracketCorrector = 1;
571 std::string varOrMeta = text.substr(posOpen - squareBracketCorrector,
572 posClose + 1 - posOpen + 2 * squareBracketCorrector);
573 if (squareBracketCorrector == 0) {
579 quantity[label] = quant;
581 text =
Replace(text, varOrMeta,
"1");
585 while (text.find(
"_") != std::string::npos) {
586 size_t pos_ = text.find(
"_");
587 size_t beginning = text.find_last_of(
" -+*/)(^%", pos_) + 1;
588 size_t end = text.find_first_of(
" -+*/)(^%", pos_);
589 std::string obs = text.substr(beginning, end - beginning);
590 text =
Replace(text, obs,
"1");
591 obsList.push_back(obs);
595 RESTWarning <<
"The expression " << exp
596 <<
" has not been correctly parsed into variables, metadata and observables"
601 for (
auto& [key, posLabel] : panel.obsPos) {
602 auto&& [obs, label, units] = key;
603 obsList.push_back(obs);
606 for (
auto& [key, posLabel] : panel.metadataPos) {
607 auto&& [metadata, label, units] = key;
611 quantity[label] = quant;
616 std::sort(obsList.begin(), obsList.end());
617 obsList.erase(std::unique(obsList.begin(), obsList.end()), obsList.end());
619 dataSet.SetObservablesList(obsList);
620 dataSet.SetQuantity(quantity);
630 dataSet.EnableMultiThreading(
true);
636 if (dataSet.
GetTree() ==
nullptr) {
637 RESTWarning <<
"Cannot import dataSet, trying to generate it with pattern " <<
fDataSetName
639 RESTWarning <<
"Note that the generation of a dataSet inside TRestDataSetPlot is deplecated. Check "
640 "TRestDataSet documentation to generate a dataSet"
643 if (dataSet.
GetTree() ==
nullptr) {
644 RESTError <<
"Cannot generate dataSet " <<
RESTendl;
653 combinedCanvas.Divide((Int_t)fCanvasDivisions.X(), (Int_t)fCanvasDivisions.Y(),
654 fCanvasDivisionMargins.X(), fCanvasDivisionMargins.Y());
659 const double startTime = dataSet.GetStartTime();
660 const double endTime = dataSet.GetEndTime();
663 std::map<std::string, std::string> paramMap;
668 const auto quantity = dataSet.GetQuantity();
673 combinedCanvas.cd(canvasIndex);
675 auto dataFrame = dataSet.
MakeCut(panel.panelCut);
676 const int entries = *dataFrame.Count();
677 const double meanRate = entries / duration;
678 const double runLength = duration / 3600.;
683 paramMap[
"[[cutNames]]"] =
"";
684 paramMap[
"[[cuts]]"] =
"";
686 for (
const auto& cut :
fCut->GetCuts()) {
687 if (paramMap[
"[[cutNames]]"].empty())
688 paramMap[
"[[cutNames]]"] += cut.GetName();
690 paramMap[
"[[cutNames]]"] +=
"," + (std::string)cut.GetName();
691 if (paramMap[
"[[cuts]]"].empty())
692 paramMap[
"[[cuts]]"] += cut.GetTitle();
694 paramMap[
"[[cuts]]"] +=
" && " + (std::string)cut.GetTitle();
698 paramMap[
"[[panelCutNames]]"] =
"";
699 paramMap[
"[[panelCuts]]"] =
"";
700 if (panel.panelCut) {
701 for (
const auto& cut : panel.panelCut->GetCuts()) {
702 if (paramMap[
"[[panelCutNames]]"].empty())
703 paramMap[
"[[panelCutNames]]"] += cut.GetName();
705 paramMap[
"[[panelCutNames]]"] +=
"," + (std::string)cut.GetName();
706 if (paramMap[
"[[panelCuts]]"].empty())
707 paramMap[
"[[panelCuts]]"] += cut.GetTitle();
709 paramMap[
"[[panelCuts]]"] +=
" && " + (std::string)cut.GetTitle();
713 RESTInfo <<
"Global cuts: " << paramMap[
"[[cuts]]"] <<
RESTendl;
714 if (!paramMap[
"[[panelCuts]]"].empty())
715 RESTInfo <<
"Additional panel cuts: " << paramMap[
"[[panelCuts]]"] <<
RESTendl;
718 for (
const auto& [key, posLabel] : panel.variablePos) {
719 auto&& [variable, label, units] = key;
721 std::string var = variable;
723 for (
const auto& [param, val] : paramMap) {
726 var =
Replace(var, param, val, pos);
731 if (!found) RESTWarning <<
"Variable " << variable <<
" not found" <<
RESTendl;
734 if (var.find(
'.') == std::string::npos) {
736 var = StringWithPrecision(dblVar, panel.precision);
739 var = StringWithPrecision(dblVar, panel.precision);
743 label + panel.delimiter.Data() + StringWithPrecision(var, panel.precision) +
" " + units;
744 panel.text.emplace_back(
new TLatex(posLabel.X(), posLabel.Y(), lab.c_str()));
748 for (
const auto& [key, posLabel] : panel.metadataPos) {
749 auto&& [metadata, label, units] = key;
750 std::string value =
"";
752 for (
const auto& [name, quant] : quantity) {
753 if (quant.metadata == metadata) value = quant.value;
757 RESTWarning <<
"Metadata quantity " << metadata <<
" not found in dataSet" <<
RESTendl;
762 label + panel.delimiter.Data() + StringWithPrecision(value, panel.precision) +
" " + units;
763 panel.text.emplace_back(
new TLatex(posLabel.X(), posLabel.Y(), lab.c_str()));
767 for (
const auto& [key, posLabel] : panel.obsPos) {
768 auto&& [obs, label, units] = key;
769 auto value = *dataFrame.Mean(obs);
772 label + panel.delimiter.Data() + StringWithPrecision(value, panel.precision) +
" " + units;
773 panel.text.emplace_back(
new TLatex(posLabel.X(), posLabel.Y(), lab.c_str()));
777 for (
const auto& [key, posLabel] : panel.expPos) {
778 auto&& [text, label, units, precision] = key;
779 std::string var = text;
782 std::vector<std::string> subtexts;
783 while (var.find_last_of(
'{') != std::string::npos) {
784 size_t posOpen = var.find_last_of(
'{');
785 size_t posClose = var.find_first_of(
'}', posOpen);
786 if (posClose == std::string::npos) {
787 RESTWarning <<
"Unmatched { in expression: " << var <<
RESTendl;
790 std::string subtext = var.substr(posOpen + 1, posClose - posOpen - 1);
791 subtexts.push_back(subtext);
793 var.erase(posOpen, posClose - posOpen + 1);
798 std::vector<std::string> precisionParts;
799 for (
const auto& part :
Split(precision,
",",
false,
true)) {
800 precisionParts.push_back(part);
802 if (precisionParts.size() != subtexts.size()) {
803 RESTDebug <<
"Not enough precision values provided for the expression: `" << text
804 <<
"`. Using " << precisionParts.back() <<
" for last subtexts." <<
RESTendl;
805 precisionParts.resize(subtexts.size(), precisionParts.back());
808 size_t precisionIndex = precisionParts.size() - 1;
809 for (
const auto& subtext : subtexts) {
810 std::string subVar = subtext;
812 for (
const auto& [param, val] : paramMap) {
813 subVar =
Replace(subVar, param, val);
816 for (
const auto& [name, quant] : quantity) {
817 subVar =
Replace(subVar,
"[" + name +
"]", quant.value);
818 subVar =
Replace(subVar, name, quant.value);
821 for (
const auto& obs : dataFrame.GetColumnNames()) {
822 if (var.find(obs) == std::string::npos)
continue;
825 double value = *dataFrame.Mean(obs);
828 subVar =
Replace(subVar,
"[",
"(");
829 subVar =
Replace(subVar,
"]",
")");
833 std::string prec = precisionParts[precisionIndex--];
834 if (prec.find(
'%') != std::string::npos) {
839 RESTWarning <<
"Unknown precision format: " << prec
840 <<
". Using panels precision: " << panel.precision <<
RESTendl;
841 subVar = StringWithPrecision(value, panel.precision);
844 var =
Replace(var,
"{" + subtext +
"}", subVar);
847 std::string lab = label + panel.delimiter.Data() + var +
" " + units;
848 panel.text.emplace_back(
new TLatex(posLabel.X(), posLabel.Y(), lab.c_str()));
852 for (
const auto& text : panel.text) {
853 text->SetTextColor(1);
854 text->SetTextSize(panel.font_size);
860 for (
auto& plots :
fPlots) {
862 combinedCanvas.cd(canvasIndex);
863 plots.hs =
new THStack(plots.name.c_str(), plots.title.c_str());
864 if (plots.legendOn) plots.legend =
new TLegend(
fLegendX1, fLegendY1, fLegendX2, fLegendY2);
866 for (
auto& hist : plots.histos) {
867 auto dataFrame = dataSet.
MakeCut(hist.histoCut);
868 if (hist.variable.front() ==
"timeStamp") {
869 hist.range.front().SetX(startTime);
870 hist.range.front().SetY(endTime);
873 if (hist.variable.size() == 1) {
874 auto histo = dataFrame.Histo1D({hist.name.c_str(), hist.name.c_str(), hist.nBins.front(),
875 hist.range.front().X(), hist.range.front().Y()},
876 hist.variable.front());
877 hist.histo =
static_cast<TH1*
>(histo->DrawClone());
879 }
else if (hist.variable.size() == 2) {
880 auto histo = dataFrame.Histo2D(
881 {hist.name.c_str(), hist.name.c_str(), hist.nBins.front(), hist.range.front().X(),
882 hist.range.front().Y(), hist.nBins.back(), hist.range.back().X(), hist.range.back().Y()},
883 hist.variable.front(), hist.variable.back());
884 hist.histo =
static_cast<TH1*
>(histo->DrawClone());
886 RESTError <<
"Only 1D or 2D histograms are supported " <<
RESTendl;
889 hist.histo->SetLineColor(hist.lineColor);
890 hist.histo->SetLineWidth(hist.lineWidth);
891 hist.histo->SetLineStyle(hist.lineStyle);
892 hist.histo->SetFillColor(hist.fillColor);
893 hist.histo->SetFillStyle(hist.fillStyle);
895 if (hist.statistics) {
896 hist.histo->SetStats(
true);
898 combinedCanvas.Update();
900 hist.histo->SetStats(
false);
903 if (plots.normalize > 0) {
904 const double integral = hist.histo->Integral();
905 if (integral > 0) hist.histo->Scale(plots.normalize / integral);
908 if (plots.scale !=
"") {
909 std::string inputScale = plots.scale;
910 double binSize = hist.histo->GetXaxis()->GetBinWidth(1);
911 double entries = hist.histo->GetEntries();
913 double integral = hist.histo->Integral(
"width");
920 std::string scale =
"1./(" + inputScale +
")";
925 plots.hs->Add(hist.histo, hist.drawOption.c_str());
927 if (plots.legend !=
nullptr) plots.legend->AddEntry(hist.histo, hist.histo->GetName(),
"lf");
932 for (
auto& plots :
fPlots) {
933 if (plots.hs ==
nullptr)
continue;
935 TPad* targetPad = (TPad*)combinedCanvas.cd(canvasIndex);
936 targetPad->SetLogx(plots.logX);
937 targetPad->SetLogy(plots.logY);
938 targetPad->SetLogz(plots.logZ);
939 targetPad->SetGridx(plots.gridX);
940 targetPad->SetGridy(plots.gridY);
941 targetPad->SetLeftMargin(plots.marginLeft);
942 targetPad->SetRightMargin(plots.marginRight);
943 targetPad->SetBottomMargin(plots.marginBottom);
944 targetPad->SetTopMargin(plots.marginTop);
947 plots.hs->Draw(plots.stackDrawOption.c_str());
948 plots.hs->GetXaxis()->SetTitle(plots.labelX.c_str());
949 plots.hs->GetYaxis()->SetTitle(plots.labelY.c_str());
950 plots.hs->GetXaxis()->SetLabelSize(1.1 * plots.hs->GetXaxis()->GetLabelSize());
951 plots.hs->GetYaxis()->SetLabelSize(1.1 * plots.hs->GetYaxis()->GetLabelSize());
952 plots.hs->GetXaxis()->SetTitleSize(1.1 * plots.hs->GetXaxis()->GetTitleSize());
953 plots.hs->GetYaxis()->SetTitleSize(1.1 * plots.hs->GetYaxis()->GetTitleSize());
955 if (plots.timeDisplay) plots.hs->GetXaxis()->SetTimeDisplay(1);
956 if (plots.legend !=
nullptr) plots.legend->Draw();
959 combinedCanvas.Update();
965 combinedCanvas.Resize();
970 for (
auto& plots :
fPlots) {
971 if (plots.save.empty())
continue;
972 std::unique_ptr<TCanvas> canvas(
new TCanvas());
973 canvas->SetLogx(plots.logX);
974 canvas->SetLogy(plots.logY);
975 canvas->SetLogz(plots.logZ);
976 canvas->SetGridx(plots.gridX);
977 canvas->SetGridy(plots.gridY);
978 canvas->SetLeftMargin(plots.marginLeft);
979 canvas->SetRightMargin(plots.marginRight);
980 canvas->SetBottomMargin(plots.marginBottom);
981 canvas->SetTopMargin(plots.marginTop);
982 plots.hs->Draw(plots.stackDrawOption.c_str());
983 canvas->Print(plots.save.c_str());
988 for (
const auto& [name, quant] : quantity) {
997 RESTError << file.Error() <<
RESTendl;
1000 for (
auto& plots :
fPlots) {
1001 for (
auto& hist : plots.histos) {
1002 hist.histo->Write();
1006 if (!file.Close()) RESTError << file.Error() <<
RESTendl;