-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleObject.cpp
480 lines (418 loc) · 16 KB
/
SimpleObject.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// File modified by Deepak Samuel on 25 Sep 2019
#include "SimpleObject.h"
#include "ui_simpleobject.h"
#include <QStringList>
#include "G4SystemOfUnits.hh"
#include "G4VProcess.hh"
#include <QDebug>
#include <SimpleMaterialBuilder.h>
int SimpleObject::objId=0;
SimpleObject::SimpleObject(QString type, G4ThreeVector pos, G4ThreeVector rot, G4ThreeVector mag, QString material, QString material_formula, QString color, bool rec_data)
{
// is_mesh=false;
if(!types.contains(type))
return;
if(type=="Box"){
dim_names = box_params;
dim_val = default_dims_box;
}
else if (type=="Cylinder"){
dim_names = cyl_params;
dim_val = default_dims_cyl;
}
else if(type=="Sphere"){
dim_names = sph_params;
dim_val = default_dims_sph;
}
else if (type=="Wedge"){
dim_names = wedge_params;
dim_val = default_dims_wedge;
}
if(objId==0){
objName = "World";
isWorld =true;
}
else{
objName = QString("%1-%2").arg(type).arg(objId,3,10, QChar('0'));
isWorld = false;
}
if(objId==0)
isWorld=true;
else {
isWorld=false;
}
id = objId;
objType =type;
objColor=color;
position = pos;
rotation = rot;
magField = mag;
objMaterial =material;
objMaterialFormula =material_formula;
objMaterialProps ="";
record_data = rec_data;
isSolid=true;
if(magField.x()==0.00000 && magField.y()==0.0000 && magField.z()==0.0000)
mag_field_changed=false;
else {
mag_field_changed=true;
}
magfield_off = false;
jsonObject["id"] = id;
jsonObject["objName"] = objName;
jsonObject["objType"] = type;
jsonObject["objColor"] = color;
jsonObject["posx"] = pos.x();
jsonObject["posy"] = pos.y();
jsonObject["posz"] = pos.z();
jsonObject["rotx"] = rot.x();
jsonObject["roty"] = rot.y();
jsonObject["rotz"] = rot.z();
jsonObject["magx"] = mag.x();
jsonObject["magy"] = mag.y();
jsonObject["magz"] = mag.z();
jsonObject["objMaterial"] = material;
jsonObject["objMaterialFormula"] = material_formula;
jsonObject["objMaterialProps"] = objMaterialProps;
jsonObject["record_data"] = rec_data;
int ii=0;
foreach(double dim, dim_val){
jsonObject[QString("dim-%1").arg(ii++)] = dim;
}
G4NistManager* nist = G4NistManager::Instance();
if(objMaterial.contains("G4_"))
g4material = nist->FindOrBuildMaterial(objMaterial.toLatin1().data());
else {
// custom material, created
g4material= SimpleMaterialBuilder::getInstance()->GetMaterial(objMaterial, objMaterialFormula);
if(g4material->GetName()=="G4_AIR"){
objMaterial="G4_AIR"; // for the initial world volume
objMaterialFormula="";
}
}
objId++;
}
SimpleObject::SimpleObject(QJsonObject jo)
{
id = jo["id"].toInt();
objName= jo["objName"].toString();
if(objName=="World") isWorld=true;
objType= jo["objType"].toString();
objColor=jo["objColor"].toString();
position.set(jo["posx"].toDouble(),jo["posy"].toDouble(),jo["posz"].toDouble());
rotation.set(jo["rotx"].toDouble(),jo["roty"].toDouble(),jo["rotz"].toDouble());
magField.set(jo["magx"].toDouble(),jo["magy"].toDouble(),jo["magz"].toDouble());
objMaterial= jo["objMaterial"].toString();
objMaterialFormula = jo["objMaterialFormula"].toString();
objMaterialProps= jo["objMaterialProps"].toString();
record_data = jo["record_data"].toBool();
if(magField.x()==0.00000 && magField.y()==0.0000 && magField.z()==0.0000)
mag_field_changed=false;
else {
mag_field_changed=true;
}
magfield_off = false;
int par_count=0;
if(objType=="Box"){ dim_names = box_params; par_count=box_params.count();}
if(objType=="Cylinder"){ dim_names = cyl_params; par_count=cyl_params.count();}
if(objType=="Sphere"){ dim_names = sph_params;par_count = sph_params.count();}
if(objType=="Wedge"){ dim_names = wedge_params;par_count = wedge_params.count();}
for(int ii=0; ii<par_count;ii++){
dim_val.append(jo[QString("dim-%1").arg(ii)].toDouble());
}
G4NistManager* nist = G4NistManager::Instance();
if(objMaterial.contains("G4"))
g4material = nist->FindOrBuildMaterial(objMaterial.toLatin1().data());
else {
// custom material, created
g4material= SimpleMaterialBuilder::getInstance()->GetMaterial(objMaterial, objMaterialFormula);
if(g4material->GetName()=="G4_AIR"){ // for the initial world volume
objMaterial="G4_AIR";
objMaterialFormula="";
}
}
jsonObject = jo;
if(objId<id) objId=id; // or else multiple objects with same name might be created, if the user has deleted some objects in between
objId++;
}
//SimpleObject::SimpleObject(QString type,G4ThreeVector pos, G4ThreeVector rot, G4ThreeVector nbins)
//{
// is_mesh =true;
// if(type=="Box Mesh")
// {
// dim_names = box_mesh_params;
// dim_val = default_dims_box_mesh;
// }
// else if(type=="Cylinder Mesh")
// {
// dim_names = cyl_mesh_params;
// dim_val = default_dims_cyl_mesh;
// }
// objName = QString("%1-%2").arg(type).arg(objId,3,10, QChar('0'));
// isWorld = false;
// bins = nbins;
// id = objId;
// objType =type;
// position = pos;
// rotation = rot;
// objMaterial ="NA";
// record_data = false;
// objId++;
//}
SimpleObject::~SimpleObject()
{
objId--;
}
G4Material* SimpleObject::CreateMaterial(QString name,QString formula)
{
// the format is a*mat1+b*mat2, density=p
// where mat1 and mat2 are G4 materials and a and b are fractions such that a+b = 1 and p is the density in g/cm3
// if the formula has errors, the material is set to G4_AIR
G4NistManager* nist = G4NistManager::Instance();
QStringList s = formula.split(",");
double density =0; // in g/cm3
int nelements=0;
if(s.count()==2){
QString d1 = s.at(1);
QStringList d2 = d1.split("=");
if(d2.count()==2){
density = d2.at(1).toDouble();
}
else {
output(QString("Error in creating material %1 with formula. Incorrect density format").arg(name).arg(formula));
output("The correct format is a*mat1+b*mat2, density=p, where mat1 and mat2 are G4 elements and a and b are fractions such that a+b = 1 and p is the density in g/cm3");
output("The material is set to G4_AIR");
objMaterial="G4_AIR";
objMaterialFormula="";
return nist->FindOrBuildMaterial("G4_AIR");
}
QStringList d3 = s.at(0).split("+");
nelements = d3.count();
if(nelements<=0){
output(QString("Error in creating material %1 with formula. Incorrect of number of elements: %3").arg(name).arg(formula).arg(nelements));
output("The correct format is a*mat1+b*mat2, density=p, where mat1 and mat2 are G4 elements and a and b are fractions such that a+b = 1 and p is the density in g/cm3");
output("The material is set to G4_AIR");
objMaterial="G4_AIR";
objMaterialFormula="";
return nist->FindOrBuildMaterial("G4_AIR");
}
G4Material* new_material = new G4Material(name.toLatin1().data(), density*g/cm3, nelements);
for(int ii=0; ii<nelements;ii++){
QStringList d4 = d3.at(ii).split("*");
if(d4.count()!=2){
output(QString("Error in creating material %1 with formula. Incorrect of number of elements: %3").arg(name).arg(formula).arg(nelements));
output("The correct format is a*mat1+b*mat2, density=p, where mat1 and mat2 are G4 elements and a and b are fractions such that a+b = 1 and p is the density in g/cm3");
output("The material is set to G4_AIR");
objMaterial="G4_AIR";
objMaterialFormula="";
return nist->FindOrBuildMaterial("G4_AIR");
}
G4Element* e=nist->FindOrBuildElement(d4.at(1).toStdString());
if(e!=nullptr)
new_material->AddElement(e,d4.at(0).toDouble());
else {
output(QString("Error in creating material %1 with formula. Could not find or build element: %3").arg(name).arg(formula).arg(d4.at(1)));
output("The correct format is a*mat1+b*mat2, density=p, where mat1 and mat2 are G4 elements and a and b are fractions such that a+b = 1 and p is the density in g/cm3");
output("The material is set to G4_AIR");
objMaterial="G4_AIR";
objMaterialFormula="";
return nist->FindOrBuildMaterial("G4_AIR");
}
}
return new_material;
}
else {
output(QString("Error in creating material %1 with formula. Incorrect format.").arg(name).arg(formula));
output("The correct format is a*mat1+b*mat2, density=p, where mat1 and mat2 are G4 elements and a and b are fractions such that a+b = 1 and p is the density in g/cm3");
output("The material is set to G4_AIR");
objMaterial="G4_AIR";
objMaterialFormula="";
return nist->FindOrBuildMaterial("G4_AIR");
}
}
void SimpleObject::SetMaterial(QString material)
{
G4NistManager* nist = G4NistManager::Instance();
objMaterial=material;
jsonObject["objMaterial"] = material;
if(!material.contains("G4_")){ // user created material
objMaterialFormula=SimpleMaterialBuilder::getInstance()->GetMaterialFormula(material);
jsonObject["objMaterialFormula"] = objMaterialFormula;
}
if(objMaterial.contains("G4"))
g4material = nist->FindOrBuildMaterial(objMaterial.toLatin1().data());
else {
g4material= SimpleMaterialBuilder::getInstance()->GetMaterial(objMaterial, objMaterialFormula);
}
}
QList <double> SimpleObject::GetDimensionDefaults()
{
if(objType=="Box"){
return default_dims_box;
}
else if (objType=="Cylinder"){
return default_dims_cyl;
}
else if(objType=="Sphere"){
return default_dims_sph;
}
else if (objType=="Wedge"){
return default_dims_wedge;
}
}
void SimpleObject::SetDimensions(QList <double> val)
{
dim_val = val;
int ii=0;
foreach(double dim, dim_val){
jsonObject[QString("dim-%1").arg(ii++)] = dim;
}
}
void SimpleObject::SetDimension(int ii, double val)
{
dim_val.replace(ii,val);
jsonObject[QString("dim-%1").arg(ii)] = val;
}
void SimpleObject::SetRecordParameters(bool recordPos, bool recordTime, bool recordEnergy, bool recordMom, bool recordProcess, QString filterPIDs)
{
filterIDs.clear();
rec_pos = recordPos;
rec_time = recordTime;
rec_energy = recordEnergy;
rec_mom = recordMom;
rec_process = recordProcess;
if(filterPIDs!=""){
QStringList sl = filterPIDs.split(",");
if(sl.count()>0){
foreach(QString s, sl)
filterIDs.append(s.toInt());
}
}
}
QList <double> SimpleObject::GetDimensions()
{
return dim_val;
}
double SimpleObject::GetDimension(int p)
{
return dim_val.at(p);
}
int SimpleObject::GetObjId()
{
return id;
}
void SimpleObject::SetVisProperties(QString color, bool isVisible, bool isSolid)
{
G4VisAttributes att = logicVol->GetVisAttributes();
QColor col(color);
att.SetColor(col.red(),col.green(),col.blue(),0.2);
att.SetVisibility(isVisible);
// if(isSolid)
// att.SetForceSolid();
// else {
// att.SetForceWireframe();
// }
logicVol->SetVisAttributes(att);
}
void SimpleObject::SetVisSolid(bool isSolid)
{
G4VisAttributes att= logicVol->GetVisAttributes();
QColor col(objColor);
att.SetColor(col.red(),col.green(),col.blue(),0.2);
att.SetVisibility(!isHidden);
if(isSolid)
att.SetForceSolid();
else {
att.SetForceWireframe();
}
logicVol->SetVisAttributes(att);
}
void SimpleObject::SetVisiblity(bool isVisible)
{
isHidden=!isVisible;
//SetVisProperties(objColor,!isHidden,isSolid);
}
void SimpleObject::ClearData()
{
}
void SimpleObject::FillData(const G4Step *step, ulong event_id)
{
if(record_data && step->GetPreStepPoint()->GetTouchableHandle() ->GetVolume()->GetLogicalVolume()->GetName()==objName.toLatin1().data()){
int columnId =0;
if((filterIDs.contains(step->GetTrack()->GetDefinition()->GetPDGEncoding())) || (filterIDs.count()==0)){
G4AnalysisManager* man = G4AnalysisManager::Instance();
{
man->FillNtupleDColumn(columnId++, event_id);
man->FillNtupleDColumn(columnId++, step->GetTrack()->GetDefinition()->GetPDGEncoding());
man->FillNtupleSColumn(columnId++, objName.toLatin1().data());
man->FillNtupleDColumn(columnId++,step->GetTrack()->GetParentID());
columnId =appendPositionParameters(columnId, step);
columnId =appendTimeParameters(columnId,step);
columnId =appendEnergyParameters(columnId,step);
columnId =appendMomentumParamters(columnId,step);
columnId =appendProcessName(columnId,step);
man->AddNtupleRow(0);
}
}
}
}
int SimpleObject::appendPositionParameters(int column_id, const G4Step *step)
{
if(rec_pos){
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->FillNtupleDColumn(column_id++,step->GetPreStepPoint()->GetPosition().x()*mm);
man->FillNtupleDColumn(column_id++,step->GetPreStepPoint()->GetPosition().y()*mm);
man->FillNtupleDColumn(column_id++,step->GetPreStepPoint()->GetPosition().z()*mm);
man->FillNtupleDColumn(column_id++,step->GetDeltaPosition().x()*mm);
man->FillNtupleDColumn(column_id++,step->GetDeltaPosition().y()*mm);
man->FillNtupleDColumn(column_id++,step->GetDeltaPosition().z()*mm);
}
return column_id;
}
int SimpleObject::appendTimeParameters(int column_id,const G4Step* step)
{
if(rec_time){
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->FillNtupleDColumn(column_id++,step->GetDeltaTime()*ns);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetGlobalTime()*ns);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetProperTime()*ns);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetLocalTime()*ns);
}
return column_id;
}
int SimpleObject::appendEnergyParameters(int column_id,const G4Step* step)
{
if(rec_energy){
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->FillNtupleDColumn(column_id++,step->GetNonIonizingEnergyDeposit()*keV);
man->FillNtupleDColumn(column_id++,step->GetTotalEnergyDeposit()*keV);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetTotalEnergy()*keV);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetKineticEnergy()*keV);
}
return column_id;
}
int SimpleObject::appendMomentumParamters(int column_id,const G4Step* step)
{
if(rec_mom){
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentum().x()*keV);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentum().y()*keV);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentum().z()*keV);
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentumDirection().x());
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentumDirection().y());
man->FillNtupleDColumn(column_id++,step->GetTrack()->GetMomentumDirection().z());
}
return column_id;
}
int SimpleObject::appendProcessName(int column_id, const G4Step *step)
{
if(rec_process){
G4AnalysisManager* man = G4AnalysisManager::Instance();
const G4VProcess* proc = step->GetPreStepPoint()->GetProcessDefinedStep();
if(proc!=NULL)
man->FillNtupleSColumn(column_id++,proc->GetProcessName());
else
man->FillNtupleSColumn(column_id++,"Unknown");
}
return column_id;
}