File indexing completed on 2024-04-06 12:01:12
0001 #include <iostream>
0002 #include <fstream>
0003 #include <string>
0004 #include <vector>
0005 #include "TCanvas.h"
0006 #include "TPad.h"
0007 #include "TLatex.h"
0008 #include "TArrow.h"
0009 #include "TGaxis.h"
0010 #include "TPolyLine.h"
0011 #include "TColor.h"
0012 #include <map>
0013 #include "TStyle.h"
0014 #include "TROOT.h"
0015
0016 TCanvas *canvas;
0017 float minvalue;
0018 float maxvalue;
0019 std::vector<TPolyLine*> vp;
0020 TStyle style;
0021
0022 static int width=3200;
0023 static int height=1600;
0024 float hmin,hmax;
0025
0026 struct Data{
0027 std::vector<short> rgb;
0028 std::vector<float> points;
0029 float value;
0030
0031 int getColorIndex() const {
0032 return rgb[0]+rgb[1]*1000+rgb[2]*1000000+10000000;
0033 }
0034
0035 void print() const{
0036 std::cout << "rgb ";
0037 for(size_t i=0;i<rgb.size();++i)
0038 std::cout << rgb[i] << " ";
0039 std::cout << std::endl;
0040
0041 std::cout << "point ";
0042 for(size_t i=0;i<points.size();++i)
0043 std::cout << points[i] << " ";
0044 std::cout << std::endl;
0045
0046 std::cout << "value " << value << std::endl;
0047 }
0048 };
0049 std::vector<Data> DataVect;
0050
0051
0052
0053 struct ColorList{
0054
0055 size_t getIndex(const Data& data){
0056 int idx=data.getColorIndex();
0057 std::vector<int>::iterator p=std::lower_bound(ColIndex.begin(),ColIndex.end(),idx);
0058 return index[p-ColIndex.begin()];
0059 }
0060
0061 void put(const Data& data){
0062 int idx=data.getColorIndex();
0063 std::vector<int>::iterator p=std::lower_bound(ColIndex.begin(),ColIndex.end(),idx);
0064 if(p!=ColIndex.end() && *p==idx)
0065 return;
0066 int id=1000+ColIndex.size()+1;
0067 TColor *c=new TColor(id,
0068 data.rgb[0]/255.,
0069 data.rgb[1]/255.,
0070 data.rgb[2]/255.
0071 );
0072
0073 index.insert(index.begin()+(p-ColIndex.begin()),id);
0074 pool.insert(pool.begin()+(p-ColIndex.begin()),c);
0075 ColIndex.insert(p,idx);
0076
0077 }
0078
0079 void setPalette(){
0080 int palette[index.size()];
0081 for(size_t i=0;i<index.size();++i){
0082 palette[i]=index[i];
0083 }
0084
0085 gStyle->SetPalette(index.size(),palette);
0086 }
0087
0088 std::vector<int> index;
0089 std::vector<int> ColIndex;
0090 std::vector<TColor*> pool;
0091 };
0092
0093 ColorList colorList;
0094
0095
0096
0097 int checkLine(std::string & line){
0098 if(line.find("svg:polygon detid")!=std::string::npos)
0099 return 1;
0100 else if(line.find("svg:rect x")!=std::string::npos)
0101 return 2;
0102
0103 return 0;
0104 }
0105
0106
0107
0108 std::vector<short> getRGB(std::string& line){
0109 std::vector<short> col(3,255);
0110 size_t first=line.find("fill=\"");
0111 size_t fillLenght=6;
0112 size_t second=line.find(" ",first);
0113 std::string scolor=line.substr(first+fillLenght,second-first-fillLenght-1);
0114 if (scolor.find("rgb")!=std::string::npos){
0115 size_t r=scolor.find("(")+1;
0116 size_t g=scolor.find(",",r+1)+1;
0117 size_t b=scolor.find(",",g+1)+1;
0118 size_t e=scolor.find(")");
0119
0120 col[0]=atoi(scolor.substr(r,g-r-1).c_str());
0121 col[1]=atoi(scolor.substr(g,b-g-1).c_str());
0122 col[2]=atoi(scolor.substr(b,e-b).c_str());
0123
0124 }
0125 return col;
0126 }
0127
0128
0129
0130 void getPair(std::string line, std::vector<float>& out){
0131 size_t a=line.find(",");
0132 if(a==std::string::npos)
0133 return;
0134 out.push_back(atof(line.substr(0,a).c_str()));
0135 out.push_back(atof(line.substr(a+1).c_str()));
0136 }
0137
0138
0139
0140 std::vector<float> getPoints(std::string& line){
0141 std::vector<float> points;
0142
0143 size_t first=line.find("points=\"");
0144 size_t fillLenght=8;
0145 size_t second=line.find("\"",first);
0146 std::string spoints=line.substr(first+fillLenght,second-first-fillLenght-1);
0147 size_t p1=0;
0148 size_t p2=spoints.find(" ",0)+1;
0149 size_t p3=spoints.find(" ",p2)+1;
0150 size_t p4=spoints.find(" ",p3)+1;
0151
0152 getPair(spoints.substr(p1,p2-p1-1),points);
0153 getPair(spoints.substr(p2,p3-p2-1),points);
0154 getPair(spoints.substr(p3,p4-p3-1),points);
0155 getPair(spoints.substr(p4),points);
0156
0157 return points;
0158 }
0159
0160
0161
0162 std::vector<float> getRect(std::string& line){
0163 std::vector<float> points;
0164
0165 size_t fillLenght;
0166 size_t first=line.find("x=\"");
0167 fillLenght=3;
0168 size_t second=line.find("\"",first);
0169 float x= atof(line.substr(first+fillLenght,second-first-fillLenght-1).c_str());
0170
0171 first=line.find("y=\"");
0172 fillLenght=3;
0173 second=line.find("\"",first);
0174 float y= atof(line.substr(first+fillLenght,second-first-fillLenght-1).c_str());
0175 y=height-y;
0176
0177 first=line.find("width=\"");
0178 fillLenght=7;
0179 second=line.find("\"",first);
0180 float w= atof(line.substr(first+fillLenght,second-first-fillLenght-1).c_str());
0181
0182 first=line.find("height=\"");
0183 fillLenght=8;
0184 second=line.find("\"",first);
0185 float h= atof(line.substr(first+fillLenght,second-first-fillLenght-1).c_str());
0186
0187 if(hmin>y-h)
0188 hmin=y-h;
0189 if(hmax<y)
0190 hmax=y;
0191
0192 points.push_back(y);
0193 points.push_back(x);
0194
0195 points.push_back(y-h);
0196 points.push_back(x);
0197
0198 points.push_back(y-h);
0199 points.push_back(x+w);
0200
0201 points.push_back(y);
0202 points.push_back(x+w);
0203
0204 return points;
0205 }
0206
0207
0208
0209 float getValue(std::string& line){
0210
0211 size_t first=line.find("value=\"");
0212 size_t fillLenght=7;
0213 size_t second=line.find("\"",first);
0214 return atof(line.substr(first+fillLenght,second-first-fillLenght-1).c_str());
0215 }
0216
0217
0218
0219 void parseSVG(char* inputFile){
0220
0221 Data data;
0222 std::ifstream inFile(inputFile, std::ios::in);
0223 std::string line;
0224 int check=0;
0225 while(std::getline(inFile,line)){
0226 check=checkLine(line);
0227 if(check>0){
0228
0229 data.rgb=getRGB(line);
0230 if(check==1)
0231 data.points=getPoints(line);
0232 else
0233 data.points=getRect(line);
0234
0235 data.value=getValue(line);
0236 DataVect.push_back(data);
0237
0238
0239 }
0240 }
0241 }
0242
0243
0244
0245 void createPolyline(const Data& data){
0246
0247 double x[data.points.size()],y[data.points.size()];
0248 for(size_t i=0;i<data.points.size()/2;i++){
0249 x[i]=data.points[2*i];
0250 y[i]=data.points[2*i+1];
0251 }
0252 TPolyLine* pline = new TPolyLine(data.points.size()/2,y,x);
0253 vp.push_back(pline);
0254 pline->SetFillColor(colorList.getIndex(data));
0255 pline->SetLineWidth(0);
0256 pline->Draw("f");
0257 }
0258
0259
0260
0261 void drawTkMap(){
0262
0263 std::vector<Data>::const_iterator iter=DataVect.begin();
0264 std::vector<Data>::const_iterator iterE=DataVect.end();
0265 for(;iter!=iterE;++iter)
0266 createPolyline(*iter);
0267
0268 }
0269
0270
0271
0272 void setMinMax(){
0273
0274 minvalue=0;
0275 maxvalue=50;
0276 };
0277
0278
0279
0280 void getColorScale(){
0281
0282 std::vector<Data>::const_iterator iter=DataVect.begin();
0283 std::vector<Data>::const_iterator iterE=DataVect.end();
0284
0285 for(;iter!=iterE;++iter)
0286 colorList.put(*iter);
0287 }
0288
0289
0290
0291 void drawLabels(){
0292 TLatex l;
0293 l.SetTextSize(0.04);
0294 l.DrawLatex(500,50,"-z");
0295 l.DrawLatex(500,1430,"+z");
0296 l.DrawLatex(900,330,"TIB L1");
0297 l.DrawLatex(900,1000,"TIB L2");
0298 l.DrawLatex(1300,330,"TIB L3");
0299 l.DrawLatex(1300,1000,"TIB L4");
0300 l.DrawLatex(1700,330,"TOB L1");
0301 l.DrawLatex(1700,1000,"TOB L2");
0302 l.DrawLatex(2100,330,"TOB L3");
0303 l.DrawLatex(2100,1000,"TOB L4");
0304 l.DrawLatex(2500,330,"TOB L5");
0305 l.DrawLatex(2500,1000,"TOB L6");
0306 TArrow arx(2900,1190,2900,1350,0.01,"|>");
0307 l.DrawLatex(2915,1350,"x");
0308 TArrow ary(2900,1190,2790,1190,0.01,"|>");
0309 l.DrawLatex(2790,1210,"y");
0310 TArrow arz(2790,373,2790,672,0.01,"|>");
0311 l.DrawLatex(2820,667,"z");
0312 TArrow arphi(2790,511,2447,511,0.01,"|>");
0313 l.DrawLatex(2433,520,"#Phi");
0314 arx.SetLineWidth(3);
0315 ary.SetLineWidth(3);
0316 arz.SetLineWidth(3);
0317 arphi.SetLineWidth(3);
0318 arx.Draw();
0319 ary.Draw();
0320 arz.Draw();
0321 arphi.Draw();
0322
0323
0324 TGaxis* axis = new TGaxis(3060,hmin,3060,hmax,0,100,510,"+L");
0325 axis->SetLabelSize(0.02);
0326 axis->Draw();
0327
0328 canvas->Update();
0329 }
0330
0331
0332
0333 void save(char* outputFile){
0334 canvas->Print(outputFile);
0335 }
0336
0337
0338
0339 void createCanvas(){
0340 canvas= new TCanvas("canvas", "TrackerMap",width,height);
0341 gPad->SetFillColor(38);
0342 gPad->Range(0,0,width,height);
0343
0344 getColorScale();
0345 drawTkMap();
0346 drawLabels();
0347
0348 }
0349
0350
0351
0352 int svgTopng(char* inputFile,char* outputFile){
0353
0354 hmin=99999999; hmax=0;
0355
0356 parseSVG(inputFile);
0357
0358 createCanvas();
0359
0360 save(outputFile);
0361
0362
0363 return 0;
0364 }
0365
0366 int main(int argc, char *argv[]){
0367
0368 char *inputFile, *outputFile;
0369 if(argc>1)
0370 inputFile=argv[1];
0371 if(argc>2)
0372 outputFile=argv[2];
0373
0374 return svgTopng(inputFile,outputFile);
0375 }
0376