CMS 3D CMS Logo

FWMagField.cc
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "TH1F.h"
10 
12  : TEveMagField(),
13 
14  m_source(kNone),
15  m_userField(-1),
16  m_eventField(-1),
17 
18  m_reverse(true),
19  m_simpleModel(false),
20 
21  m_guessValHist(nullptr),
22  m_numberOfFieldIsOnEstimates(0),
23  m_numberOfFieldEstimates(0),
24  m_updateFieldEstimate(true),
25  m_guessedField(0) {
26  m_guessValHist = new TH1F("FieldEstimations", "Field estimations from tracks and muons", 200, -4.5, 4.5);
27  m_guessValHist->SetDirectory(nullptr);
28 }
29 
31 
32 //______________________________________________________________________________
33 
34 TEveVector FWMagField::GetField(Float_t x, Float_t y, Float_t z) const {
35  // Virtual method of TEveMagField class.
36 
37  Float_t R = sqrt(x * x + y * y);
38  Float_t field = m_reverse ? -GetFieldMag() : GetFieldMag();
39 
40  //barrel
41  if (TMath::Abs(z) < 724) {
42  //inside solenoid
43  if (R < 300)
44  return TEveVector(0, 0, field);
45  // outside solinoid
46  if (m_simpleModel || (R > 461.0 && R < 490.5) || (R > 534.5 && R < 597.5) || (R > 637.0 && R < 700.0))
47  return TEveVector(0, 0, -field / 3.8 * 1.2);
48 
49  } else {
50  // endcaps
51  if (m_simpleModel) {
52  if (R < 50)
53  return TEveVector(0, 0, field);
54  if (z > 0)
55  return TEveVector(x / R * field / 3.8 * 2.0, y / R * field / 3.8 * 2.0, 0);
56  else
57  return TEveVector(-x / R * field / 3.8 * 2.0, -y / R * field / 3.8 * 2.0, 0);
58  }
59  // proper model
60  if (((TMath::Abs(z) > 724) && (TMath::Abs(z) < 786)) || ((TMath::Abs(z) > 850) && (TMath::Abs(z) < 910)) ||
61  ((TMath::Abs(z) > 975) && (TMath::Abs(z) < 1003))) {
62  if (z > 0)
63  return TEveVector(x / R * field / 3.8 * 2.0, y / R * field / 3.8 * 2.0, 0);
64  else
65  return TEveVector(-x / R * field / 3.8 * 2.0, -y / R * field / 3.8 * 2.0, 0);
66  }
67  }
68  return TEveVector(0, 0, 0);
69 }
70 
71 //______________________________________________________________________________
72 
73 Float_t FWMagField::GetFieldMag() const {
74  float res;
75  switch (m_source) {
76  case kEvent: {
77  res = m_eventField;
78  break;
79  }
80  case kUser: {
81  res = m_userField;
82  break;
83  }
84  default: {
86  if (m_guessValHist->GetEntries() > 2 && m_guessValHist->GetRMS() < 0.5) {
87  m_guessedField = m_guessValHist->GetMean();
88 
89  // std::cout << "FWMagField::GetFieldMag(), get average "
90  // << m_guessValHist->GetMean() << " guessed value: RMS= "<< m_guessValHist->GetRMS()
91  // <<" samples "<< m_guessValHist->GetEntries() << std::endl;
92 
94  m_guessedField = 3.8;
95  // fwLog(fwlog::kDebug) << "FWMagField::GetFieldMag() get default field, number estimates "
96  // << m_numberOfFieldEstimates << " number fields is on m_numberOfFieldIsOnEstimates" <<std::endl;
97  } else {
98  m_guessedField = 0;
99  // fwLog(fwlog::kDebug) << "Update field estimate, guess field is OFF." <<std::endl;
100  }
101  m_updateFieldEstimate = false;
102  }
103 
105  }
106  }
107 
108  return res;
109 }
110 
111 Float_t FWMagField::GetMaxFieldMag() const {
112  // Runge-Kutta stepper does not like this to be zero.
113  // Will be fixed in root.
114  // The return value should definitley not be negative -- so Abs
115  // should stay!
116 
117  return TMath::Max(TMath::Abs(GetFieldMag()), 0.01f);
118 }
119 
120 //______________________________________________________________________________
121 
122 void FWMagField::guessFieldIsOn(bool isOn) const {
123  if (isOn)
126  m_updateFieldEstimate = true;
127 }
128 
129 void FWMagField::guessField(float val) const {
130  // fwLog(filedDebug) << "FWMagField::guessField "<< val << std::endl;
131  m_guessValHist->Fill(val);
132  m_updateFieldEstimate = true;
133 }
134 
136  m_guessValHist->Reset();
137  m_guessValHist->SetAxisRange(-4, 4);
140  m_updateFieldEstimate = true;
141 }
142 
143 //______________________________________________________________________________
145  const static float currentToField = 3.8 / 18160;
146  bool available = false;
147  try {
148  edm::InputTag conditionsTag("conditionsInEdm");
150  // FIXME: ugly hack to avoid exposing an fwlite::Event. Need to ask
151  // Chris / Ianna how to get mag field from an EventBase.
152  const fwlite::Event* fwEvent = dynamic_cast<const fwlite::Event*>(event);
153  if (!fwEvent)
154  return;
155 
156  m_source = kNone;
157  fwEvent->getRun().getByLabel(conditionsTag, runCond);
158 
159  if (runCond.isValid()) {
160  available = true;
161  m_eventField = currentToField * runCond->BAvgCurrent;
162  m_source = kEvent;
163  fwLog(fwlog::kDebug) << "Magnetic field info found in ConditionsInEdm branch : " << m_eventField << std::endl;
164  } else {
165  edm::InputTag dcsTag("scalersRawToDigi");
167  event->getByLabel(dcsTag, dcsStatus);
168 
169  if (dcsStatus.isValid() && !dcsStatus->empty()) {
170  float sum = 0;
171  for (std::vector<DcsStatus>::const_iterator i = dcsStatus->begin(); i < dcsStatus->end(); ++i)
172  sum += (*i).magnetCurrent();
173 
174  available = true;
175  m_eventField = currentToField * sum / dcsStatus->size();
176  m_source = kEvent;
177  fwLog(fwlog::kDebug) << "Magnetic field info found in DcsStatus branch: " << m_eventField << std::endl;
178  }
179  }
180  } catch (cms::Exception&) {
181  fwLog(fwlog::kDebug) << "Cought exception in FWMagField::checkFieldInfo\n";
182  }
183 
184  if (!available) {
185  fwLog(fwlog::kDebug) << "No magnetic field info available in Event\n";
186  }
187 }
188 
189 //______________________________________________________________________________
191  // AMT this is a workaround for seting FF in FFLooper
192  // Correct imeplementation is having a base class of FWMagField amd do implementation for FF and FWLite version
193 
194  m_source = kEvent;
195  m_eventField = mag;
196 }
int m_numberOfFieldEstimates
Definition: FWMagField.h:61
int m_numberOfFieldIsOnEstimates
Definition: FWMagField.h:60
bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const override
Definition: Run.cc:184
void resetFieldEstimate() const
Definition: FWMagField.cc:135
void guessFieldIsOn(bool guess) const
Definition: FWMagField.cc:122
TEveVector GetField(Float_t x, Float_t y, Float_t z) const override
Definition: FWMagField.cc:34
bool m_simpleModel
Definition: FWMagField.h:56
float m_guessedField
Definition: FWMagField.h:63
~FWMagField() override
Definition: FWMagField.cc:30
bool m_reverse
Definition: FWMagField.h:55
bool m_updateFieldEstimate
Definition: FWMagField.h:62
TH1F * m_guessValHist
Definition: FWMagField.h:59
float float float z
Definition: Electron.h:6
Float_t GetMaxFieldMag() const override
Definition: FWMagField.cc:111
void setFFFieldMag(float)
Definition: FWMagField.cc:190
ESource m_source
Definition: FWMagField.h:51
T sqrt(T t)
Definition: SSEVec.h:19
double f[11][100]
float m_userField
Definition: FWMagField.h:52
Float_t GetFieldMag() const
Definition: FWMagField.cc:73
void checkFieldInfo(const edm::EventBase *)
Definition: FWMagField.cc:144
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
void guessField(float estimate) const
Definition: FWMagField.cc:129
#define fwLog(_level_)
Definition: fwLog.h:45
bool isValid() const
Definition: HandleBase.h:70
float m_eventField
Definition: FWMagField.h:53
float x
fwlite::Run const & getRun() const
Definition: Event.cc:517
Definition: event.py:1