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