source: trunk/source/digits_hits/utils/src/G4ScoreLogColorMap.cc @ 1348

Last change on this file since 1348 was 1340, checked in by garnier, 14 years ago

update ti head

File size: 8.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4ScoreLogColorMap.cc,v 1.10 2010/11/03 19:31:36 asaim Exp $
28// GEANT4 tag $Name:  $
29//
30
31#include "G4ScoreLogColorMap.hh"
32#include <cmath>
33#include <string>
34#include <sstream>
35#include <iomanip>
36
37
38#include "G4VVisManager.hh"
39#include "G4VisAttributes.hh"
40#include "G4Text.hh"
41#include "G4Circle.hh"
42#include "G4Polyline.hh"
43#include "G4Colour.hh"
44
45G4ScoreLogColorMap::G4ScoreLogColorMap(G4String mName)
46:G4VScoreColorMap(mName)
47{;}
48
49G4ScoreLogColorMap::~G4ScoreLogColorMap()
50{;}
51
52#include "G4UIcommand.hh"
53void G4ScoreLogColorMap::GetMapColor(G4double val, G4double color[4])
54{
55  G4bool lmin = true, lmax = true, lval = true;
56  if(fMinVal < 0.) {
57    lmin = false;
58    G4String message = "    The min. value (fMinVal) is negative. : ";
59    message += G4UIcommand::ConvertToString(fMinVal);
60    G4Exception("G4ScoreLogColorMap::GetMapColor()",
61                "DigiHitsUtilsScoreLogColorMap000", JustWarning,
62                message);
63  }
64  if(fMaxVal < 0.) {
65    lmax = false;
66    G4String message = "    The max. value (fMaxVal) is negative. : ";
67    message += G4UIcommand::ConvertToString(fMaxVal);
68    G4Exception("G4ScoreLogColorMap::GetMapColor()",
69                "DigiHitsUtilsScoreLogColorMap001", JustWarning,
70                message);
71  }
72  if(!lmin || !lmax) {
73    color[0] = 0.;
74    color[1] = 0.;
75    color[2] = 0.;
76    color[3] = 0.;
77    return;
78  }
79
80  if(val < 0.) {
81    lval = false;
82    G4String message = "     'val' (first argument) is negative : ";
83    message += G4UIcommand::ConvertToString(fMaxVal);
84    G4Exception("G4ScoreLogColorMap::GetMapColor()",
85                "DigiHitsUtilsScoreLogColorMap002", JustWarning,
86                message);
87  }
88  if(!lval) {
89    color[0] = 0.;
90    color[1] = 0.;
91    color[2] = 0.;
92    color[3] = -1.;
93    return;
94  }
95
96  G4double logmin = 0., logmax = 0., logval = 0.;
97  if(lmin) logmin = std::log10(fMinVal);
98  if(lmax) logmax = std::log10(fMaxVal);
99  if(lval) logval = std::log10(val);
100  G4double value = 0.;
101  if(lmax) value = (logval-logmin)/(logmax-logmin);
102
103  if(value > 1.) {value=1.;}
104  if(value < 0.) {value=0.;}
105
106  // color map
107  const int NCOLOR = 6;
108  struct ColorMap {
109    G4double val;
110    G4double rgb[4];
111  } colormap[NCOLOR] = {{0.0, {1., 1., 1., 1.}}, // value, r, g, b, alpha
112                        {0.2, {0., 0., 1., 1.}},
113                        {0.4, {0., 1., 1., 1.}},
114                        {0.6, {0., 1., 0., 1.}},
115                        {0.8, {1., 1., 0., 1.}},
116                        {1.0, {1., 0., 0., 1.}}};
117 
118  // search
119  G4int during[2] = {0, 0};
120  for(int i = 1; i < NCOLOR; i++) {
121    if(colormap[i].val >= value) {
122      during[0] = i-1;
123      during[1] = i;
124      break;
125    }
126  }
127
128  // interpolate
129  G4double a = std::fabs(value - colormap[during[0]].val);
130  G4double b = std::fabs(value - colormap[during[1]].val);
131  for(int i = 0; i < 4; i++) {
132    color[i] = (b*colormap[during[0]].rgb[i] + a*colormap[during[1]].rgb[i])
133      /(colormap[during[1]].val - colormap[during[0]].val);
134  } 
135
136}
137
138
139void G4ScoreLogColorMap::DrawColorChartBar(G4int _nPoint) {
140
141  G4cout << "++++++ " << fMinVal << " - " << fMaxVal << G4endl;
142  G4bool lmin = true, lmax = true;
143  if(fMinVal <= 0.) lmin = false;
144  if(fMaxVal <= 0.) lmax = false;
145  G4double min = 0.;
146  if(lmin) min = std::log10(fMinVal);
147  G4double max = 0.;
148  if(lmax) max = std::log10(fMaxVal);
149
150  G4double smin = -0.89, smax = smin + 0.05*(_nPoint)*0.83, step=0.001;
151  G4double c[4];
152  for(G4double y = smin; y < smax; y+=step) {
153    G4double ra = (y-smin)/(smax-smin), rb = 1.-ra;
154    G4Polyline line;
155    line.push_back(G4Point3D(-0.96, y, 0.));
156    line.push_back(G4Point3D(-0.91, y, 0.));
157    G4double val = std::pow(10., (ra*max+rb*min)/(ra+rb));
158    this->GetMapColor(val, c);
159    if(c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == 0) return;
160    if(c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == -1.) continue;
161    G4Colour col(c[0], c[1], c[2]);
162    G4VisAttributes att(col);
163    line.SetVisAttributes(&att);
164    fVisManager->Draw2D(line);
165  }
166
167}
168void G4ScoreLogColorMap::DrawColorChartText(G4int _nPoint) {
169  G4bool lmin = true, lmax = true;
170  if(fMinVal <= 0.) lmin = false;
171  if(fMaxVal <= 0.) lmax = false;
172
173  G4double min = 0.;
174  if(lmin) min = std::log10(fMinVal);
175  //if(min > 0.) min = std::floor(min);
176  //else min = std::ceil(min);
177
178  G4double max = 0.;
179  if(lmax) max = std::log10(fMaxVal);
180  //if(max > 0.) max = std::ceil(max);
181  //else max = std::floor(max);
182
183  G4double c[4];
184  G4Colour black(0., 0., 0.);
185  for(int n = 0; n < _nPoint; n++) {
186    G4double a = n/(_nPoint-1.), b = 1.-a;
187    G4double v = (a*max + b*min)/(a+b);
188
189    this->GetMapColor(std::pow(10., v), c);
190    if(c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == 0) return;
191    if(c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == -1.) continue;
192
193    // background color
194    for(int l = 0; l < 21; l++) {
195      G4Polyline line;
196      line.push_back(G4Point3D(-0.908, -0.905+0.05*n+0.002*l, 0.));
197      line.push_back(G4Point3D(-0.705, -0.905+0.05*n+0.002*l, 0.));
198      G4VisAttributes attblack(black);
199      line.SetVisAttributes(&attblack);
200      fVisManager->Draw2D(line);
201    }
202    // text
203    //char cstring[80];
204    //std::sprintf(cstring, "%8.1e", std::pow(10., v));
205    //G4String value(cstring);
206    std::ostringstream oss;
207    oss << std::setw(8) << std::setprecision(1) << std::scientific << std::pow(10., v);
208    std::string str = oss.str();
209    G4String value(str.c_str());
210    G4Text text(value, G4Point3D(-0.9, -0.9+0.05*n, 0));
211    G4double size = 12.;
212    text.SetScreenSize(size);
213    //this->GetMapColor(std::pow(10., v), c);
214    G4Colour color(c[0], c[1], c[2]);
215    G4VisAttributes att(color);
216    text.SetVisAttributes(&att);
217
218    fVisManager->Draw2D(text);
219  }
220
221
222  // draw ps name
223  // background
224  G4int lpsname = 20;// fPSName.size();
225  if(lpsname > 0) {
226    for(int l = 0; l < 22; l++) {
227      G4Polyline line;
228      line.push_back(G4Point3D(-0.9, -0.965+0.002*l, 0.));
229      line.push_back(G4Point3D(-0.9+0.025*lpsname, -0.965+0.002*l, 0.));
230      G4VisAttributes attblack(black);
231      //G4VisAttributes attblack(G4Colour(.0, .5, .0));
232      line.SetVisAttributes(&attblack);
233      fVisManager->Draw2D(line);
234    }
235    // ps name
236    G4Text txtpsname(fPSName, G4Point3D(-0.9, -0.96, 0.));
237    G4double size = 12.;
238    txtpsname.SetScreenSize(size);
239    G4Colour color(1., 1., 1.);
240    G4VisAttributes att(color);
241    txtpsname.SetVisAttributes(&att);
242    fVisManager->Draw2D(txtpsname);
243  }
244
245
246
247  // draw unit
248  // background
249  G4int len = fPSUnit.size();
250  if(len > 0) {
251    for(int l = 0; l < 21; l++) {
252      G4Polyline line;
253      line.push_back(G4Point3D(-0.7, -0.9+0.002*l, 0.));
254      line.push_back(G4Point3D(-0.7+0.3, -0.9+0.002*l, 0.));
255      G4VisAttributes attblack(black);
256      //G4VisAttributes attblack(G4Colour(.5, .0, .0));
257      line.SetVisAttributes(&attblack);
258      fVisManager->Draw2D(line);
259    }
260    // unit
261    G4String psunit = "[" + fPSUnit + "]";
262    G4Text txtunit(psunit, G4Point3D(-0.69, -0.9, 0.));
263    G4double size = 12.;
264    txtunit.SetScreenSize(size);
265    G4Colour color(1., 1., 1.);
266    G4VisAttributes att(color);
267    txtunit.SetVisAttributes(&att);
268    fVisManager->Draw2D(txtunit);
269  }
270
271}
Note: See TracBrowser for help on using the repository browser.