CMS 3D CMS Logo

HIPUserVariablesIORoot.cc
Go to the documentation of this file.
1 #include "TTree.h"
2 
4 
8 
9 // this class's header
11 
12 // ----------------------------------------------------------------------------
13 // constructor
14 
16  : ObjId(0), Id(0), Nhit(0), Nparj(0), Npare(0), DataType(-1), AlignableChi2(0.), AlignableNdof(0) {
17  treename = "T9";
18  treetxt = "HIP User Variables";
19 
20  for (int i = 0; i < nparmax * (nparmax + 1) / 2; ++i)
21  Jtvj[i] = 0.;
22  for (int i = 0; i < nparmax; ++i) {
23  Jtve[i] = 0.;
24  Par[i] = 0.;
25  ParError[i] = 0.;
26  }
27 }
28 
29 // ----------------------------------------------------------------------------
30 
32  tree->Branch("Id", &Id, "Id/i");
33  tree->Branch("ObjId", &ObjId, "ObjId/I");
34 
35  tree->Branch("Nhit", &Nhit, "Nhit/I");
36  tree->Branch("DataType", &DataType, "DataType/I");
37  tree->Branch("Nparj", &Nparj, "Nparj/I");
38  tree->Branch("Jtvj", &Jtvj, "Jtvj[Nparj]/D");
39  tree->Branch("Npare", &Npare, "Npare/I");
40  tree->Branch("Jtve", &Jtve, "Jtve[Npare]/D");
41  tree->Branch("AlignableChi2", &AlignableChi2, "AlignableChi2/D");
42  tree->Branch("AlignableNdof", &AlignableNdof, "AlignableNdof/i");
43  tree->Branch("Par", &Par, "Par[Npare]/D");
44  tree->Branch("ParError", &ParError, "ParError[Npare]/D");
45 }
46 
47 // ----------------------------------------------------------------------------
48 
50  tree->SetBranchAddress("Id", &Id);
51  tree->SetBranchAddress("ObjId", &ObjId);
52 
53  tree->SetBranchAddress("Nhit", &Nhit);
54  tree->SetBranchAddress("DataType", &DataType);
55  tree->SetBranchAddress("Nparj", &Nparj);
56  tree->SetBranchAddress("Jtvj", &Jtvj);
57  tree->SetBranchAddress("Npare", &Npare);
58  tree->SetBranchAddress("Jtve", &Jtve);
59  tree->SetBranchAddress("AlignableChi2", &AlignableChi2);
60  tree->SetBranchAddress("AlignableNdof", &AlignableNdof);
61  tree->SetBranchAddress("Par", &Par);
62  tree->SetBranchAddress("ParError", &ParError);
63 }
64 
65 // ----------------------------------------------------------------------------
66 // find tree entry based on detID and typeID
67 
68 int HIPUserVariablesIORoot::findEntry(unsigned int detId, int comp) {
69  if (newopen) { // we're here for the first time
70  edm::LogInfo("Alignment") << "[HIPUserVariablesIORoot::findEntry] fill map ...";
71  treemap.erase(treemap.begin(), treemap.end());
72  for (int ev = 0; ev < tree->GetEntries(); ev++) {
73  tree->GetEntry(ev);
74  treemap[std::make_pair(Id, ObjId)] = ev;
75  }
76  newopen = false;
77  }
78 
79  // now we have filled the map
80  treemaptype::iterator imap = treemap.find(std::make_pair(detId, comp));
81  int result = -1;
82  if (imap != treemap.end())
83  result = (*imap).second;
84  return result;
85 
86  //double noAliPar = tree->GetEntries();
87  //for (int ev = 0;ev<noAliPar;ev++) {
88  // tree->GetEntry(ev);
89  // if(Id==detId&&comp==ObjId) return (ev);
90  //}
91  //return(-1);
92 }
93 
94 // ----------------------------------------------------------------------------
95 
98 
99  if ((ap->userVariables()) == nullptr) {
100  edm::LogError("Alignment") << "UserVariables not found!";
101  return -1;
102  }
103 
104  HIPUserVariables* uvar = dynamic_cast<HIPUserVariables*>(ap->userVariables());
105 
106  AlgebraicSymMatrix jtvj = uvar->jtvj;
107  AlgebraicVector jtve = uvar->jtve;
108  AlgebraicVector alipar = uvar->alipar;
109  AlgebraicVector alierr = uvar->alierr;
110  int nhit = uvar->nhit;
111  int datatype = uvar->datatype;
112  int np = jtve.num_row();
113 
114  Nhit = nhit;
115  DataType = datatype;
116  Npare = np;
117  Nparj = np * (np + 1) / 2;
118  int count = 0;
119  for (int row = 0; row < np; row++) {
120  Jtve[row] = jtve[row];
121  Par[row] = alipar[row];
122  ParError[row] = alierr[row];
123  for (int col = row; col < np; col++) {
124  Jtvj[count] = jtvj[row][col];
125  count++;
126  }
127  }
128  Id = ali->id();
129  ObjId = ali->alignableObjectId();
130 
131  //Chi^2 of alignable
132  AlignableChi2 = uvar->alichi2;
133  AlignableNdof = uvar->alindof;
134  tree->Fill();
135  return 0;
136 }
137 
138 // ----------------------------------------------------------------------------
139 
141  ierr = 0;
142  HIPUserVariables* uvar;
143 
144  int entry = findEntry(ali->id(), ali->alignableObjectId());
145  if (entry != -1) {
146  tree->GetEntry(entry);
147 
148  int np = Npare;
149  AlgebraicVector jtve(np, 0);
150  AlgebraicSymMatrix jtvj(np, 0);
151  AlgebraicVector alipar(np, 0);
152  AlgebraicVector alierr(np, 0);
153  int count = 0;
154  for (int row = 0; row < np; row++) {
155  jtve[row] = Jtve[row];
156  alipar[row] = Par[row];
157  alierr[row] = ParError[row];
158  for (int col = row; col < np; col++) {
159  jtvj[row][col] = Jtvj[count];
160  count++;
161  }
162  }
163 
164  uvar = new HIPUserVariables(np);
165  uvar->jtvj = jtvj;
166  uvar->jtve = jtve;
167  uvar->nhit = Nhit;
168  uvar->datatype = DataType;
169  uvar->alipar = alipar;
170  uvar->alierr = alierr;
171  //Chi2n
172  uvar->alichi2 = AlignableChi2;
173  uvar->alindof = AlignableNdof;
174 
175  return uvar;
176  }
177 
178  // ierr=-1;
179  return nullptr;
180 }
181 
182 //-----------------------------------------------------------------------------
183 
185  const Alignables& alivec, const char* filename, int iter, bool validCheck, int& ierr) {
186  ierr = 0;
187  int iret;
188  iret = open(filename, iter, true);
189  if (iret != 0) {
190  ierr = -1;
191  return;
192  }
193  iret = write(alivec, validCheck);
194  if (iret != 0) {
195  ierr = -2;
196  return;
197  }
198  iret = close();
199  if (iret != 0) {
200  ierr = -3;
201  return;
202  }
203 }
204 
205 //-----------------------------------------------------------------------------
206 
207 std::vector<AlignmentUserVariables*> HIPUserVariablesIORoot::readHIPUserVariables(const Alignables& alivec,
208  const char* filename,
209  int iter,
210  int& ierr) {
211  std::vector<AlignmentUserVariables*> result;
212  ierr = 0;
213  int iret;
214  iret = open(filename, iter, false);
215  if (iret != 0) {
216  ierr = -1;
217  return result;
218  }
219  result = read(alivec, iret);
220  if (iret != 0) {
221  ierr = -2;
222  return result;
223  }
224  iret = close();
225  if (iret != 0) {
226  ierr = -3;
227  return result;
228  }
229 
230  return result;
231 }
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:180
AlignmentUserVariables * readOne(Alignable *ali, int &ierr) override
int findEntry(unsigned int detId, int comp)
void createBranches(void) override
create root branches
AlgebraicVector alipar
bool ev
AlgebraicVector jtve
AlignmentParameters * alignmentParameters() const
Get the AlignmentParameters.
Definition: Alignable.h:58
void writeHIPUserVariables(const Alignables &alivec, const char *filename, int iter, bool validCheck, int &ierr)
AlgebraicSymMatrix jtvj
AlignmentUserVariables * userVariables(void) const
Get pointer to user variables.
void setBranchAddresses(void) override
set root branches
int np
Definition: AMPTWrapper.h:43
int writeOne(Alignable *ali) override
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
std::vector< AlignmentUserVariables * > readHIPUserVariables(const Alignables &alivec, const char *filename, int iter, int &ierr)
AlgebraicVector alierr
double Jtvj[nparmax *(nparmax+1)/2]
(Abstract) Base class for alignment algorithm user variables
CLHEP::HepVector AlgebraicVector
int write(const align::Alignables &alivec, bool validCheck)
std::vector< AlignmentUserVariables * > read(const align::Alignables &alivec, int &ierr)
col
Definition: cuy.py:1010
CLHEP::HepSymMatrix AlgebraicSymMatrix
Definition: tree.py:1
int open(const char *filename, int iteration, bool writemode) override