19#include "BmnSiliconHit.h"
21Int_t BmnGlobalAlignment::fCurrentEvent = 0;
27 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++)
28 delete [] fixedGemElements[iStat];
29 delete [] fixedGemElements;
31 delete [] fDetectorSet;
36 system(
"cp millepede.res Millepede.res");
38 system(
"rm millepede.*");
46 fGemAlignCorr(nullptr),
50 fGlobalTracks(nullptr),
51 fUseRealHitErrors(kFALSE),
52 fChi2MaxPerNDF(LDBL_MAX),
58 fIsExcludedTx(kFALSE),
59 fIsExcludedTy(kFALSE),
64 fNumOfIterations(500000),
67 fUseRegularization(kFALSE),
70 fOutlierdownweighting(0),
77 fUseConstraints(kTRUE),
79 fMisAlignFile(misAlignFile),
80 fBmnGemMisalign(nullptr)
84 fRecoFileName = inFileName;
86 fBranchSiHits =
"BmnSiliconHit";
87 fBranchGemHits =
"BmnGemStripHit";
89 fBranchGemTracks =
"BmnGemTrack";
90 fBranchSilTracks =
"BmnSiliconTrack";
91 fBranchGlobalTracks =
"BmnGlobalTrack";
93 fBranchGemAlignCorr =
"BmnGemAlignCorrections";
94 fBranchSiAlignCorr =
"BmnSiliconAlignCorrections";
96 fBranchGemResiduals =
"BmnResiduals";
97 fBranchFairEventHeader =
"EventHeader.";
99 CreateDetectorGeometries();
104 cout <<
" BmnGlobalAlignment::Init() " << endl;
105 cout <<
"Use detectors: GEM - " << fDetectorSet[0] <<
" SILICON - " << fDetectorSet[1] << endl;
107 TChain* chain =
new TChain(
"bmndata");
108 chain->Add(fRecoFileName.Data());
109 FairEventHeader* evHeader = NULL;
110 chain->SetBranchAddress(fBranchFairEventHeader.Data(), &evHeader);
114 Double_t fieldVolt = 0.;
121 fIsField = (fieldVolt > 10.) ? kTRUE : kFALSE;
124 FairRootManager* ioman = FairRootManager::Instance();
126 fSiHits = (TClonesArray*) ioman->GetObject(fBranchSiHits.Data());
127 fGemHits = (TClonesArray*) ioman->GetObject(fBranchGemHits.Data());
129 fSilTracks = (TClonesArray*) ioman->GetObject(fBranchSilTracks.Data());
130 fGemTracks = (TClonesArray*) ioman->GetObject(fBranchGemTracks.Data());
131 fGlobalTracks = (TClonesArray*) ioman->GetObject(fBranchGlobalTracks.Data());
133 fFairEventHeader = (FairEventHeader*) ioman->GetObject(fBranchFairEventHeader.Data());
135 fGemAlignCorr =
new TClonesArray(fBranchGemAlignCorr.Data());
136 fSiAlignCorr =
new TClonesArray(fBranchSiAlignCorr.Data());
138 ioman->Register(fBranchGemAlignCorr.Data(),
"GEM", fGemAlignCorr, kTRUE);
139 ioman->Register(fBranchSiAlignCorr.Data(),
"SI", fSiAlignCorr, kTRUE);
141 fChain = ioman->GetInChain();
143 Char_t* geoFileName = (Char_t*)
"current_geo_file.root";
146 cout <<
"Geometry file can't be read from the database" << endl;
149 TGeoManager::Import(geoFileName);
152 TChain* ch =
new TChain(
"bmndata");
153 ch->Add(fMisAlignFile.Data());
154 ch->SetBranchAddress(fBranchGemAlignCorr.Data(), &fBmnGemMisalign);
162 fFairEventHeader->SetMCEntryNumber(fCurrentEvent);
163 fFairEventHeader->SetRunId(fRunId);
165 if (fCurrentEvent % 1000 == 0)
166 cout <<
"Event# = " << fCurrentEvent << endl;
171 for (Int_t iGlobTrack = 0; iGlobTrack < fGlobalTracks->GetEntriesFast(); iGlobTrack++) {
178 Double_t chi2 = globTrack->
GetChi2();
179 Int_t ndf = globTrack->
GetNDF();
180 Int_t nHits = globTrack->
GetNHits();
181 Double_t Tx = params->GetTx();
182 Double_t Ty = params->GetTy();
185 if (Tx < fTxMin || Tx > fTxMax || Ty < fTyMin || Ty > fTyMax || nHits < fMinHitsAccepted || chi2 / ndf > fChi2MaxPerNDF)
189 if (fIsExcludedTx && Tx > fTxLeft && Tx < fTxRight)
191 if (fIsExcludedTy && Ty > fTyLeft && Ty < fTyRight)
194 Int_t* idx =
new Int_t[nDetectors];
198 vector <BmnMilleContainer*>
GEM;
199 vector <BmnMilleContainer*>
SILICON;
201 for (Int_t iDet = 0; iDet < nDetectors; iDet++) {
202 if (!fDetectorSet[iDet] || idx[iDet] == -1 || fIsField)
206 MilleNoFieldRuns(globTrack, idx[iDet], iDet,
GEM,
SILICON);
209 fCONTAINER[fNTracks] = pair <vector <BmnMilleContainer*>, vector < BmnMilleContainer*>> (
SILICON,
GEM);
214 if (fNEvents == fCurrentEvent) {
227 vector <Double_t> locDerX;
228 vector <Double_t> locDerY;
229 locDerX.push_back(1.);
230 locDerX.push_back(hit->GetZ());
231 locDerX.push_back(0.);
232 locDerX.push_back(0.);
234 locDerY.push_back(0.);
235 locDerY.push_back(0.);
236 locDerY.push_back(1.);
237 locDerY.push_back(hit->GetZ());
239 vector <Double_t> globDerX;
240 vector <Double_t> globDerY;
241 globDerX.push_back(1.);
242 globDerX.push_back(0.);
245 globDerY.push_back(0.);
246 globDerY.push_back(1.);
253 mille->
SetDMeasures(fUseRealHitErrors ? 2. * hit->GetDx() : 1., fUseRealHitErrors ? hit->GetDy() : 1.);
258void BmnGlobalAlignment::MilleNoFieldRuns(
BmnGlobalTrack* glTrack, Int_t idx, Int_t iDet, vector <BmnMilleContainer*>&
GEM, vector <BmnMilleContainer*>&
SILICON) {
262 for (Int_t iHit = 0; iHit < track->
GetNHits(); iHit++) {
264 GEM.push_back(FillMilleContainer(glTrack, hit));
271 for (Int_t iHit = 0; iHit < track->
GetNHits(); iHit++) {
273 SILICON.push_back(FillMilleContainer(glTrack, hit));
278void BmnGlobalAlignment::_Mille(Double_t* DerLc, Double_t* DerGl,
BmnMille* Mille, Int_t iTrack) {
279 fITERATOR = next(fCONTAINER.begin(), iTrack);
281 TString* dets =
new TString[nDetectors];
285 for (Int_t iDet = 0; iDet < nDetectors; iDet++) {
286 vector <BmnMilleContainer*> cont;
287 if (dets[iDet].Contains(
"GEM"))
288 cont = fITERATOR->second.second;
289 else if (dets[iDet].Contains(
"SILICON"))
290 cont = fITERATOR->second.first;
292 cout <<
"Wrong input data! Exiting ..." << endl;
296 const Int_t nLays = 2;
297 TString lays[nLays] = {
"X",
"Y"};
299 for (
size_t iEle = 0; iEle < cont.size(); iEle++)
302 for (Int_t iLoc = 0; iLoc < fNLC; iLoc++)
304 for (Int_t iGlob = 0; iGlob < fNGL; iGlob++)
307 for (Int_t iLay = 0; iLay < nLays; iLay++) {
308 vector <Double_t> locDers = _cont->
GetLocDers(lays[iLay]);
309 DerLc[0] = locDers[0];
310 DerLc[1] = locDers[1];
311 DerLc[2] = locDers[2];
312 DerLc[3] = locDers[3];
314 Int_t idx = dets[iDet].Contains(
"GEM") ? GemStatModLabel(_cont->
GetStation(), _cont->
GetModule()) :
315 SiliconStatModLabel(_cont->GetStation(), _cont->GetModule());
316 Int_t idxUp = idx - 1;
317 Int_t idxMed = idxUp - 1;
318 Int_t idxLow = idxMed - 1;
320 vector <Double_t> globDers = _cont->
GetGlobDers(lays[iLay]);
321 DerGl[idxLow] = globDers[0];
322 DerGl[idxMed] = globDers[1];
323 DerGl[idxUp] = globDers[2];
327 Mille->
mille(fNLC, DerLc, fNGL, DerGl, Labels, meas, dmeas);
330 cout << dets[iDet] << endl;
331 cout << lays[iLay] << endl;
333 cout <<
"Loc. ders : ";
334 for (Int_t iLoc = 0; iLoc < fNLC; iLoc++)
335 cout << DerLc[iLoc] <<
" ";
337 cout <<
"Glob. ders : ";
338 for (Int_t iGlob = 0; iGlob < fNGL; iGlob++)
339 cout << DerGl[iGlob] <<
" ";
341 cout <<
"Meas = " << meas <<
" dMeas = " << dmeas << endl;
349const Int_t BmnGlobalAlignment::MakeBinFile() {
350 const Int_t ngl_per_subdetector = 3;
353 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++)
357 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++)
361 Labels =
new Int_t[fNGL];
362 for (Int_t iEle = 0; iEle < fNGL; iEle++)
363 Labels[iEle] = 1 + iEle;
367 Int_t nTracks = fCONTAINER.size();
368 Double_t DerLc[fNLC];
369 Double_t DerGl[fNGL];
372 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
374 cout <<
"Track# " << iTrack << endl;
375 _Mille(DerLc, DerGl, Mille, iTrack);
377 if (iTrack % 1000 == 0 && !fDebug)
378 cout <<
"Track processed# " << iTrack << endl;
385void BmnGlobalAlignment::MakeSteerFile() {
412 FILE* steer = fopen(
"steer.txt",
"w");
413 TString alignType =
"alignment.bin";
414 fprintf(steer,
"%s\n", alignType.Data());
415 fprintf(steer,
"method inversion %d %f\n", fNumOfIterations, fAccuracy);
416 if (fUseRegularization)
417 fprintf(steer,
"regularization 1.0\n");
418 fprintf(steer,
"hugecut %G\n", fHugecut);
419 if (fChisqcut[0] * fChisqcut[1] != 0)
420 fprintf(steer,
"chisqcut %G %G\n", fChisqcut[0], fChisqcut[1]);
421 fprintf(steer,
"entries %d\n", fEntries);
422 fprintf(steer,
"outlierdownweighting %d\n", fOutlierdownweighting);
423 fprintf(steer,
"dwfractioncut %G\n", fDwfractioncut);
424 fprintf(steer,
"Parameter\n");
426 Int_t parCounterGem = 0;
427 Int_t parCounterSi = 0;
428 const Int_t nParams = 3;
430 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++)
431 for (Int_t iMod = 0; iMod < fDetectorGEM->
GetGemStation(iStat)->GetNModules(); iMod++)
434 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++)
435 for (Int_t iMod = 0; iMod < fDetectorSI->
GetSiliconStation(iStat)->GetNModules(); iMod++)
439 for (Int_t iDet = 0; iDet < nDetectors; iDet++) {
441 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++) {
442 for (Int_t iPar = 0; iPar < fDetectorGEM->
GetGemStation(iStat)->GetNModules() * nParams; iPar++) {
443 if (!fDetectorSet[0])
444 fprintf(steer,
"%d %G %G\n", Labels[startIdx + iPar], 0., -1.);
446 fprintf(steer,
"%d %G %G\n", Labels[startIdx + iPar], 0., (fixedGemElements[iStat][iPar / nParams]) ? -1. : fPreSigma);
450 }
else if (iDet == 1) {
451 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++) {
452 for (Int_t iPar = 0; iPar < fDetectorSI->
GetSiliconStation(iStat)->GetNModules() * nParams; iPar++) {
453 if (!fDetectorSet[1])
454 fprintf(steer,
"%d %G %G\n", Labels[startIdx + iPar], 0., -1.);
456 fprintf(steer,
"%d %G %G\n", Labels[startIdx + iPar], 0., (fixedSiElements[iStat][iPar / nParams]) ? -1. : (fIsDoTest) ? -1. : fPreSigma);
463 if (!fUseConstraints) {
469 map <pair <Int_t, Int_t>, Double_t> z_GEM;
470 map <pair <Int_t, Int_t>, Double_t> z_SI;
472 if (fDetectorSet[0]) {
473 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++)
474 for (Int_t iMod = 0; iMod < fDetectorGEM->
GetGemStation(iStat)->GetNModules(); iMod++)
475 if (!fixedGemElements[iStat][iMod])
479 if (fDetectorSet[1]) {
480 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++)
481 for (Int_t iMod = 0; iMod < fDetectorSI->
GetSiliconStation(iStat)->GetNModules(); iMod++)
482 if (!fixedSiElements[iStat][iMod])
487 for (
auto it : z_GEM)
493 Double_t zC = zSum / (z_GEM.size() + z_SI.size());
494 cout << zC <<
" " << z_GEM.size() + z_SI.size() << endl;
497 map <pair <Int_t, Int_t>, Double_t> dZ_GEM;
498 map <pair <Int_t, Int_t>, Double_t> dZ_SI;
500 for (
auto it : z_GEM)
501 dZ_GEM[it.first] = it.second - zC;
504 dZ_SI[it.first] = it.second - zC;
506 map <Int_t, Double_t> deltas;
508 for (
auto it : dZ_GEM) {
509 Int_t stat = it.first.first;
510 Int_t mod = it.first.second;
511 Int_t labZ = GemStatModLabel(stat, mod);
512 Int_t labY = labZ - 1;
513 Int_t labX = labY - 1;
514 deltas[labX] = it.second;
515 deltas[labY] = it.second;
516 deltas[labZ] = it.second + zC;
519 for (
auto it : dZ_SI) {
520 Int_t stat = it.first.first;
521 Int_t mod = it.first.second;
522 Int_t labZ = SiliconStatModLabel(stat, mod);
523 Int_t labY = labZ - 1;
524 Int_t labX = labY - 1;
525 deltas[labX] = it.second;
526 deltas[labY] = it.second;
527 deltas[labZ] = it.second + zC;
530 if (!fDetectorSet[0] && !fDetectorSet[1])
537 for (Int_t iRemain = 0; iRemain < nParams; iRemain++) {
538 fprintf(steer,
"constraint 0.0\n");
540 for (Int_t iPar = 0; iPar < parCounterGem * nParams; iPar++) {
541 Int_t stat = GetGemStatMod(Labels[iPar])[0];
542 Int_t mod = GetGemStatMod(Labels[iPar])[1];
543 if (Labels[iPar] % nParams == iRemain && !fixedGemElements[stat][mod])
544 fprintf(steer,
"%d %G\n", Labels[iPar], 1.);
548 for (Int_t iPar = parCounterGem * nParams; iPar < fNGL; iPar++) {
549 Int_t stat = GetSiliconStatMod(Labels[iPar])[0];
550 Int_t mod = GetSiliconStatMod(Labels[iPar])[1];
551 if (Labels[iPar] % nParams == iRemain && !fixedSiElements[stat][mod])
552 fprintf(steer,
"%d %G\n", Labels[iPar], 1.);
559 for (Int_t iRemain = 0; iRemain < nParams; iRemain++) {
560 fprintf(steer,
"constraint 0.0\n");
561 for (
auto it : deltas) {
562 if (it.first <= parCounterGem * nParams) {
563 Int_t stat = GetGemStatMod(it.first)[0];
564 Int_t mod = GetGemStatMod(it.first)[1];
565 if (it.first % nParams == iRemain && !fixedGemElements[stat][mod])
566 fprintf(steer,
"%d %G\n", it.first, it.second);
568 Int_t stat = GetSiliconStatMod(it.first)[0];
569 Int_t mod = GetSiliconStatMod(it.first)[1];
570 if (it.first % nParams == iRemain && !fixedSiElements[stat][mod])
571 fprintf(steer,
"%d %G\n", it.first, it.second);
578void BmnGlobalAlignment::Pede() {
579 system(
"pede steer.txt");
580 ifstream resFile(
"millepede.res", ios::in);
581 ReadPedeOutput(resFile);
585void BmnGlobalAlignment::ReadPedeOutput(ifstream& resFile) {
587 cout <<
"BmnGlobalAlignment::ReadPedeOutput" <<
" No input file found!!" << endl;
590 resFile.ignore(numeric_limits<streamsize>::max(),
'\n');
592 const Int_t nParams = 3;
593 Double_t* corrs =
new Double_t[nParams];
596 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++) {
597 for (Int_t iMod = 0; iMod < fDetectorGEM->
GetGemStation(iStat)->GetNModules(); iMod++) {
598 ExtractCorrValues(resFile, corrs);
607 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++) {
609 ExtractCorrValues(resFile, corrs);
620void BmnGlobalAlignment::ExtractCorrValues(ifstream& resFile, Double_t* corrs) {
621 const Int_t nParams = 3;
622 TString parValue =
"", dummy =
"";
625 for (Int_t iCorr = 0; iCorr < nParams; iCorr++)
627 for (Int_t iLine = 0; iLine < nParams; iLine++) {
628 getline(resFile, line);
629 stringstream ss(line);
630 Int_t size = ss.str().length();
633 ss >> dummy >> parValue >> dummy;
635 ss >> dummy >> parValue >> dummy >> dummy >> dummy;
637 cout <<
"Unsupported format given!" << endl;
639 Int_t idx = (iLine % nParams == 0) ? 0 : (iLine % nParams == 1) ? 1 : 2;
640 corrs[idx] = -parValue.Atof();
644void BmnGlobalAlignment::CreateDetectorGeometries() {
645 fDetectorSet =
new Bool_t[nDetectors]();
648 Bool_t isSRC = (fRunId < 3589) ? kTRUE : kFALSE;
649 TString gPathConfig = gSystem->Getenv(
"VMCWORKDIR");
650 TString confSi =
"SiliconRun" + TString(isSRC ?
"SRC" :
"") +
"Spring2018.xml";
651 TString confGem =
"GemRun" + TString(isSRC ?
"SRC" :
"") +
"Spring2018.xml";
654 TString gPathSiliconConfig = gPathConfig +
"/parameters/silicon/XMLConfigs/";
658 fixedSiElements =
new Bool_t*[fDetectorSI->
GetNStations()];
659 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++) {
663 for (Int_t iStat = 0; iStat < fDetectorSI->
GetNStations(); iStat++)
664 for (Int_t iMod = 0; iMod < fDetectorSI->
GetSiliconStation(iStat)->GetNModules(); iMod++)
665 fixedSiElements[iStat][iMod] = kFALSE;
668 TString gPathGemConfig = gPathConfig +
"/parameters/gem/XMLConfigs/";
672 fixedGemElements =
new Bool_t*[fDetectorGEM->
GetNStations()];
673 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++) {
677 for (Int_t iStat = 0; iStat < fDetectorGEM->
GetNStations(); iStat++)
678 for (Int_t iMod = 0; iMod < fDetectorGEM->
GetGemStation(iStat)->GetNModules(); iMod++)
679 fixedGemElements[iStat][iMod] = kFALSE;
void SetCorrections(Double_t x, Double_t y, Double_t z)
void SetModule(Int_t num)
void SetStation(Int_t num)
Double_t GetZPositionRegistered()
BmnGemStripStation * GetGemStation(Int_t station_num)
BmnGemStripModule * GetModule(Int_t module_num)
virtual ~BmnGlobalAlignment()
virtual InitStatus Init()
virtual void Exec(Option_t *opt)
Int_t GetGemTrackIndex() const
Int_t GetSilTrackIndex() const
Short_t GetStation() const
void SetModule(Int_t mod)
vector< Double_t > GetGlobDers(TString lay)
void SetMeasures(Double_t measX, Double_t measY)
void SetLocDers(vector< Double_t > vecX, vector< Double_t > vecY)
vector< Double_t > GetLocDers(TString lay)
void SetGlobDers(vector< Double_t > vecX, vector< Double_t > vecY)
void SetDMeasures(Double_t dMeasX, Double_t dMeasY)
void SetStation(Int_t stat)
void end()
Write buffer (set of derivatives with same local parameters) to file.
void mille(Int_t NLC, const Double_t *derLc, Int_t NGL, const Double_t *derGl, const Int_t *label, Double_t rMeas, Double_t sigma)
Add measurement to buffer.
void SetModule(Int_t num)
void SetStation(Int_t num)
Double_t GetZPositionRegistered()
BmnSiliconStation * GetSiliconStation(Int_t station_num)
BmnSiliconModule * GetModule(Int_t module_num)
FairTrackParam * GetParamFirst()
Int_t GetHitIndex(Int_t iHit) const
static UniRun * GetRun(int period_number, int run_number)
get run from the database
static int ReadGeometryFile(int period_number, int run_number, char *geo_file_path, bool usePrevGeometryIfMissing=false)
double * GetFieldVoltage()
get field voltage of the current run