BOSS 7.0.8
BESIII Offline Software System
Loading...
Searching...
No Matches
EmcBhaCalib.cxx
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2// File and Version Information:
3//
4// Description:
5// Class EmcBhaCalib - performs calibration of EMC Xtals with Bhabha
6// events and a Chi^2 fit, the resulting system of linear equations
7// of the fit is solved with the SLAP Algebra package
8//
9// Environment:
10// Software developed for the BESIII Detector at the BEPCII.
11//
12// Author List:
13// Chunxiu Liu
14//
15// Copyright Information:
16// Copyright (C) 2005 IHEP
17//
18//------------------------------------------------------------------------
19
20//-----------------------
21// This Class's Header --
22//-----------------------
24
25//-------------
26// C Headers --
27//-------------
28extern "C" {
29#include "slap/dlap.h"
30}
31#include <iostream>
32#include <fstream>
33#include <cmath>
34#include <cassert>
35#include <cstdlib>
36//-------------------------------
37// Collaborating Class Headers --
38//-------------------------------
39
40#include "GaudiKernel/MsgStream.h"
41#include "GaudiKernel/AlgFactory.h"
42#include "GaudiKernel/ISvcLocator.h"
43#include "GaudiKernel/SmartDataPtr.h"
44#include "GaudiKernel/IDataProviderSvc.h"
45#include "GaudiKernel/PropertyMgr.h"
46#include "GaudiKernel/DataObject.h"
47
48#include "CLHEP/Vector/ThreeVector.h"
49using namespace std;
50
51using CLHEP::Hep3Vector;
52
53//--------------------
54#include "GaudiKernel/MsgStream.h"
55
56
57#include "TROOT.h"
58#include "TFile.h"
59#include "TTree.h"
60#include "TH1F.h"
61#include "TObjArray.h"
62
63
64
65//----------------
66// Constructors --
67//----------------
68EmcBhaCalib::EmcBhaCalib(const std::string& name, ISvcLocator* pSvcLocator)
69 :Algorithm(name, pSvcLocator),
70 m_dirHitsCut(200),
71 m_convCrit(0.000001),
72 m_askForMatrixInversion(true),
73 m_fitPolynom(true), //no used now
74 m_writeToFile(false),
75 m_readDataFromDB(false),
76 m_equationSolver("GMR"),
77 m_fileExt(""),
78 m_fileDir("/ihepbatch/besdata/public/liucx/Calib/"),
79 m_nrNonZeros(0),
80 m_nrXtalsEnoughHits(0),
81 m_runNumberFile("runnumbers.dat"),
82 m_MsgFlag(0)
83
84{
85
86 // Declare the properties
87 declareProperty("equationSolver", m_equationSolver);
88 declareProperty("dirHitsCut", m_dirHitsCut);
89 declareProperty("writeToFile", m_writeToFile);
90 declareProperty("fileExt", m_fileExt);
91 declareProperty("fileDir", m_fileDir);
92 declareProperty("readDataFromDB", m_readDataFromDB);
93 declareProperty("askForMatrixInversion", m_askForMatrixInversion);
94 declareProperty("fitPolynom", m_fitPolynom);
95 declareProperty("convCrit", m_convCrit);
96 declareProperty("runNumberFile", m_runNumberFile);
97 declareProperty("MsgFlag", m_MsgFlag);
98
99 m_calibConst = new double[6240];
100 m_calibConstUnred = new double[6240];
101 m_absoluteConstants = new double[6240];
102 m_oldConstants = new double[6240];
103
104
105 //ntuple
106 m_tuple1=0;
107
108
109
110}
111
112//--------------
113// Destructor --
114//--------------
116 if ( 0 != m_calibConst) {
117 delete [] m_calibConst;
118 m_calibConst = 0;
119 }
120 if ( 0 != m_calibConstUnred) {
121 delete [] m_calibConstUnred;
122 m_calibConstUnred = 0;
123 }
124 if ( 0 != m_absoluteConstants) {
125 delete [] m_absoluteConstants;
126 m_absoluteConstants = 0;
127 }
128 if ( 0 != m_oldConstants) {
129 delete [] m_oldConstants;
130 m_oldConstants = 0;
131 }
132 if ( 0 != m_myCalibData) {
133 delete m_myCalibData;
134 m_myCalibData = 0;
135 }
136
137}
138
139// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
141
142 MsgStream log(msgSvc(), name());
143 log << MSG::INFO << "in initialize()" << endreq;
144
145 m_myCalibData = new EmcBhaCalibData(6240, m_MsgFlag);
146
147 m_calibrationSuccessful = false;
148
149 StatusCode status1;
150
151 NTuplePtr nt1(ntupleSvc(),"FILE302/n1");
152 if ( nt1 ) m_tuple1 = nt1;
153 else {
154 m_tuple1=ntupleSvc()->book("FILE302/n1",CLID_ColumnWiseTuple,"Calib");
155 if( m_tuple1 ) {
156
157 status1 = m_tuple1->addItem ("ixtal",ixtal);
158 status1 = m_tuple1->addItem ("gi0",gi0);
159 status1 = m_tuple1->addItem ("calibConst",calibConst);
160 status1 = m_tuple1->addItem ("err",err);
161 status1 = m_tuple1->addItem ("nhitxtal",nhitxtal);
162
163 }
164 else { // did not manage to book the N tuple....
165 log << MSG::ERROR <<"Cannot book N-tuple:" << long(m_tuple1) << endmsg;
166 return StatusCode::FAILURE;
167 }
168 }
169
170 // use EmcCalibConstSvc
171 StatusCode scCalib;
172 scCalib = Gaudi::svcLocator() -> service("EmcCalibConstSvc", m_emcCalibConstSvc);
173 if( scCalib != StatusCode::SUCCESS){
174 log << MSG::ERROR << "can not use EmcCalibConstSvc" << endreq;
175 }
176 else {
177 std::cout << "Test EmcCalibConstSvc DigiCalibConst(0)= "
178 << m_emcCalibConstSvc -> getDigiCalibConst(0) << std::endl;
179 }
180
181 //init starting values for calibration constants from file or set to 1
182 initCalibConst();
183
184 //digiConstCor();
185
186 return StatusCode::SUCCESS;
187}
188
189// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
191
192 MsgStream log(msgSvc(), name());
193 log << MSG::DEBUG << "in execute()" << endreq;
194 //read in accumulated matrix/matrices
195 if ( m_readDataFromDB ) {
196 m_calibrationSuccessful = readInFromDB();
197
198 log << MSG::INFO << "read in accumulated matrix from DB"<<endreq;
199
200 } else {
201 m_calibrationSuccessful = readInFromFile();
202
203 log << MSG::INFO <<"read in accumulated matrix from file"<<endreq;
204
205 }
206
207 if ( m_calibrationSuccessful == true ) {
208
209 //ask first if to do a matrix inversion
210 if (m_askForMatrixInversion==true) {
211
212 m_calibrationSuccessful = false;
213 log << MSG::INFO << " Well, we have "
214 << m_nrXtalsEnoughHits << " crystals whith more "
215 << "than " << m_dirHitsCut
216 << " direct hits. Do you want do "
217 << "a matrix inversion ? [N]" << endreq;
218
219
220 m_calibrationSuccessful = true;
221
222 }
223
224 if ( m_calibrationSuccessful == true ) {
225
226 //write added matrix and vector to files
227 if ( m_writeToFile == true) {
228 writeOut();
229 }
230 //cout <<"writeout"<<endl;
231 m_myCalibData->printVec(2);
232
233 //reduce to continious array of only non zeros elements for SLAP
234 if ( m_calibrationSuccessful = m_myCalibData->reduce() ) {
235
236 cout<<"m_calibrationSuccessful="<<m_calibrationSuccessful<<endl;
237
238 if ( m_myCalibData->nXtalsHit() != m_myCalibData->nXtals() ){
239 log << MSG::INFO << " Reduced number of Xtals for calibration: "
240 << m_myCalibData->nXtalsHit()
241 << endreq;
242 }
243 cout<<"m_myCalibData->nXtalsHit()="<<m_myCalibData->nXtalsHit()
244 <<"m_myCalibData->nXtals()="<<m_myCalibData->nXtals()<<endl;
245 m_myCalibData->printVec(10);
246
247 //solve matrix equation
248 if ( m_calibrationSuccessful = solveEqua() ) {
249
250 for (int i=0; i<m_myCalibData->nXtalsHit(); i++){
251 //fill an array of constants for storage in database
252 m_calibConstUnred[m_myCalibData->xtalInd(i)]
253 = m_calibConst[i];
254
255 // if (m_myCalibData->xtalHitsDir(i)>10)
256 // cout<<"Index,calibconst="<<" "<<i <<" "<<m_myCalibData->xtalInd(i)
257 // <<" "<<m_calibConstUnred[m_myCalibData->xtalInd(i)]
258 // <<" "<<m_calibConstUnred[m_myCalibData->xtalInd(i)]*
259 // m_oldConstants[m_myCalibData->xtalInd(i)]<<endl;
260
261 }
262 //do validation, fit polynom if necessary, fill CalList
263 prepareConstants();
264
265 //if wanted write constants to plain ASCII file
266 if ( m_writeToFile==true){
267 writeOutConst();
268 }
269
270 ntupleOut();
271
272 }
273
274 } else {
275 log << MSG::WARNING << " Reduction of the Emc Bhabha calibration matrix FAILED !"
276 << endl
277 << " Will NOT perform Emc Bhabha calibration !"
278 << endreq;
279 return( StatusCode::FAILURE);
280 }
281 }
282 } else {
283 log << MSG::WARNING << " ERROR in reading in Emc Bhabha calibration data !"
284 << endl
285 << " Will NOT perform Emc Bhabha calibration !"
286 << endreq;
287 return( StatusCode::FAILURE);
288 }
289
290 return StatusCode::SUCCESS;
291}
292
293// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
295
296 MsgStream log(msgSvc(), name());
297
298
299 log << MSG::INFO << "in endRun()" << endreq;
300
301
302 return StatusCode::SUCCESS;
303}
304
305
306void
308
309 MsgStream log(msgSvc(), name());
310
311 log << MSG::INFO<< "Performs the Chi square fit of the accumulated "
312 << endreq;
313}
314
315//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
316void
317EmcBhaCalib::initCalibConst( ) {
318
319
320 MsgStream log(msgSvc(), name());
321
322 for ( int i = 0; i< m_myCalibData->nXtals(); i++ ) {
323 m_calibConst[i] = 1.;
324 }
325
326 ifstream inConst("EmcCalib.Const");
327 if ( !inConst ) {
328 log << MSG::VERBOSE
329 << " No starting values for calibration constants found ! "
330 << "(EmcCalib.Const)"
331 << endl
332 << "Set them to 1.0 ! " << endreq;
333 } else {
334 log << MSG::VERBOSE << " Read in starting values of calibration constants !"
335 << endreq;
336
337 int nConst;
338 inConst >> nConst;
339 log << MSG::VERBOSE << " Have starting values for "
340 << nConst << " Xtals !" << endl
341 << "Set the others to 1.0 ! " << endmsg;
342
343 int numberXtal;
344 for ( int i = 0; i< nConst; i++ ) {
345 inConst >> numberXtal >> m_calibConst[numberXtal];
346 }
347 }
348
349 int nConstEmc;
350
351 nConstEmc= m_emcCalibConstSvc -> getDigiCalibConstNo() ;
352
353 if ( nConstEmc!=6240) cout<<"number of calibconst="<< nConstEmc<<endl;
354
355 // log << MSG::VERBOSE << " Have starting values for "
356 // << nConstEmc << " Xtals !" << endl
357 // << "Set the others to 1.0 ! " << endmsg;
358
359 for ( int i = 0; i< nConstEmc; i++ ) {
360 //cout<<i<<" "
361 //<<m_emcCalibConstSvc->getThetaIndex(i)<<" "
362 //<<m_emcCalibConstSvc->getPhiIndex(i)
363 //<<" "<<m_emcCalibConstSvc->getEmcID(i)<<endl;
364 m_calibConst[i] = m_emcCalibConstSvc -> getDigiCalibConst(i);
365 //m_calibConst[i] =5.0;
366 m_oldConstants[i]=m_emcCalibConstSvc -> getDigiCalibConst(i);
367
368 // initialize m_absoluteConstants as m_oldConstants.
369 //m_absoluteConstants[i] =m_emcCalibConstSvc -> getDigiCalibConst(i);
370 m_absoluteConstants[i] =1.0;
371 // initialize m_calibConstUnred as 1.
372 m_calibConstUnred[i] =1.0;
373 }
374
375
376}
377
378
379//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
380bool
381EmcBhaCalib::solveEqua() {
382
383 MsgStream log(msgSvc(), name());
384 //-----------------------------------------------------
385 // solve system of equations with SLAP package
386 //-----------------------------------------------------
387 log << MSG::VERBOSE << " Solve Matrix equation with method "
388 << m_equationSolver
389 << endl
390 << "Xtals hit: " << m_myCalibData->nXtalsHit()
391 << endl
392 << "Nr of non Zeros: " << m_nrNonZeros << endreq;
393
394 //input parameters for SLAP
395 long int isym = 1; //matrix M is symmetric
396 long int nsave = 20;
397 long int itol = 0; //type of convergence criterion
398 double convCriterion = m_convCrit; //convergence crtiterion
399 long int maxIter = 1000; //maximum number of iterations
400 long int errUnit = 6; //unit on which to write error estimation
401 // at each iteration
402
403 //working arrays needed by SLAP
404 long int lengthDoubleWork;
405 double* doubleWork;
406 long int lengthIntWork;
407 long int* intWork;
408
409 //output parameters of slap
410 long int iters=0; //number of iterations required
411 double errorv=1000; //error estimate
412 long int errorFlag=9999;
413
414 //sparse Ax=b solver.
415 //uses the generalized minimum residual method
416 //The preconditioner is diagonal scaling.
417 if ( m_equationSolver == "GMR" ) {
418
419 cout<<m_equationSolver<<endl;
420
421 cout<<"errorFlag="<<errorFlag<<endl;
422
423 //workspaces
424 lengthDoubleWork = (1 + m_myCalibData->nXtals()*(nsave+7)
425 + nsave*(nsave+3))
426 + 50000;
427 doubleWork = new double[lengthDoubleWork];
428 lengthIntWork = 50;
429 intWork = new long int [lengthIntWork];
430
431 dsdgmr_ ( &(m_myCalibData->nXtalsHit()),
432 m_myCalibData->getVectorR(),
433 m_calibConst,
434 &m_nrNonZeros,
435 m_myCalibData->getMatrixM()->row(),
436 m_myCalibData->getMatrixM()->column(),
437 m_myCalibData->getMatrixM()->matrix(),
438 &isym,
439 &nsave,
440 &itol,
441 &convCriterion,
442 &maxIter,
443 &iters,
444 &errorv,
445 &errorFlag,
446 &errUnit,
447 doubleWork,
448 &lengthDoubleWork,
449 intWork,
450 &lengthIntWork
451 );
452 cout<<"errorFlag="<<errorFlag<<endl;
453
454
455 log << MSG::VERBOSE << " Error flag: " << errorFlag << endreq;
456 if ( errorFlag < 0 ) errorFlag = labs(errorFlag) + 2;
457 switch ( errorFlag ) {
458 case 0: log << MSG::VERBOSE << " all went well"
459 << endreq; break;
460 case 1: log << MSG::ERROR << " insufficient storage allocated for _doubleWork or _intWork"
461 << endreq; break;
462 case 2: log << MSG::ERROR << " method failed to reduce norm of current residual"
463 << endreq; break;
464 case 3: log << MSG::ERROR << " insufficient length for _doubleWork"
465 << endreq; break;
466 case 4: log << MSG::ERROR << " inconsistent _itol and values"
467 << endreq; break;
468 default: log << MSG::ERROR << " unknown flag" << endreq;
469 }
470 log << MSG::VERBOSE << " Integer workspace used = " << intWork[8] << endl
471 << " Double workspace used = " << intWork[9] << endreq;
472 }
473
474 //Routine to solve a symmetric positive definite linear
475 //system Ax = b using the Preconditioned Conjugate
476 //Gradient method. The preconditioner is diagonal scaling.
477 if ( m_equationSolver == "PCG" ) {
478
479 cout<<m_equationSolver<<endl;
480
481 itol = 1;
482
483 //workspaces
484 lengthDoubleWork = 5 *m_myCalibData->nXtals() + 50000;
485 doubleWork = new double[lengthDoubleWork];
486 lengthIntWork = 50;
487 intWork = new long int [lengthIntWork];
488
489 dsdcg_( &(m_myCalibData->nXtalsHit()),
490 m_myCalibData->getVectorR(),
491 m_calibConst,
492 &m_nrNonZeros,
493 m_myCalibData->getMatrixM()->row(),
494 m_myCalibData->getMatrixM()->column(),
495 m_myCalibData->getMatrixM()->matrix(),
496 &isym,
497 &itol,
498 &convCriterion,
499 &maxIter,
500 &iters,
501 &errorv,
502 &errorFlag,
503 &errUnit,
504 doubleWork,
505 &lengthDoubleWork,
506 intWork,
507 &lengthIntWork
508 );
509
510 switch ( errorFlag ) {
511 case 0: log << MSG::VERBOSE << "all went well" << endreq; break;
512 case 1: log << MSG::ERROR << " insufficient storage allocated for WORK or IWORK"
513 << endreq; break;
514 case 2: log << MSG::ERROR << " method failed to to converge in maxIter steps."
515 << endreq; break;
516 case 3:log << MSG::ERROR << " Error in user input. Check input value of _nXtal,_itol."
517 << endreq; break;
518 case 4:log << MSG::ERROR << " User error tolerance set too tight. "
519 << "Reset to 500.0*D1MACH(3). Iteration proceeded."
520 << endreq; break;
521 case 5:log << MSG::ERROR << " Preconditioning matrix, M, is not Positive Definite. "
522 << endreq; break;
523 case 6: log << MSG::ERROR << " Matrix A is not Positive Definite."
524 << endreq; break;
525 default: log << MSG::ERROR << " unknown flag" << endreq;
526 }
527 log << MSG::VERBOSE << " Integer workspace used = " << intWork[9] << endl
528 << "Double workspace used = " << intWork[10] << endreq;
529 }
530
531 log << MSG::VERBOSE << "------ Calibration fit statistics ------- " << endl
532 << "maximum number of iterations =" << maxIter << endl
533 << " number of iterations =" << iters << endl
534 << "error estimate of error in final solution ="
535 << errorv << endreq;
536
537 if ( 0 != doubleWork) delete [] doubleWork;
538 if ( 0 != intWork) delete [] intWork;
539
540 if ( errorFlag != 0 ) return false;
541 else return true;
542
543 return true;
544}
545
546//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
547void
548EmcBhaCalib::writeOut() {
549
550 ofstream vectorOut;
551 std::string vectorFile = m_fileDir;
552 vectorFile += m_fileExt;
553 vectorFile += "allCalibVector.dat";
554 vectorOut.open(vectorFile.c_str());
555
556 ofstream matrixOut;
557 std::string matrixFile = m_fileDir;
558 matrixFile += m_fileExt;
559 matrixFile += "allCalibMatrix.dat";
560 matrixOut.open(matrixFile.c_str());
561
562 MsgStream log(msgSvc(), name());
563
564 log << MSG::VERBOSE << " Write out files "
565 << vectorFile << " "
566 << matrixFile
567 << endreq;
568
569 m_myCalibData->writeOut(matrixOut,vectorOut);
570
571 vectorOut.close();
572 matrixOut.close();
573
574}
575
576//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
577void
578EmcBhaCalib::writeOutConst() {
579
580 ofstream constOut;
581
582 std::string constFile = m_fileDir;
583 constFile += m_fileExt;
584 constFile += "CalibConst.dat";
585
586 constOut.open(constFile.c_str());
587
588 constOut << "#crystalIndex relative-constant absolute-constant" << endl;
589
590 int chanIndex;
591
592 //output constants to file
593 for ( int i=0; i < m_myCalibData->nXtalsHit(); i++) {
594
595 chanIndex=m_myCalibData->xtalInd(i); //xtalInd(i) array of xtal indices
596
597 //---- get the new absolute constant ----
598 //set it to 0 if we have not enough hits -> we'll keep the old constant
599 if ( m_myCalibData->xtalHitsDir(i) < m_dirHitsCut )
600 m_absoluteConstants[chanIndex] = m_oldConstants[chanIndex];
601 else
602 m_absoluteConstants[chanIndex] =
603 m_oldConstants[chanIndex] * m_calibConstUnred[chanIndex];
604
605 }
606
607 //output constants to file
608 for ( int i=0; i < m_myCalibData->nXtals(); i++) {
609
610 long Xtal_Index = i;
611 if(m_calibConstUnred[Xtal_Index]>0){
612 constOut << Xtal_Index << " "
613 << m_calibConstUnred[Xtal_Index] << " "
614 << m_oldConstants[Xtal_Index] << " "
615 << m_absoluteConstants[Xtal_Index]
616 << endl;
617
618
619 }
620 }
621 constOut.close();
622
623}
624
625//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
626bool
627EmcBhaCalib::readInFromFile() {
628
629 MsgStream log(msgSvc(), name());
630
631 log << MSG::INFO << " Read Bhabha calibration data from files !"
632 << endreq;
633
634 std::string runNumberFile = m_fileDir;
635 runNumberFile += m_fileExt;
636 runNumberFile += m_runNumberFile;
637
638 bool success = false;
639 log << MSG::INFO << " Get file names from input file : "
640 << runNumberFile
641 << endreq;
642
643 std::string vectorFile;
644 std::string matrixFile;
645
646 log << MSG::INFO << "Runnumber non-zeros xtals"
647 << endreq;
648
649 ifstream datafile(runNumberFile.c_str());
650
651 //cout<<"datafile="<<runNumberFile.c_str()<<""<<datafile.good()<<endl;
652
653 if (datafile.good() > 0 ) {
654
655 while( !datafile.eof() ) {
656
657 ifstream vectorIn;
658 ifstream matrixIn;
659
660 std::string num;
661 datafile >> num;
662
663 if ( num.length() > 0 ) {
664
665 vectorFile = m_fileDir;
666 vectorFile += m_fileExt;
667 vectorFile += num;
668 vectorFile += "CalibVector.dat";
669
670 matrixFile = m_fileDir;
671 matrixFile += m_fileExt;
672 matrixFile += num;
673 matrixFile += "CalibMatrix.dat";
674
675 vectorIn.open(vectorFile.c_str());
676 matrixIn.open(matrixFile.c_str());
677
678 if ( vectorIn.good() > 0 && matrixIn.good() > 0 ) {
679
680 m_myCalibData->readIn(matrixIn,vectorIn);
681
682 log << MSG::INFO << num
683 << " "
684 << m_myCalibData->getMatrixM()->num_nonZeros()
685 << " "
686 << m_myCalibData->nXtalsHit()
687 << endreq;
688
689 success = true;
690
691 }
692 else {
693 if ( !vectorIn )
694 log << MSG::ERROR << " ERROR: Vector input file "
695 << vectorFile
696 << " doesn't exist !" << endreq;
697 if ( !matrixIn )
698 log << MSG::ERROR << " ERROR: Matrix input file "
699 << matrixFile
700 << " doesn't exist !" << endreq;
701 }
702 vectorIn.close();
703 matrixIn.close();
704 }
705 }
706 }
707
708 if ( success == true) {
709
710 for (int i=0; i < m_myCalibData->nXtals(); i++) {
711
712 if ( m_myCalibData->xtalHitsDir(i) > m_dirHitsCut )
713 {
714 m_nrXtalsEnoughHits++;
715 }
716 }
717 m_nrNonZeros = m_myCalibData->getMatrixM()->num_nonZeros();
718 log << MSG::INFO<< " Final matrix : "
719 << "Number of non zero elements: " << m_nrNonZeros
720 << " "
721 << m_myCalibData->nXtalsHit()
722 << endreq;
723
724 }
725
726
727 return success;
728}
729
730//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
731bool
732EmcBhaCalib::readInFromDB() {
733
734 bool success = true;
735
736 return success;
737}
738
739//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
740bool
741EmcBhaCalib::prepareConstants() {
742
743 bool successfull=false;
744
745 //the old constant
746 //float theCalConst = 1.0;
747 int chanIndex;
748
749 for ( int i = 0; i< m_myCalibData->nXtalsHit(); i++ ) {
750
751 chanIndex=m_myCalibData->xtalInd(i); //xtalInd(i) array of xtal indices
752
753 //////////deal with the crystal of ixtal=689, because of ADC is very small;
754 // m_calibConstUnred[chanIndex]=1;
755 //if (chanIndex==689) m_calibConstUnred[chanIndex]=1.47;
756 //if (chanIndex==689)
757 // m_oldConstants[chanIndex]=m_oldConstants[chanIndex]*1.47;
758 //---- get the new absolute constant ----
759 //set it to 0 if we have not enough hits -> we'll keep the old constant
760 if ( m_myCalibData->xtalHitsDir(i) < m_dirHitsCut )
761 m_absoluteConstants[chanIndex] = m_oldConstants[chanIndex];
762 else
763 m_absoluteConstants[chanIndex] =
764 m_oldConstants[chanIndex] * m_calibConstUnred[chanIndex];
765 }
766
767
768 double DigiConst[6240];
769 int IxtalNumber[6240];
770
771 for(int ind=0; ind<m_myCalibData->nXtals(); ind++ ) {
772
773 DigiConst[ind]=m_absoluteConstants[ind];
774 // cout<<"ind="<<ind<<"\t"<< DigiConst[ind]<<endl;
775 }
776
777 int ixtal1,ixtal2,ixtal3;
778 ixtal1=m_emcCalibConstSvc->getIndex(1,20,26);
779 ixtal2=m_emcCalibConstSvc->getIndex(1,23,26);
780 ixtal3=m_emcCalibConstSvc->getIndex(1,24,26);
781 //cout<<ixtal1<<" "<<ixtal2<<" "<<ixtal3<<" "<<endl;
782 for(int ind=0; ind<m_myCalibData->nXtals(); ind++ ) {
783
784 IxtalNumber[ind]=-1;
785 /*
786 if (ind==ixtal1) IxtalNumber[ind]=ixtal3;
787 if (ind==ixtal2) IxtalNumber[ind]=ixtal1;
788 if (ind==ixtal3) IxtalNumber[ind]=ixtal2;
789 */
790 }
791
792 TFile fconst("EmcCalibConst.root", "recreate");
793
794 //define tree fill the absolute digicalibconsts into the root-file
795 TTree* constr= new TTree("DigiCalibConst","DigiCalibConst");
796 constr->Branch("DigiCalibConst",DigiConst,"DigiConst[6240]/D");
797 constr->Branch("IxtalNumber",IxtalNumber,"IxtalNumber[6240]/I");
798
799 constr->Fill();
800 /*
801
802 double aa[6240];
803 int Iaa[6240];
804 constr->SetBranchAddress("DigiCalibConst", aa);
805 constr->SetBranchAddress("IxtalNumber", Iaa);
806 constr->GetEntry(0);
807 cout<<Iaa[10]<<endl;
808 cout<<aa[10]<<endl;
809
810 cout<<constr->GetEntry(0)<<endl;
811
812 */
813
814 constr->Write();
815
816 delete constr;
817
818 fconst.Close();
819
820 // end tree
821
822 successfull=true;
823 return successfull;
824
825}
826
827void
828EmcBhaCalib::ntupleOut(){
829
830
831 for (int i=0; i < m_myCalibData->nXtalsHit(); i++) {
832 int xtalIndex=m_myCalibData->xtalInd(i);
833
834 gi0 = m_oldConstants[xtalIndex];
835
836 if (gi0<0) cout<<"gi0="<<gi0<<endl;
837 err = (m_calibConstUnred[xtalIndex]-gi0)/gi0;
838 calibConst=m_calibConstUnred[xtalIndex];
839 ixtal=xtalIndex;
840 nhitxtal=m_myCalibData->xtalHitsDir(i); ///
841 m_tuple1->write();
842 }
843
844}
845
846
847void
848EmcBhaCalib::printInfo(std::string fileName) {
849
850 ofstream xtalHitsDirOut;
851 std::string xtalHitsDirFile = m_fileDir;
852 xtalHitsDirFile += m_fileExt;
853 xtalHitsDirFile += fileName;
854 xtalHitsDirOut.open(xtalHitsDirFile.c_str());
855
856 //nXtalsHit() is number of xtals hit
857 for (int i=0; i < m_myCalibData->nXtalsHit(); i++) {
858 if ( m_myCalibData->xtalHitsDir(i) > m_dirHitsCut )
859 {
860 int chanindex=m_myCalibData->xtalInd(i);
861 xtalHitsDirOut<<chanindex<<endl;
862 }
863 }
864 xtalHitsDirOut.close();
865
866}
867
868void
869EmcBhaCalib::digiConstCor(){
870
871 // ofstream Fuout;
872 //std::string Fuoutfile;
873
874 //Fuoutfile="emccalibconst.txt";
875 //Fuout.open(Fuoutfile.c_str());
876
877 double digiConst[6240];
878
879 for(int ind=0; ind<m_myCalibData->nXtals(); ind++ ) {
880
881 digiConst[ind]=m_oldConstants[ind];
882 // Fuout<<m_oldConstants[ind]<<endl;
883 }
884
885 // Fuout.close();
886
887 ifstream ExpEneIn;
888 std::string ExpEneFile;
889 ExpEneFile = "cor.conf";
890 ExpEneIn.open(ExpEneFile.c_str());
891
892 double ExpEne[56];
893 double Exp_barrel[44],Exp_east[6],Exp_west[6];
894
895 for(int i=0;i<56;i++) {
896
897 ExpEneIn>>ExpEne[i];
898 ExpEne[i]=ExpEne[i]*1.843;
899 if (i<6) Exp_east[i]=ExpEne[i];
900 if (i>=6&&i<50) Exp_barrel[i-6]=ExpEne[i];
901 if (i>=50) Exp_west[55-i]=ExpEne[i];
902
903 //cout<<ExpEne[i]<<endl;
904 }
905 ExpEneIn.close();
906
907 ifstream EDepEneIn;
908 std::string EDepEneFile = "allxtal.dat";
909 EDepEneIn.open(EDepEneFile.c_str());
910
911 double CorDigiConst[6240];
912 int ipart,ithe,iphi;
913 double peak,sigma;
914 double coeff;
915
916 for(int i=0;i<6240;i++) {
917
918 EDepEneIn>>ipart>>ithe>>iphi>>peak>>sigma;
919 int ix=m_emcCalibConstSvc->getIndex(ipart,ithe,iphi);
920
921 if (ipart==0) coeff=peak/Exp_east[ithe];
922 if (ipart==1) coeff=peak/Exp_barrel[ithe];
923 if (ipart==2) coeff=peak/Exp_west[ithe];
924 cout<<coeff<<endl;
925 //CorDigiConst[ix]=digiConst[ix]/coeff;
926 CorDigiConst[ix]=coeff;
927 }
928 EDepEneIn.close();
929
930
931 //define tree fill the absolute digicalibconsts into the root-file
932
933 TTree* constr= new TTree("DigiCalibConst","DigiCalibConst");
934 constr->Branch("DigiCalibConst",CorDigiConst,"CorDigiConst[6240]/D");
935 constr->Fill();
936
937 double aa[6240];
938 constr->SetBranchAddress("DigiCalibConst", aa);
939 constr->GetEntry(0);
940
941 TFile fconst("EmcCalibConstCor.root", "recreate");
942 constr->Write();
943
944 fconst.Close();
945 delete constr;
946
947
948}
TTree * sigma
int dsdcg_(const int *n, const double *b, double *x, const long *nelt, int *ia, int *ja, double *a, const long *isym, const long *itol, const double *tol, const long *itmax, long *iter, double *err, long *ierr, const long *iunit, double *rwork, const long *lenw, long *iwork, const long *leniw)
int dsdgmr_(const int *n, const double *b, double *x, const long *nelt, int *ia, int *ja, double *a, const long *isym, long *nsave, const long *itol, const double *tol, const long *itmax, long *iter, double *err, long *ierr, const long *iunit, double *rwork, const long *lenw, long *iwork, const long *leniw)
INTupleSvc * ntupleSvc()
IMessageSvc * msgSvc()
int & xtalHitsDir(int ind)
double * getVectorR()
void printVec(int number)
int xtalInd(int ind)
EmcLSSMatrix * getMatrixM()
void writeOut(ostream &OutM, ostream &outV)
void readIn(istream &InM, istream &InV)
StatusCode execute()
EmcBhaCalib(const std::string &name, ISvcLocator *pSvcLocator)
Definition: EmcBhaCalib.cxx:68
virtual void help()
StatusCode finalize()
virtual ~EmcBhaCalib()
StatusCode initialize()
int * row() const
Definition: EmcLSSMatrix.h:84
long int num_nonZeros()
int * column(const int &rowind=0) const
double * matrix(const int &rowind=0) const
virtual int getIndex(unsigned int PartId, unsigned int ThetaIndex, unsigned int PhiIndex) const =0
#define ix(i)
Definition: minoreval.cpp:387
std::ifstream ifstream
Definition: bpkt_streams.h:44
std::ofstream ofstream
Definition: bpkt_streams.h:42