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 }
FWMagField::m_userField
float m_userField
Definition: FWMagField.h:52
DDAxes::y
FWMagField::guessField
void guessField(float estimate) const
Definition: FWMagField.cc:129
FWMagField::GetFieldMag
Float_t GetFieldMag() const
Definition: FWMagField.cc:73
fwLog
#define fwLog(_level_)
Definition: fwLog.h:45
mps_fire.i
i
Definition: mps_fire.py:428
funct::false
false
Definition: Factorize.h:29
Handle.h
DcsStatus.h
FWMagField::kNone
Definition: FWMagField.h:18
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
detailsBasic3DVector::z
float float float z
Definition: extBasic3DVector.h:14
fwlite::Run::getByLabel
bool getByLabel(std::type_info const &, char const *, char const *, char const *, void *) const override
Definition: Run.cc:184
FWMagField::FWMagField
FWMagField()
Definition: FWMagField.cc:11
DDAxes::x
fwlite::Event::getRun
fwlite::Run const & getRun() const
Definition: Event.cc:517
edm::Handle
Definition: AssociativeIterator.h:50
FWMagField::checkFieldInfo
void checkFieldInfo(const edm::EventBase *)
Definition: FWMagField.cc:144
FWMagField::m_eventField
float m_eventField
Definition: FWMagField.h:53
FWMagField::m_numberOfFieldEstimates
int m_numberOfFieldEstimates
Definition: FWMagField.h:61
Abs
T Abs(T a)
Definition: MathUtil.h:49
edm::ConditionsInRunBlock::BAvgCurrent
float BAvgCurrent
Definition: ConditionsInEdm.h:27
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
DDAxes::z
topElectronID_cff.dcsTag
dcsTag
Definition: topElectronID_cff.py:11
FWMagField::GetField
TEveVector GetField(Float_t x, Float_t y, Float_t z) const override
Definition: FWMagField.cc:34
funct::true
true
Definition: Factorize.h:173
FWMagField::resetFieldEstimate
void resetFieldEstimate() const
Definition: FWMagField.cc:135
FWMagField::kEvent
Definition: FWMagField.h:18
Max
T Max(T a, T b)
Definition: MathUtil.h:44
fwLog.h
res
Definition: Electron.h:6
FWMagField::m_numberOfFieldIsOnEstimates
int m_numberOfFieldIsOnEstimates
Definition: FWMagField.h:60
FWMagField::m_simpleModel
bool m_simpleModel
Definition: FWMagField.h:56
FWMagField::m_guessedField
float m_guessedField
Definition: FWMagField.h:63
FWMagField.h
mag
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Definition: Basic3DVectorLD.h:127
WDecay::kNone
Definition: TopGenEvent.h:27
heppy_batch.val
val
Definition: heppy_batch.py:351
FWMagField::m_source
ESource m_source
Definition: FWMagField.h:51
fwlite::Event
Definition: Event.h:87
FWMagField::guessFieldIsOn
void guessFieldIsOn(bool guess) const
Definition: FWMagField.cc:122
ConditionsInEdm.h
fwlog::kDebug
Definition: fwLog.h:35
FWMagField::~FWMagField
~FWMagField() override
Definition: FWMagField.cc:30
Event.h
edm::EventBase
Definition: EventBase.h:46
genVertex_cff.x
x
Definition: genVertex_cff.py:12
detailsBasic3DVector::y
float float y
Definition: extBasic3DVector.h:14
FWMagField::m_updateFieldEstimate
bool m_updateFieldEstimate
Definition: FWMagField.h:62
FWMagField::setFFFieldMag
void setFFFieldMag(float)
Definition: FWMagField.cc:190
cms::Exception
Definition: Exception.h:70
FWMagField::kUser
Definition: FWMagField.h:18
FWMagField::m_reverse
bool m_reverse
Definition: FWMagField.h:55
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
FWMagField::GetMaxFieldMag
Float_t GetMaxFieldMag() const override
Definition: FWMagField.cc:111
event
Definition: event.py:1
dttmaxenums::R
Definition: DTTMax.h:29
edm::InputTag
Definition: InputTag.h:15
FWMagField::m_guessValHist
TH1F * m_guessValHist
Definition: FWMagField.h:59