00001
00009 #include "Alignment/MuonStandaloneAlgorithm/plugins/StandaloneMerger.h"
00010
00011
00012 #include "FWCore/Framework/interface/MakerMacros.h"
00013 #include "FWCore/Framework/interface/Frameworkfwd.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016
00017 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
00018 #include "Geometry/CommonDetUnit/interface/GlobalTrackingGeometry.h"
00019 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00020 #include "DataFormats/DetId/interface/DetId.h"
00021 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00022 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00023
00024 #include "Alignment/MuonStandaloneAlgorithm/interface/AlignmentDetector.h"
00025 #include "Alignment/MuonStandaloneAlgorithm/interface/PointForAlignment.h"
00026 #include "Alignment/MuonStandaloneAlgorithm/interface/TrackForAlignment.h"
00027 #include "Alignment/MuonStandaloneAlgorithm/interface/AlignmentDetectorCollection.h"
00028 #include "Alignment/MuonStandaloneAlgorithm/interface/TrackForAlignmentCollection.h"
00029
00030 #include "FWCore/ServiceRegistry/interface/Service.h"
00031
00032
00033
00034 #include "TFile.h"
00035 #include "TH1F.h"
00036 #include <stdio.h>
00037 #include <fstream>
00038
00039 using namespace std;
00040 using namespace edm;
00041
00042
00043
00045 StandaloneMerger::StandaloneMerger(const ParameterSet& pset){
00046
00047 maxFiles = pset.getParameter<int>("maxFiles");
00048 surveyFileName = pset.getParameter<std::string>("surveyFileName");
00049 outputFile = pset.getParameter<std::string>("outputFileName");
00050 submatrices = pset.getParameter<std::string>("subMatrixDir");
00051 }
00052
00053
00055 StandaloneMerger::~StandaloneMerger(){
00056 }
00057
00058 void StandaloneMerger::updateMatrix(TMatrixD *Cm, TMatrixD *bm, TMatrixD *in, TMatrixD *numberOfTracks) {
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 TMatrixD realIndex(in->GetNrows(), 1);
00071
00072 N(0,0) += (*numberOfTracks)(0,0);
00073
00074 for(int indexCounter = 0; indexCounter < in->GetNrows(); ++indexCounter) {
00075 bool oldIndex = false;
00076 for(int theIndexCounter = 0; theIndexCounter < theIndex.GetNrows(); ++theIndexCounter) {
00077 if(theIndex(theIndexCounter, 0) == (*in)(indexCounter, 0)) {
00078 realIndex(indexCounter, 0) = theIndexCounter;
00079 oldIndex = true;
00080 break;
00081 }
00082 }
00083 if(oldIndex == false) {
00084 theIndex.ResizeTo(theIndex.GetNrows()+1,1);
00085 C.ResizeTo(C.GetNrows()+NDOFAlign, C.GetNcols()+NDOFAlign);
00086 b.ResizeTo(b.GetNrows()+NDOFAlign, 1);
00087 theIndex(theIndex.GetNrows()-1, 0) = (*in)(indexCounter, 0);
00088 realIndex(indexCounter, 0) = theIndex.GetNrows()-1;
00089 }
00090 }
00091
00092 for(int param = 0; param < NDOFAlign; param++) {
00093 for(int rowCounter = 0; rowCounter < in->GetNrows(); rowCounter++) {
00094 for(int param2 = 0; param2 < NDOFAlign; param2++) {
00095 for(int columnCounter = 0; columnCounter < in->GetNrows(); columnCounter++) {
00096 C((int)realIndex(rowCounter,0)*NDOFAlign+param, (int)realIndex(columnCounter,0)*NDOFAlign+param2) += (*Cm)(rowCounter*NDOFAlign+param, columnCounter*NDOFAlign+param2);
00097 if(C((int)realIndex(rowCounter,0)*NDOFAlign+param, (int)realIndex(columnCounter,0)*NDOFAlign+param2) > 1e100) {
00098 std::cout << "Error. Anormal values of matrix: " << std::endl;
00099 }
00100 }
00101 }
00102 b((int)realIndex(rowCounter,0)*NDOFAlign+param,0) += (*bm)(rowCounter*NDOFAlign+param, 0);
00103 }
00104 }
00105 }
00106
00107
00108 void StandaloneMerger::mergeMatrix() {
00109
00110 for(int counterOfFiles = 0; counterOfFiles < maxFiles; ++counterOfFiles) {
00111
00112 char name[100];
00113 sprintf(name, "%s/SubMatrices_%d.root", submatrices.c_str(), counterOfFiles);
00114
00115 f = new TFile(name, "READ");
00116 if(f->IsZombie()) continue;
00117 std::cout << "Analyzing: " << name << std::endl;
00118 TMatrixD *Cm = (TMatrixD *)f->Get("CMatrix");
00119 TMatrixD *bm = (TMatrixD *)f->Get("bMatrix");
00120 TMatrixD *in = (TMatrixD *)f->Get("Index");
00121 TMatrixD *numberOfTracks = (TMatrixD *)f->Get("numberOfTracks");
00122 updateMatrix(Cm, bm, in, numberOfTracks);
00123
00124 f->Close();
00125 delete f;
00126 delete Cm;
00127 delete bm;
00128 delete in;
00129 }
00130 }
00131
00132
00133 void StandaloneMerger::beginJob(const EventSetup& eventSetup){
00134
00135
00136
00137 C.ResizeTo(0,0);
00138 b.ResizeTo(0,0);
00139 theIndex.ResizeTo(0,0);
00140 N.ResizeTo(1,1);
00141
00142
00143 mergeMatrix();
00144 surveyAdd();
00145 CMatrixInvert.ResizeTo(C.GetNrows(), C.GetNcols());
00146 CMatrixInvert = TotalMatrix;
00147 CMatrixInvert.Invert();
00148 Delta.ResizeTo(C.GetNrows(), 1);
00149 Delta = CMatrixInvert*TotalVector;
00150 TMatrixD unit, unit2;
00151 unit.ResizeTo(C.GetNrows(), C.GetNcols());
00152 unit2.ResizeTo(C.GetNrows(), C.GetNcols());
00153 unit.UnitMatrix();
00154 unit2 = unit-CMatrixInvert*TotalMatrix;
00155 std::cout << "Inversion check: " << unit2.E2Norm() << std::endl;
00156
00157 }
00158
00159
00160
00161 void StandaloneMerger::BuildDelta() {
00162
00163 Delta.ResizeTo(C.GetNrows(), 1);
00164 for(int j = C.GetNrows()-1; j >= 0; --j) {
00165 double acum = 0.0;
00166 for(int i = C.GetNrows()-1; i > j; --i) {
00167 acum += R(j, i)*Delta(i,0);
00168 }
00169 Delta(j,0) = (IndependentVector(j,0)-acum)/R(j,j);
00170 }
00171 }
00172
00173
00174
00175
00176 bool StandaloneMerger::CalculateSolution() {
00177
00178
00179 Q.ResizeTo(CMatrixInvert.GetNrows(), CMatrixInvert.GetNcols());
00180 R.ResizeTo(CMatrixInvert.GetNrows(), CMatrixInvert.GetNcols());
00181 IndependentVector.ResizeTo(CMatrixInvert.GetNrows(), 1);
00182 return true;
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 }
00195
00196
00197 void StandaloneMerger::endJob(){
00198
00199
00200
00201 theFile = TFile::Open(outputFile.c_str(), "RECREATE");
00202 theFile->cd();
00203
00204 C.Write("CMatrix");
00205 b.Write("bMatrix");
00206 Delta.Write("Delta");
00207 CMatrixInvert.Write("CMatrixInvert");
00208 theIndex.Write("Index");
00209 N.Write("numberOfTracks");
00210 SurveyMatrix.Write("SurveyMatrix");
00211 SurveyVector.Write("SurveyVector");
00212 TotalMatrix.Write("TotalMatrix");
00213 TotalVector.Write("TotalVector");
00214
00215
00216
00217
00218 theFile->Close();
00219
00220 }
00221
00222
00223
00224 void StandaloneMerger::analyze(const Event & event, const EventSetup& eventSetup){}
00225
00226
00227 void StandaloneMerger::surveyAdd() {
00228
00229 SurveyMatrix.ResizeTo(theIndex.GetNrows()*NDOFAlign, theIndex.GetNrows()*NDOFAlign);
00230 SurveyVector.ResizeTo(theIndex.GetNrows()*NDOFAlign, 1);
00231 TotalMatrix.ResizeTo(theIndex.GetNrows()*NDOFAlign, theIndex.GetNrows()*NDOFAlign);
00232 TotalVector.ResizeTo(theIndex.GetNrows()*NDOFAlign, 1);
00233
00234
00235
00236 TMatrixD SurveyDat(theIndex.GetNrows()*NDOFAlign, 1);
00237
00238 TFile surveyFile(surveyFileName.c_str());
00239 TMatrixD *SurveyCov = (TMatrixD *)surveyFile.Get("SurveyCov");
00240 TMatrixD *SurveyData = (TMatrixD *)surveyFile.Get("SurveyData");
00241 TMatrixD *SurveyIndex = (TMatrixD *)surveyFile.Get("SurveyIndex");
00242
00243
00244
00245 std::vector<long> realIndex;
00246 for(int counter = 0; counter < theIndex.GetNrows(); ++counter) {
00247 for(int counterC = 0; counterC < SurveyIndex->GetNrows(); ++counterC) {
00248 if(theIndex(counter,0) == (*SurveyIndex)(counterC,0)) {
00249 realIndex.push_back(counterC);
00250 break;
00251 }
00252 }
00253 }
00254
00255 for(int counteri = 0; counteri < theIndex.GetNrows(); ++counteri) {
00256 for(int alignN = 0; alignN < NDOFAlign; ++alignN) {
00257 SurveyDat(counteri*NDOFAlign+alignN, 0) = (*SurveyData)(realIndex.at(counteri)*NDOFAlign+alignN, 0);
00258 for(int alignN2 = 0; alignN2 < NDOFAlign; ++alignN2) {
00259 for(int counterj = 0; counterj < theIndex.GetNrows(); ++counterj) {
00260 SurveyMatrix(counteri*NDOFAlign+alignN, counterj*NDOFAlign+alignN2) = (*SurveyCov)(realIndex.at(counteri)*NDOFAlign+alignN, realIndex.at(counterj)*NDOFAlign+alignN2);
00261 }
00262 }
00263 }
00264 }
00265
00266 SurveyVector = SurveyMatrix*SurveyDat;
00267
00268 for(int i = 0; i < SurveyMatrix.GetNrows(); ++i) {
00269 for(int j = 0; j < SurveyMatrix.GetNcols(); ++j) {
00270 SurveyMatrix(i,j) = SurveyMatrix(i,j);
00271 }
00272 SurveyVector(i,0) = SurveyVector(i,0);
00273 }
00274
00275
00276 TotalMatrix = SurveyMatrix + C;
00277 TotalVector = SurveyVector + b;
00278
00279
00280
00281 surveyFile.Close();
00282
00283 }
00284
00285
00286
00287
00288 DEFINE_FWK_MODULE(StandaloneMerger);