234 #include "TRestMetadataPlot.h"
236 #include "TRestManager.h"
237 #include "TRestTools.h"
301 if (
fHostmgr->GetRunInfo() !=
nullptr) {
305 RESTDebug <<
"TRestMetadataPlot: Reading canvas settings" <<
RESTendl;
306 TiXmlElement* formatDefinition =
GetElement(
"labels");
307 if (formatDefinition !=
nullptr) {
309 cout << formatDefinition << endl;
310 cout <<
"Reading format definition : " << endl;
311 cout <<
"---------------------------" << endl;
344 TiXmlElement* legendDefinition =
GetElement(
"legendPosition");
345 if (legendDefinition !=
nullptr) {
347 cout << legendDefinition << endl;
348 cout <<
"Reading legend definition : " << endl;
349 cout <<
"---------------------------" << endl;
375 TiXmlElement* canvasDefinition =
GetElement(
"canvas");
376 if (canvasDefinition !=
nullptr) {
385 RESTDebug <<
"TRestMetadataPlot: Reading plot sections" <<
RESTendl;
387 TiXmlElement* plotele =
fElement->FirstChildElement(
"plot");
388 while (plotele !=
nullptr) {
415 RESTDebug <<
" Plot parameters read from <plot section " <<
RESTendl;
416 RESTDebug <<
" --------------------------------------- " <<
RESTendl;
428 TiXmlElement* graphele = plotele->FirstChildElement(
"graph");
429 if (graphele ==
nullptr) {
434 while (graphele !=
nullptr) {
440 plot.
graphs.push_back(graph);
442 graphele = graphele->NextSiblingElement(
"graph");
446 plotele = plotele->NextSiblingElement(
"plot");
450 RESTDebug <<
"TRestMetadataPlot: Reading panel sections" <<
RESTendl;
451 maxPlots -=
fPlots.size();
452 TiXmlElement* panelele =
fElement->FirstChildElement(
"panel");
453 while (panelele !=
nullptr) {
460 <<
" plots, and " << N + 1 <<
" info panels" <<
RESTendl;
467 TiXmlElement* labelele = panelele->FirstChildElement(
"label");
468 while (labelele !=
nullptr) {
469 panel.
label.push_back(
GetParameter(
"value", labelele,
"Error. Label value not defined"));
473 labelele = labelele->NextSiblingElement(
"label");
477 panelele = panelele->NextSiblingElement(
"panel");
481 for (
unsigned int n = 0; n <
fPanels.size(); n++) {
482 RESTExtreme <<
"Panel " << n <<
" with font size : " <<
fPanels[n].font_size <<
RESTendl;
483 for (
unsigned int m = 0; m <
fPanels[n].posX.size(); m++) {
506 <<
"Repeated plot/graph names were found! Please, use different names for different plots!"
508 RESTError <<
"<plot/graph name=\"" << graph.
name <<
"\" already defined!" <<
RESTendl;
513 RESTError <<
"Problem reading yVariable from graph with name : " << graph.
name <<
RESTendl;
535 RESTDebug <<
"TRestMetadataPlot::AddFile. Adding file. " <<
RESTendl;
536 RESTDebug <<
"File name: " << fileName <<
RESTendl;
547 if (
fRun !=
nullptr) {
548 if (
fHostmgr->GetProcessRunner() !=
nullptr &&
fRun->GetOutputFileName() !=
"") {
552 }
else if (
fRun->GetInputFileNames().size() != 0) {
554 auto names =
fRun->GetInputFileNames();
555 for (
unsigned int i = 0; i < names.size(); i++) {
570 for (
unsigned int n = 0; n < files.size(); n++) {
571 RESTEssential <<
"Adding file : " << files[n] <<
RESTendl;
580 for (
unsigned int n = 0; n <
fPlots.size(); n++)
581 if (
fPlots[n].name == plotName)
return n;
583 RESTWarning <<
"TRestMetadataPlot::GetPlotIndex. Plot name " << plotName <<
" not found" <<
RESTendl;
592 RESTInfo <<
"--------------------------" <<
RESTendl;
593 RESTInfo <<
"Starting to GenerateCanvas" <<
RESTendl;
594 RESTInfo <<
"--------------------------" <<
RESTendl;
596 TiXmlElement* ele =
fElement->FirstChildElement(
"addFile");
597 while (ele !=
nullptr) {
600 ele = ele->NextSiblingElement(
"addFile");
608 RESTError <<
"TRestMetadataPlot: No input files are added!" <<
RESTendl;
613 TFile* fOutputRootFile =
nullptr;
614 if (
fRun !=
nullptr) {
615 fOutputRootFile =
fRun->GetOutputFile();
616 if (fOutputRootFile ==
nullptr) {
617 fRun->SetHistoricMetadataSaving(
false);
631 TStyle* st =
new TStyle();
635 Double_t startTime = 0;
636 Double_t endTime = 0;
637 Double_t runLength = 0;
638 Int_t totalEntries = 0;
641 run->SetHistoricMetadataSaving(
false);
646 Double_t endTimeStamp = run->GetEndTimestamp();
647 Double_t startTimeStamp = run->GetStartTimestamp();
650 if (!startTime || startTime > endTimeStamp) startTime = endTimeStamp;
651 if (!startTime || startTime > startTimeStamp) startTime = startTimeStamp;
653 if (!endTime || endTime < startTimeStamp) endTime = startTimeStamp;
654 if (!endTime || endTime < endTimeStamp) endTime = endTimeStamp;
656 if (endTimeStamp - startTimeStamp > 0) {
657 runLength += endTimeStamp - startTimeStamp;
658 totalEntries += run->GetEntries();
663 Double_t meanRate = totalEntries / runLength;
668 panelRun->SetHistoricMetadataSaving(
false);
670 for (
unsigned int n = 0; n <
fPanels.size(); n++) {
672 for (
unsigned int m = 0; m <
fPanels[n].posX.size(); m++) {
673 string label =
fPanels[n].label[m];
680 label =
Replace(label,
"<<entries>>", Form(
"%d", totalEntries), pos);
682 label =
Replace(label,
"<<runLength>>", Form(
"%5.2lf", runLength), pos);
684 label =
Replace(label,
"<<meanRate>>", Form(
"%5.2lf", meanRate), pos);
688 TLatex* texxt =
new TLatex(
fPanels[n].posX[m],
fPanels[n].posY[m], label.c_str());
689 texxt->SetTextColor(1);
690 texxt->SetTextSize(
fPanels[n].font_size);
698 vector<TGraph*> graphCollectionAll;
699 for (
unsigned int n = 0; n <
fPlots.size(); n++) {
703 targetPad->SetLogx(plot.
logX);
704 targetPad->SetLogy(plot.
logY);
705 targetPad->SetLeftMargin(0.18);
706 targetPad->SetRightMargin(0.1);
707 targetPad->SetBottomMargin(0.15);
708 targetPad->SetTopMargin(0.07);
711 if (plot.
graphs[0].drawOption.find(
"A") == string::npos)
712 plot.
graphs[0].drawOption = plot.
graphs[0].drawOption +
"A";
715 vector<TGraph*> graphCollectionPlot;
716 for (
unsigned int i = 0; i < plot.
graphs.size(); i++) {
719 TString graphName = graph.
name;
724 cout <<
"--------------------------------------" << endl;
725 cout <<
"Graph name : " << graphName << endl;
726 cout <<
"Graph variable : " << graphVariable << endl;
727 cout <<
"++++++++++++++++++++++++++++++++++++++" << endl;
731 Bool_t skipZeroData =
true;
733 std::vector<Double_t> xData;
734 std::vector<Double_t> yData;
735 std::map<Double_t, Double_t> dataMap;
736 std::map<Double_t, Int_t> dataMapN;
748 run->SetHistoricMetadataSaving(
false);
760 if (yVal == 0 && skipZeroData) {
762 <<
"Ignoring zero data. This message could be disabled with variable skipZeroData"
770 Double_t startTimeStamp = run->GetStartTimestamp();
771 Double_t endTimeStamp = run->GetEndTimestamp();
773 Double_t meanTime = (endTimeStamp + startTimeStamp) / 2.;
774 xData.push_back(meanTime);
775 yData.push_back(yVal);
778 if (dataMap.find(xVal) == dataMap.end()) {
779 dataMap[xVal] = yVal;
782 dataMap[xVal] += yVal;
790 for (
auto const& x : dataMap) {
791 xData.push_back(x.first);
792 yData.push_back(dataMap[x.first] / dataMapN[x.first]);
796 if (xData.size() == 0) {
797 RESTWarning <<
"TRestMetadataPlot: no input file matches condition for graph: " << graph.
name
799 RESTWarning <<
"This graph is empty and it will not be plotted" <<
RESTendl;
804 if (xData.size() == 1) {
805 RESTWarning <<
"TRestMetadataPlot: Only 1-point for graph: " << graph.
name <<
RESTendl;
806 RESTWarning <<
"X: " << xData[0] <<
" Y: " << yData[0] <<
RESTendl;
809 for (
unsigned int nn = 0; nn < xData.size(); nn++)
810 RESTDebug <<
"X: " << xData[nn] <<
" Y: " << yData[nn] <<
RESTendl;
813 TGraph* gr_temp =
new TGraph(xData.size(), &xData[0], &yData[0]);
814 gr_temp->SetName(graph.
name.c_str());
815 gr_temp->SetTitle(plot.
title.c_str());
817 gr_temp->GetXaxis()->SetTitle(plot.
labelX.c_str());
818 gr_temp->GetYaxis()->SetTitle(plot.
labelY.c_str());
823 gr_temp->GetXaxis()->SetLabelSize(
fTicksScaleX * gr_temp->GetXaxis()->GetLabelSize());
824 gr_temp->GetYaxis()->SetLabelSize(
fTicksScaleY * gr_temp->GetYaxis()->GetLabelSize());
825 gr_temp->GetXaxis()->SetTitleSize(
fLabelScaleX * gr_temp->GetXaxis()->GetTitleSize());
826 gr_temp->GetYaxis()->SetTitleSize(
fLabelScaleY * gr_temp->GetYaxis()->GetTitleSize());
827 gr_temp->GetXaxis()->SetTitleOffset(
fLabelOffsetX * gr_temp->GetXaxis()->GetTitleOffset());
828 gr_temp->GetYaxis()->SetTitleOffset(
fLabelOffsetY * gr_temp->GetYaxis()->GetTitleOffset());
837 gr_temp->SetDrawOption(graph.
drawOption.c_str());
842 if (plot.
timeDisplay) gr_temp->GetXaxis()->SetTimeDisplay(1);
844 graphCollectionPlot.push_back(gr_temp);
845 graphCollectionAll.push_back(gr_temp);
848 if (graphCollectionPlot.size() == 0) {
849 RESTWarning <<
"TRestMetadataPlot: pad empty for the plot: " << plot.
name <<
RESTendl;
854 Double_t minValue_Pad = 0;
855 Double_t maxValue_Pad = 0;
856 for (
unsigned int i = 0; i < graphCollectionPlot.size(); i++) {
857 TGraph* gr = graphCollectionPlot[i];
859 Double_t maxValue = TMath::MaxElement(gr->GetN(), gr->GetY());
860 Double_t minValue = TMath::MinElement(gr->GetN(), gr->GetY());
862 maxValue_Pad = maxValue;
863 minValue_Pad = minValue;
865 if (maxValue > maxValue_Pad) maxValue_Pad = maxValue;
866 if (minValue < minValue_Pad) minValue_Pad = minValue;
871 if (minValue_Pad > 0)
876 if (maxValue_Pad > 0)
882 if (!(plot.
yRange.X() == -1 && plot.
yRange.Y() == -1)) {
883 minValue_Pad = plot.
yRange.X();
884 maxValue_Pad = plot.
yRange.Y();
887 for (
unsigned int i = 0; i < graphCollectionPlot.size(); i++) {
890 graphCollectionPlot[i]->GetXaxis()->SetLimits(plot.
xRange.X(), plot.
xRange.Y());
891 graphCollectionPlot[i]->GetHistogram()->SetMinimum(minValue_Pad);
892 graphCollectionPlot[i]->GetHistogram()->SetMaximum(maxValue_Pad);
894 graphCollectionPlot[i]->Draw(plot.
graphs[i].drawOption.c_str());
898 for (
unsigned int i = 0; i < graphCollectionPlot.size(); i++) {
899 if (
fRun !=
nullptr) {
900 fOutputRootFile->cd();
901 graphCollectionPlot[i]->Write();
908 for (
unsigned int i = 0; i < graphCollectionPlot.size(); i++)
910 legend->Draw(
"same");
915 if (plot.
save !=
"") {
923 if (StringToBool(
GetParameter(
"previewPlot",
"TRUE"))) {
929 run->SetHistoricMetadataSaving(
false);
940 for (
unsigned int n = 0; n < graphCollectionAll.size(); n++) graphCollectionAll[n]->
Write();
945 if (
fRun !=
nullptr && fOutputRootFile !=
nullptr) {
946 fOutputRootFile->cd();
974 TCanvas* c =
new TCanvas(
fPlots[n - 1].name.c_str(),
fPlots[n - 1].name.c_str(), 800, 600);
992 string name =
fPlots[nPlot].graphs[nGraph].name;
993 TGraph* graph = (TGraph*)gPad->GetPrimitive(name.c_str());
995 TCanvas* c =
new TCanvas(name.c_str(), name.c_str(), 800, 600);
Data provider and manager in REST.
void OpenInputFile(int i)
Open the i th file in the file list.
void CloseFile()
Close both input file and output file, setting trees to nullptr also.
TFile * FormOutputFile()
Create a new TFile as REST output file. Writing metadata objects into it.
TString FormFormat(const TString &filenameFormat)
Form output file name according to file info list, proc info list and run data.
std::string ReplaceMetadataMembers(const std::string &instr, Int_t precision=8)
It will replace the data members contained inside the string given as input. The data members in the ...
Bool_t EvaluateMetadataMember(const std::string &instr)
It will evaluate the expression given including the data member from the corresponding metadata class...
@ REST_Extreme
show everything
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Double_t StringToDouble(std::string in)
Gets a double from a string.
std::string ToUpper(std::string in)
Convert string to its upper case. Alternative of TString::ToUpper.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.
std::string ToDateTimeString(time_t time)
Format time_t into string.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.