CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Alignment/HIPAlignmentAlgorithm/src/HIPUserVariablesIORoot.cc

Go to the documentation of this file.
00001 #include "TTree.h"
00002 
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 #include "Alignment/CommonAlignment/interface/Alignable.h"
00006 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00007 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariables.h"
00008 
00009 
00010 // this class's header
00011 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariablesIORoot.h"
00012 
00013 // ----------------------------------------------------------------------------
00014 // constructor
00015 
00016 HIPUserVariablesIORoot::HIPUserVariablesIORoot() :
00017   ObjId(0), Id(0),Nhit(0), Nparj(0), Npare(0),
00018   AlignableChi2(0.), AlignableNdof(0)
00019 {
00020   treename = "T9";
00021   treetxt = "HIP User Variables";
00022   
00023   for (int i=0;i<nparmax*(nparmax+1)/2;++i) 
00024     Jtvj[i] = 0.;
00025   for (int i=0;i<nparmax;++i) 
00026     Jtve[i] = 0.;
00027 }
00028 
00029 // ----------------------------------------------------------------------------
00030 
00031 void HIPUserVariablesIORoot::createBranches(void) 
00032 {
00033   tree->Branch("Id",        &Id,        "Id/i");
00034   tree->Branch("ObjId",     &ObjId,     "ObjId/I");
00035 
00036   tree->Branch("Nhit",      &Nhit,      "Nhit/I");
00037   tree->Branch("Nparj",     &Nparj,     "Nparj/I");
00038   tree->Branch("Jtvj",      &Jtvj,      "Jtvj[Nparj]/D");
00039   tree->Branch("Npare",     &Npare,     "Npare/I");
00040   tree->Branch("Jtve",      &Jtve,      "Jtve[Npare]/D");
00041   tree->Branch("AlignableChi2",          &AlignableChi2, "AlignableChi2/D");
00042   tree->Branch("AlignableNdof",          &AlignableNdof, "AlignableNdof/i");
00043 }
00044 
00045 // ----------------------------------------------------------------------------
00046 
00047 void HIPUserVariablesIORoot::setBranchAddresses(void) 
00048 {
00049   tree->SetBranchAddress("Id",        &Id);
00050   tree->SetBranchAddress("ObjId",     &ObjId);
00051 
00052   tree->SetBranchAddress("Nhit",      &Nhit);
00053   tree->SetBranchAddress("Nparj",     &Nparj);
00054   tree->SetBranchAddress("Jtvj",      &Jtvj);
00055   tree->SetBranchAddress("Npare",     &Npare);
00056   tree->SetBranchAddress("Jtve",      &Jtve);
00057   tree->SetBranchAddress("AlignableChi2",     &AlignableChi2);
00058   tree->SetBranchAddress("AlignableNdof",      &AlignableNdof);
00059 
00060 }
00061 
00062 // ----------------------------------------------------------------------------
00063 // find tree entry based on detID and typeID
00064 
00065 int HIPUserVariablesIORoot::findEntry(unsigned int detId,int comp)
00066 {
00067   if (newopen) { // we're here for the first time
00068     edm::LogInfo("Alignment") <<"[HIPUserVariablesIORoot::findEntry] fill map ...";
00069     treemap.erase(treemap.begin(),treemap.end());
00070     for (int ev = 0;ev<tree->GetEntries();ev++) {
00071       tree->GetEntry(ev); 
00072       treemap[std::make_pair(Id,ObjId)]=ev;
00073     }
00074     newopen=false;
00075   }
00076   
00077   // now we have filled the map
00078   treemaptype::iterator imap = treemap.find(std::make_pair(detId,comp));
00079   int result=-1;
00080   if (imap != treemap.end()) result=(*imap).second;
00081   return result;
00082 
00083 
00084   //double noAliPar = tree->GetEntries();
00085   //for (int ev = 0;ev<noAliPar;ev++) {
00086   //  tree->GetEntry(ev); 
00087   //  if(Id==detId&&comp==ObjId) return (ev);
00088   //}
00089   //return(-1);
00090 }
00091 
00092 // ----------------------------------------------------------------------------
00093 
00094 int HIPUserVariablesIORoot::writeOne(Alignable* ali)
00095 {
00096   AlignmentParameters* ap=ali->alignmentParameters();
00097 
00098   if ((ap->userVariables())==0) { 
00099     edm::LogError("Alignment") <<"UserVariables not found!"; 
00100     return -1; 
00101   }
00102 
00103   HIPUserVariables* uvar = 
00104     dynamic_cast<HIPUserVariables*>(ap->userVariables());
00105 
00106   AlgebraicSymMatrix jtvj = uvar->jtvj;
00107   AlgebraicVector jtve = uvar->jtve;
00108   int nhit=uvar->nhit;
00109   int np=jtve.num_row();
00110 
00111   Nhit=nhit;
00112   Npare=np;
00113   Nparj=np*(np+1)/2;
00114   int count=0;
00115   for(int row=0;row<np;row++){
00116     Jtve[row]=jtve[row];
00117     for(int col=0;col<np;col++){
00118       if(row-1<col){Jtvj[count]=jtvj[row][col];count++;}
00119     }
00120   }
00121   Id = ali->id();
00122   ObjId = ali->alignableObjectId();
00123 
00124   //Chi^2 of alignable
00125   AlignableChi2= uvar->alichi2 ;
00126   AlignableNdof= uvar->alindof ;
00127 
00128   tree->Fill();
00129   return 0;
00130 }
00131 
00132 // ----------------------------------------------------------------------------
00133 
00134 AlignmentUserVariables* HIPUserVariablesIORoot::readOne(Alignable* ali, 
00135   int& ierr)
00136 {
00137   ierr=0;
00138   HIPUserVariables* uvar;
00139 
00140   int entry = findEntry(ali->id(), ali->alignableObjectId());
00141   if(entry!=-1) {
00142     tree->GetEntry(entry);
00143 
00144     int np=Npare;
00145     AlgebraicVector jtve(np,0);
00146     AlgebraicSymMatrix jtvj(np,0);
00147     int count=0;
00148     for(int row=0;row<np;row++) {
00149       jtve[row]=Jtve[row];
00150       for(int col=0; col < np;col++) {
00151         if(row-1<col) {jtvj[row][col]=Jtvj[count];count++;}
00152       }
00153     } 
00154 
00155     uvar = new HIPUserVariables(np);
00156     uvar->jtvj=jtvj;
00157     uvar->jtve=jtve;
00158     uvar->nhit=Nhit;
00159 
00160     //Chi2n
00161     uvar->alichi2=AlignableChi2;
00162     uvar->alindof=AlignableNdof;
00163 
00164     return uvar;
00165   }
00166 
00167   //  ierr=-1;
00168   return 0 ;
00169 }
00170 
00171 //-----------------------------------------------------------------------------
00172 
00173 void 
00174 HIPUserVariablesIORoot::writeHIPUserVariables (const Alignables& alivec, 
00175   const char* filename, int iter, bool validCheck, int& ierr)
00176 {
00177   ierr=0;
00178   int iret;
00179   iret = open(filename,iter,true);
00180   if (iret!=0) { ierr=-1; return;}
00181   iret = write(alivec,validCheck);
00182   if (iret!=0) { ierr=-2; return;}
00183   iret = close();
00184   if (iret!=0) { ierr=-3; return;}
00185 }
00186 
00187 //-----------------------------------------------------------------------------
00188 
00189 std::vector<AlignmentUserVariables*> 
00190 HIPUserVariablesIORoot::readHIPUserVariables (const Alignables& alivec, 
00191   const char* filename, int iter, int& ierr)
00192 {
00193   std::vector<AlignmentUserVariables*> result;
00194   ierr=0;
00195   int iret;
00196   iret = open(filename,iter,false);
00197   if (iret!=0) { ierr=-1; return result;}
00198   result = read(alivec,iret);
00199   if (iret!=0) { ierr=-2; return result;}
00200   iret = close();
00201   if (iret!=0) { ierr=-3; return result;}
00202 
00203   return result;
00204 }