Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:59

0001 #include "TTree.h"
0002 
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 #include "Alignment/CommonAlignment/interface/Alignable.h"
0006 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
0007 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariables.h"
0008 
0009 // this class's header
0010 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariablesIORoot.h"
0011 
0012 // ----------------------------------------------------------------------------
0013 // constructor
0014 
0015 HIPUserVariablesIORoot::HIPUserVariablesIORoot()
0016     : ObjId(0), Id(0), Nhit(0), Nparj(0), Npare(0), DataType(-1), AlignableChi2(0.), AlignableNdof(0) {
0017   treename = "T9";
0018   treetxt = "HIP User Variables";
0019 
0020   for (int i = 0; i < nparmax * (nparmax + 1) / 2; ++i)
0021     Jtvj[i] = 0.;
0022   for (int i = 0; i < nparmax; ++i) {
0023     Jtve[i] = 0.;
0024     Par[i] = 0.;
0025     ParError[i] = 0.;
0026   }
0027 }
0028 
0029 // ----------------------------------------------------------------------------
0030 
0031 void HIPUserVariablesIORoot::createBranches(void) {
0032   tree->Branch("Id", &Id, "Id/i");
0033   tree->Branch("ObjId", &ObjId, "ObjId/I");
0034 
0035   tree->Branch("Nhit", &Nhit, "Nhit/I");
0036   tree->Branch("DataType", &DataType, "DataType/I");
0037   tree->Branch("Nparj", &Nparj, "Nparj/I");
0038   tree->Branch("Jtvj", &Jtvj, "Jtvj[Nparj]/D");
0039   tree->Branch("Npare", &Npare, "Npare/I");
0040   tree->Branch("Jtve", &Jtve, "Jtve[Npare]/D");
0041   tree->Branch("AlignableChi2", &AlignableChi2, "AlignableChi2/D");
0042   tree->Branch("AlignableNdof", &AlignableNdof, "AlignableNdof/i");
0043   tree->Branch("Par", &Par, "Par[Npare]/D");
0044   tree->Branch("ParError", &ParError, "ParError[Npare]/D");
0045 }
0046 
0047 // ----------------------------------------------------------------------------
0048 
0049 void HIPUserVariablesIORoot::setBranchAddresses(void) {
0050   tree->SetBranchAddress("Id", &Id);
0051   tree->SetBranchAddress("ObjId", &ObjId);
0052 
0053   tree->SetBranchAddress("Nhit", &Nhit);
0054   tree->SetBranchAddress("DataType", &DataType);
0055   tree->SetBranchAddress("Nparj", &Nparj);
0056   tree->SetBranchAddress("Jtvj", &Jtvj);
0057   tree->SetBranchAddress("Npare", &Npare);
0058   tree->SetBranchAddress("Jtve", &Jtve);
0059   tree->SetBranchAddress("AlignableChi2", &AlignableChi2);
0060   tree->SetBranchAddress("AlignableNdof", &AlignableNdof);
0061   tree->SetBranchAddress("Par", &Par);
0062   tree->SetBranchAddress("ParError", &ParError);
0063 }
0064 
0065 // ----------------------------------------------------------------------------
0066 // find tree entry based on detID and typeID
0067 
0068 int HIPUserVariablesIORoot::findEntry(unsigned int detId, int comp) {
0069   if (newopen) {  // we're here for the first time
0070     edm::LogInfo("Alignment") << "[HIPUserVariablesIORoot::findEntry] fill map ...";
0071     treemap.erase(treemap.begin(), treemap.end());
0072     for (int ev = 0; ev < tree->GetEntries(); ev++) {
0073       tree->GetEntry(ev);
0074       treemap[std::make_pair(Id, ObjId)] = ev;
0075     }
0076     newopen = false;
0077   }
0078 
0079   // now we have filled the map
0080   treemaptype::iterator imap = treemap.find(std::make_pair(detId, comp));
0081   int result = -1;
0082   if (imap != treemap.end())
0083     result = (*imap).second;
0084   return result;
0085 
0086   //double noAliPar = tree->GetEntries();
0087   //for (int ev = 0;ev<noAliPar;ev++) {
0088   //  tree->GetEntry(ev);
0089   //  if(Id==detId&&comp==ObjId) return (ev);
0090   //}
0091   //return(-1);
0092 }
0093 
0094 // ----------------------------------------------------------------------------
0095 
0096 int HIPUserVariablesIORoot::writeOne(Alignable* ali) {
0097   AlignmentParameters* ap = ali->alignmentParameters();
0098 
0099   if ((ap->userVariables()) == nullptr) {
0100     edm::LogError("Alignment") << "UserVariables not found!";
0101     return -1;
0102   }
0103 
0104   HIPUserVariables* uvar = dynamic_cast<HIPUserVariables*>(ap->userVariables());
0105 
0106   AlgebraicSymMatrix jtvj = uvar->jtvj;
0107   AlgebraicVector jtve = uvar->jtve;
0108   AlgebraicVector alipar = uvar->alipar;
0109   AlgebraicVector alierr = uvar->alierr;
0110   int nhit = uvar->nhit;
0111   int datatype = uvar->datatype;
0112   int np = jtve.num_row();
0113 
0114   Nhit = nhit;
0115   DataType = datatype;
0116   Npare = np;
0117   Nparj = np * (np + 1) / 2;
0118   int count = 0;
0119   for (int row = 0; row < np; row++) {
0120     Jtve[row] = jtve[row];
0121     Par[row] = alipar[row];
0122     ParError[row] = alierr[row];
0123     for (int col = row; col < np; col++) {
0124       Jtvj[count] = jtvj[row][col];
0125       count++;
0126     }
0127   }
0128   Id = ali->id();
0129   ObjId = ali->alignableObjectId();
0130 
0131   //Chi^2 of alignable
0132   AlignableChi2 = uvar->alichi2;
0133   AlignableNdof = uvar->alindof;
0134   tree->Fill();
0135   return 0;
0136 }
0137 
0138 // ----------------------------------------------------------------------------
0139 
0140 AlignmentUserVariables* HIPUserVariablesIORoot::readOne(Alignable* ali, int& ierr) {
0141   ierr = 0;
0142   HIPUserVariables* uvar;
0143 
0144   int entry = findEntry(ali->id(), ali->alignableObjectId());
0145   if (entry != -1) {
0146     tree->GetEntry(entry);
0147 
0148     int np = Npare;
0149     AlgebraicVector jtve(np, 0);
0150     AlgebraicSymMatrix jtvj(np, 0);
0151     AlgebraicVector alipar(np, 0);
0152     AlgebraicVector alierr(np, 0);
0153     int count = 0;
0154     for (int row = 0; row < np; row++) {
0155       jtve[row] = Jtve[row];
0156       alipar[row] = Par[row];
0157       alierr[row] = ParError[row];
0158       for (int col = row; col < np; col++) {
0159         jtvj[row][col] = Jtvj[count];
0160         count++;
0161       }
0162     }
0163 
0164     uvar = new HIPUserVariables(np);
0165     uvar->jtvj = jtvj;
0166     uvar->jtve = jtve;
0167     uvar->nhit = Nhit;
0168     uvar->datatype = DataType;
0169     uvar->alipar = alipar;
0170     uvar->alierr = alierr;
0171     //Chi2n
0172     uvar->alichi2 = AlignableChi2;
0173     uvar->alindof = AlignableNdof;
0174 
0175     return uvar;
0176   }
0177 
0178   //  ierr=-1;
0179   return nullptr;
0180 }
0181 
0182 //-----------------------------------------------------------------------------
0183 
0184 void HIPUserVariablesIORoot::writeHIPUserVariables(
0185     const Alignables& alivec, const char* filename, int iter, bool validCheck, int& ierr) {
0186   ierr = 0;
0187   int iret;
0188   iret = open(filename, iter, true);
0189   if (iret != 0) {
0190     ierr = -1;
0191     return;
0192   }
0193   iret = write(alivec, validCheck);
0194   if (iret != 0) {
0195     ierr = -2;
0196     return;
0197   }
0198   iret = close();
0199   if (iret != 0) {
0200     ierr = -3;
0201     return;
0202   }
0203 }
0204 
0205 //-----------------------------------------------------------------------------
0206 
0207 std::vector<AlignmentUserVariables*> HIPUserVariablesIORoot::readHIPUserVariables(const Alignables& alivec,
0208                                                                                   const char* filename,
0209                                                                                   int iter,
0210                                                                                   int& ierr) {
0211   std::vector<AlignmentUserVariables*> result;
0212   ierr = 0;
0213   int iret;
0214   iret = open(filename, iter, false);
0215   if (iret != 0) {
0216     ierr = -1;
0217     return result;
0218   }
0219   result = read(alivec, iret);
0220   if (iret != 0) {
0221     ierr = -2;
0222     return result;
0223   }
0224   iret = close();
0225   if (iret != 0) {
0226     ierr = -3;
0227     return result;
0228   }
0229 
0230   return result;
0231 }