CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/Alignment/MillePedeAlignmentAlgorithm/src/MillePedeVariablesIORoot.cc

Go to the documentation of this file.
00001 
00011 // this class's header
00012 #include "Alignment/MillePedeAlignmentAlgorithm/interface/MillePedeVariablesIORoot.h"
00013 
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 #include "Alignment/CommonAlignment/interface/Alignable.h"
00017 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00018 #include "Alignment/MillePedeAlignmentAlgorithm/interface/MillePedeVariables.h"
00019 
00020 #include "TTree.h"
00021 
00022 // -------------------------------------------------------------------------------------------------
00023 MillePedeVariablesIORoot::MillePedeVariablesIORoot() :
00024   myId(0), myObjId(0), myNumPar(0),
00025   myHitsX(0), myHitsY(0), myLabel(0)
00026 {
00027   treename = "MillePedeUser";
00028   treetxt = "MillePede User Variables";
00029   for (unsigned int i=0;i<kMaxNumPar;i++) {
00030     myIsValid[i] = 0;
00031     myDiffBefore[i] = 0.;
00032     myGlobalCor[i] = 0.;
00033     myPreSigma[i] = 0.;
00034     myParameter[i] = 0.;
00035     mySigma[i] = 0.;
00036   }
00037 }
00038 
00039 // -------------------------------------------------------------------------------------------------
00040 void MillePedeVariablesIORoot::writeMillePedeVariables
00041 (const std::vector<Alignable*> &alivec, const char *filename, int iter, bool validCheck, int &ierr)
00042 {
00043   ierr = 0;
00044 
00045   int iret = this->open(filename, iter, true); 
00046   if (iret != 0) {
00047     ierr = -1; 
00048   } else {
00049     iret = this->write(alivec, validCheck);
00050     tree->BuildIndex("Id", "ObjId");
00051     if (iret != 0) {
00052       ierr = -2;
00053     } else {
00054       iret = this->close();
00055       if (iret != 0) {
00056         ierr = -3;
00057       }
00058     }
00059   }
00060   
00061   return;
00062 }
00063 
00064 // -------------------------------------------------------------------------------------------------
00065 std::vector<AlignmentUserVariables*> MillePedeVariablesIORoot::readMillePedeVariables
00066 (const std::vector<Alignable*> &alivec, const char *filename, int iter, int &ierr)
00067 {
00068   std::vector<AlignmentUserVariables*> result;
00069   ierr = 0;
00070   int iret = this->open(filename, iter, false);
00071   if (iret != 0) {
00072     ierr = -1;
00073   } else {
00074     result = this->read(alivec, iret);
00075     if (iret != 0) {
00076       ierr = -2;
00077     } else {
00078       iret = this->close();
00079       if (iret != 0) {
00080         ierr = -3;
00081       }
00082     }
00083   }
00084 
00085   return result;
00086 }
00087 
00088 // -------------------------------------------------------------------------------------------------
00089 int MillePedeVariablesIORoot::writeOne(Alignable* ali)
00090 {
00091   if (!ali || !ali->alignmentParameters() 
00092       || !dynamic_cast<MillePedeVariables*>(ali->alignmentParameters()->userVariables())) {
00093     edm::LogError("Alignment") << "@SUB=MillePedeVariablesIORoot::writeOne"
00094                                << "No MillePedeVariables found!"; 
00095     return -1;
00096   }
00097 
00098   const MillePedeVariables *mpVar = 
00099     static_cast<MillePedeVariables*>(ali->alignmentParameters()->userVariables());
00100   myNumPar = mpVar->size();
00101   if (myNumPar >= kMaxNumPar) {
00102     edm::LogError("Alignment") << "@SUB=MillePedeVariablesIORoot::writeOne"
00103                                << "Ignoring parameters " << static_cast<int>(kMaxNumPar) << " to " << myNumPar-1;
00104     myNumPar = kMaxNumPar;
00105   }
00106 
00107   for (unsigned int iPar = 0; iPar < myNumPar; ++iPar) {
00108     myIsValid[iPar]    = mpVar->isValid()[iPar];
00109     myDiffBefore[iPar] = mpVar->diffBefore()[iPar];
00110     myGlobalCor[iPar]  = mpVar->globalCor()[iPar];
00111     myPreSigma[iPar]   = mpVar->preSigma()[iPar];
00112     myParameter[iPar]  = mpVar->parameter()[iPar];
00113     mySigma[iPar]      = mpVar->sigma()[iPar];
00114   }
00115   myHitsX = mpVar->hitsX();
00116   myHitsY = mpVar->hitsY();
00117   myLabel = mpVar->label();
00118 
00119   myId = ali->id();
00120   myObjId = ali->alignableObjectId();
00121 
00122   tree->Fill();
00123 
00124   return 0;
00125 }
00126 
00127 // -------------------------------------------------------------------------------------------------
00128 AlignmentUserVariables* MillePedeVariablesIORoot::readOne(Alignable *ali, int &ierr)
00129 {
00130   ierr = 0;
00131 
00132   if (tree->GetEntryWithIndex(ali->id(), ali->alignableObjectId()) < 0) {
00133     edm::LogError("Alignment") << "@SUB=MillePedeVariablesIORoot::readOne"
00134                                << "No index for id/type = (" << ali->id() << "/"
00135                                << ali->alignableObjectId() << ") found!";
00136     ierr = 1;
00137     return 0;
00138   }
00139 
00140   MillePedeVariables *mpVar = new MillePedeVariables(myNumPar, myLabel);
00141   for (unsigned int iPar = 0; iPar < myNumPar; ++iPar) {
00142     mpVar->isValid()[iPar]    = myIsValid[iPar];
00143     mpVar->diffBefore()[iPar] = myDiffBefore[iPar];
00144     mpVar->globalCor()[iPar]  = myGlobalCor[iPar];
00145     mpVar->preSigma()[iPar]   = myPreSigma[iPar];
00146     mpVar->parameter()[iPar]  = myParameter[iPar];
00147     mpVar->sigma()[iPar]      = mySigma[iPar];
00148   }
00149   mpVar->setHitsX(myHitsX);
00150   mpVar->setHitsY(myHitsY);
00151   
00152   return mpVar;
00153 }
00154 
00155 // -------------------------------------------------------------------------------------------------
00156 void MillePedeVariablesIORoot::createBranches() 
00157 {
00158   tree->Branch("Id",        &myId,        "Id/i");
00159   tree->Branch("ObjId",     &myObjId,     "ObjId/I");
00160   tree->Branch("NumPar",    &myNumPar,    "NumPar/i");
00161   tree->Branch("IsValid",    myIsValid,   "IsValid[NumPar]/b");
00162   tree->Branch("DiffBefore", myDiffBefore,"DiffBefore[NumPar]/F");
00163   tree->Branch("GlobalCor",  myGlobalCor, "GlobalCor[NumPar]/F");
00164   tree->Branch("PreSigma",   myPreSigma,  "PreSigma[NumPar]/F");
00165   tree->Branch("Par",        myParameter, "Par[NumPar]/F"); // name as in AlignmentParametersIORoot
00166   tree->Branch("Sigma",      mySigma,     "Sigma[NumPar]/F");
00167   tree->Branch("HitsX",     &myHitsX,     "HitsX/i");
00168   tree->Branch("HitsY",     &myHitsY,     "HitsY/i");
00169   tree->Branch("Label",     &myLabel,     "Label/i");
00170 }
00171 
00172 // -------------------------------------------------------------------------------------------------
00173 void MillePedeVariablesIORoot::setBranchAddresses() 
00174 {
00175   tree->SetBranchAddress("Id",        &myId);
00176   tree->SetBranchAddress("ObjId",     &myObjId);
00177   tree->SetBranchAddress("NumPar",    &myNumPar);
00178   tree->SetBranchAddress("IsValid",    myIsValid);
00179   tree->SetBranchAddress("DiffBefore", myDiffBefore);
00180   tree->SetBranchAddress("GlobalCor",  myGlobalCor);
00181   tree->SetBranchAddress("PreSigma",   myPreSigma);
00182   tree->SetBranchAddress("Par",        myParameter);
00183   tree->SetBranchAddress("Sigma",      mySigma);
00184   tree->SetBranchAddress("HitsX",     &myHitsX);
00185   tree->SetBranchAddress("HitsY",     &myHitsY);
00186   tree->SetBranchAddress("Label",     &myLabel);
00187 }