-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathtestGraphics.C
More file actions
368 lines (312 loc) · 13.5 KB
/
Copy pathtestGraphics.C
File metadata and controls
368 lines (312 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Author: Adrian Düsselberg CERN 07/2024
/*************************************************************************
* Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "TROOT.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TPaveLabel.h"
#include "TPaveText.h"
#include "TArrow.h"
#include "TLine.h"
#include "TString.h"
#include "TApplication.h"
#include "TWebCanvas.h"
#include "TLatex.h"
#include <TSystem.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <string>
#include <sstream>
#include <tuple>
//---------------------HELPER--------------------------------------------------------------
// Function to read file content into a string
std::string readFileToString(const std::string& filePath) {
std::ifstream file(filePath);
if (!file) {
throw std::runtime_error("Could not open file: " + filePath);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
std::vector<std::string> splitString(const std::string &str, const char delimiter = '\n')
{
auto lines = std::vector<std::string>{};
lines.reserve(128);
auto strAsStream = std::stringstream{str};
for (std::string line; std::getline(strAsStream, line, delimiter);) {
lines.emplace_back(line);
}
return lines;
}
void printSideBySide(const std::string &str1, const std::string &str2)
{
auto lines1 = splitString(str1);
auto lines2 = splitString(str2);
unsigned int idx1 = 0;
unsigned int idx2 = 0;
for (auto [idx1, idx2] = std::tuple<unsigned int, unsigned int>{0, 0};
idx1 != lines1.size() || idx2 != lines2.size();) {
auto &line1 = lines1[idx1];
auto &line2 = lines2[idx2];
if (line1 != line2) {
std::cout << idx1 << ", " << idx2 << ": " << line1 << " <-> " << line2 << std::endl;
}
if (idx1 < lines1.size())
++idx1;
if (idx2 < lines2.size())
++idx2;
}
}
//---------------------JSON----------------------------------------------------------------
std::string preprocessJSONContent(const std::string& jsonString) {
std::string result = jsonString;
// Remove fTsumwx key
std::regex fTsumwxRegex(R"("fTsumwx" : \d+\.\d+,)");
result = std::regex_replace(result, fTsumwxRegex, "");
// Remove fTsumwx2 key
std::regex fTsumwx2Regex(R"("fTsumwx2" : \d+\.\d+,)");
result = std::regex_replace(result, fTsumwx2Regex, "");
return result;
}
bool compare_json(const TString& jsonOutput, const std::string& ref_filename, const std::string& macroName) {
// Read the reference JSON content from file
std::ifstream refFile(ref_filename);
if (!refFile.is_open()) {
std::string jsonFilePath = "./json_ref/" + macroName + ".json";
std::cerr << "Failed to open reference JSON file: " << ref_filename << "\nCreating new reference JSON: " << jsonFilePath << std::endl;
std::ofstream jsonFile(jsonFilePath);
if (!jsonFile.is_open()) {
throw std::runtime_error("Error: Unable to open file for writing:" + jsonFilePath);
return false;
}
jsonFile << jsonOutput.Data();
return true;
}
std::stringstream refBuffer;
refBuffer << refFile.rdbuf();
// Remove specific lines from both JSON strings
std::string produced_json = preprocessJSONContent(jsonOutput.Data());
std::string reference_json = preprocessJSONContent(refBuffer.str());
// Compare the created JSON to the reference JSON
std::cerr << "Length of produced JSON after adjustments: " << jsonOutput.Length() << std::endl;
std::cerr << "Length of reference JSON after adjustments: " << refBuffer.str().length() << std::endl;
const auto areEqual = produced_json == reference_json;
if (!areEqual) printSideBySide(produced_json, reference_json);
return areEqual;
}
int test_json(TCanvas* c1, const std::string& macroName, const std::string& builddir) {
// Create JSON output from canvas
TString jsonOutput = TWebCanvas::CreateCanvasJSON(c1, 1, kFALSE);
// Path to the reference and produced JSON file
std::string ref_filename = "./json_ref/" + macroName + ".json";
std::string jsonFilePath = builddir + "/json_pro/" + macroName + "_pro.json";
FileStat_t fstat;
if (1 == gSystem->GetPathInfo(ref_filename.c_str(), fstat)) {
std::cout << "Reference JSON file not found. Saving generated JSON file as reference." << std::endl;
// Save the generated JSON file as reference
std::ofstream jsonFile(ref_filename);
if (jsonFile.is_open()) {
jsonFile << jsonOutput.Data();
jsonFile.close();
return 1;
} else {
std::cerr << "Error: Unable to open file for writing: " << ref_filename << std::endl;
return 1;
}
} else {
// Save the generated JSON file
std::ofstream jsonFile(jsonFilePath);
if (jsonFile.is_open()) {
jsonFile << jsonOutput.Data();
jsonFile.close();
} else {
std::cerr << "Error: Unable to open file for writing: " << jsonFilePath << std::endl;
return 1;
}
}
// Compare the created JSON to the reference JSON
if (compare_json(jsonOutput, ref_filename, macroName)) {
std::cout << "JSON test passed for " << macroName << std::endl;
} else {
std::cerr << "JSON test failed for " << macroName << std::endl;
return 1;
}
return 0;
}
//---------------------SVG-----------------------------------------------------------------
// Function to remove or normalize specific parts of the SVG content
std::string preprocessSVGContent(const std::string& svgContent) {
std::string result = svgContent;
// Remove <title>...</title> sections
std::regex titleRegex(R"(<title>(.|\n)*?<\/title>)");
result = std::regex_replace(result, titleRegex, "");
// Remove <desc>...</desc> sections
std::regex descRegex(R"(<desc>(.|\n)*?<\/desc>)");
result = std::regex_replace(result, descRegex, "");
return result;
}
// Function to compare two SVG files (old graphics)
bool compareSVGFiles(const std::string& filePath1, const std::string& filePath2) {
try {
std::string content1 = readFileToString(filePath1);
std::string content2 = readFileToString(filePath2);
content1 = preprocessSVGContent(content1);
content2 = preprocessSVGContent(content2);
// Compare the created SVG to the reference SVG
std::cerr << "Length of produced SVG after adjustments: " << content1.length() << std::endl;
std::cerr << "Length of reference SVG after adjustments: " << content2.length() << std::endl;
const auto areEqual = content1 == content2;
if (!areEqual) printSideBySide(content1, content2);
return areEqual;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return false;
}
}
int test_svg(TCanvas* c1, const std::string& macroName, const std::string& builddir) {
// Paths to the reference and generated SVG files
std::string refFilePath = "./old_svg_ref/" + macroName + ".svg";
std::string genFilePath = builddir + "/old_svg_pro/" + macroName + "_pro.svg";
// Check if the reference file exists
FileStat_t fstat;
if (1 == gSystem->GetPathInfo(refFilePath.c_str(), fstat)) {
std::cout << "Reference file not found. Saving generated file as reference: " << macroName << std::endl;
c1->SaveAs(refFilePath.c_str());
return 1;
} else {
// Save the generated SVG file
c1->SaveAs(genFilePath.c_str());
}
// Compare the generated SVG file with the reference SVG file
if (compareSVGFiles(refFilePath, genFilePath)) {
std::cout << "SVG test passed for " << macroName << std::endl;
} else {
std::cout << "SVG test failed for " << macroName << std::endl;
return 1;
}
return 0;
}
//---------------------PDF-----------------------------------------------------------------
std::string preprocessPDFContent(const std::string& pdfString) {
std::string result = pdfString;
// Regular expressions to match the metadata patterns
std::regex creationDatePattern(R"(/CreationDate \(D:\d{14}.*?\))");
std::regex modDatePattern(R"(/ModDate \(D:\d{14}[-+Z].*?\))");
std::regex titlePattern(R"(/Title \(.*?\))");
// Regular expressions to match the cross-reference table and trailer section
std::regex xrefPattern(R"(xref[\s\S]*?EOF)");
// Remove the matched patterns from the content
result = std::regex_replace(result, creationDatePattern, "");
result = std::regex_replace(result, modDatePattern, "");
result = std::regex_replace(result, titlePattern, "");
result = std::regex_replace(result, xrefPattern, "");
return result;
}
// Function to compare two PDF files (old graphics)
bool comparePDFFiles(const std::string& filePath1, const std::string& filePath2) {
try {
std::string content1 = readFileToString(filePath1);
std::string content2 = readFileToString(filePath2);
content1 = preprocessPDFContent(content1);
content2 = preprocessPDFContent(content2);
// Compare the created JSON to the reference JSON
std::cerr << "Length of produced PDF after adjustments: " << content1.length() << std::endl;
std::cerr << "Length of reference PDF after adjustments: " << content2.length() << std::endl;
const auto areEqual = content1 == content2;
if (!areEqual) printSideBySide(content1, content2);
return areEqual;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return false;
}
}
int test_pdf(TCanvas* c1, const std::string& macroName, const std::string& buildir) {
// Paths to the reference and generated SVG files
std::string refFilePath = "./pdf_ref/" + macroName + ".pdf";
std::string genFilePath = buildir + "/pdf_pro/" + macroName + "_pro.pdf";
// Check if the reference file exists
FileStat_t fstat;
if (1 == gSystem->GetPathInfo(refFilePath.c_str(), fstat)) {
std::cout << "Reference file not found. Saving generated file as reference: " << macroName << std::endl;
c1->SaveAs(refFilePath.c_str());
return 1;
} else {
// Save the generated PDF file
c1->SaveAs(genFilePath.c_str());
}
// Compare the generated PDF file with the reference PDF file
if (comparePDFFiles(refFilePath, genFilePath)) {
std::cout << "PDF test passed for " << macroName << std::endl;
} else {
std::cout << "PDF test failed for " << macroName << std::endl;
return 1;
}
return 0;
}
// TEST ROOT MACRO -------------------------------------------------------------------------------
int testGraphics(const std::string& macroName, const std::string& test_type, const std::string& macro_folder, const std::string& builddir) {
// Set paths
std::string macroPath = macro_folder + "/" + macroName + ".C";
gROOT->SetMacroPath("./macros");
gROOT->SetStyle("ATLAS");
// Set statistics box parameters explicitly
gStyle->SetStatX(0.7); // X position of stat box
gStyle->SetStatY(0.8); // Y position of stat box
gStyle->SetStatW(0.2); // Width of stat box
gStyle->SetStatH(0.1); // Height of stat box
gStyle->SetStatFormat("6.4g"); // Number format in stat box
gStyle->SetStatFont(42); // Font type for stat box (Helvetica)
gStyle->SetStatFontSize(0.025); // Font size for stat box
gStyle->SetStatColor(0); // Background color of stat box
gStyle->SetStatTextColor(1); // Text color of stat box
gStyle->SetStatBorderSize(1); // Border size of stat box
// Call the macro to generate the canvas
std::string command = ".x " + macroPath;
gROOT->ProcessLine(command.c_str());
// Retrieve the current canvas from gPad
auto c1 = dynamic_cast<TCanvas*>(gPad->GetCanvas());
if (!c1) {
std::cerr << "Error: No canvas found in gPad" << std::endl;
return 1;
}
// Different tests for root graphics
// a == invokes all tests
// j === test the JSON creation
// o === test the SVG creation in ROOT (old graphics with --web=off)
// p == test the PDF creation in ROOT (old graphics with --web=off)
if (test_type == "j") {
return test_json(c1, macroName, builddir);
}
if (test_type == "o") {
gROOT->SetWebDisplay("off");
return test_svg(c1, macroName, builddir);
}
if (test_type == "p") {
gROOT->SetWebDisplay("off");
return test_pdf(c1, macroName, builddir);
}
std::cerr << "Unrecognised test type '" << test_type << "'" << std::endl;
return 1;
}
int main(int argc, char** argv) {
if (argc != 5) {
std::cerr << "Usage: " << argv[0] << " <macro_name> <test_type> <macro_folder> <builddir>" << std::endl;
return 1;
}
std::string macroName = argv[1];
std::string test_type = argv[2];
std::string macro_folder = argv[3];
std::string builddir = argv[4];
return testGraphics(macroName, test_type, macro_folder, builddir);
}