CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Alignment/CommonAlignmentAlgorithm/plugins/ApeSettingAlgorithm.cc

Go to the documentation of this file.
00001 
00009 /*
00010  *# Parameters:
00011  *#    saveApeToASCII -- Do we write out an APE text file?
00012  *#    saveComposites -- Do we write APEs for composite detectors?
00013  *#    saveLocalNotGlobal -- Do we write the APEs in the local or global coordinates?
00014  *#    apeASCIISaveFile -- The name of the save-file.
00015  *#    readApeFromASCII -- Do we read in APEs from a text file?
00016  *#    readLocalNotGlobal -- Do we read APEs in the local or the global frame?
00017  *#    readFullLocalMatrix -- Do we read the full local matrix or just the diagonal elements?
00018  *#                        -- Always write full matrix
00019  *# Full matrix format: DetID dxx dxy dyy dxz dyz dzz
00020  *# Diagonal element format: DetID sqrt(dxx) sqrt(dyy) sqrt(dzz)
00021  *#    setComposites -- Do we set the APEs for composite detectors or just ignore them?
00022  *#    apeASCIIReadFile -- Input file name.
00023  *# Also note:
00024  *#    process.AlignmentProducer.saveApeToDB -- to save as an sqlite file
00025  *# and associated entries in _cfg.py
00026  */
00027 
00028 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
00029 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmBase.h"
00030 #include "Alignment/CommonAlignment/interface/AlignableModifier.h"
00031 
00032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00033 
00034 #include "CondFormats/Alignment/interface/AlignmentErrors.h" 
00035 #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h"
00036 #include "CLHEP/Matrix/SymMatrix.h"
00037 
00038 #include <fstream>
00039 #include <string>
00040 #include <set>
00041 
00042 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00043 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
00044 
00045 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
00046 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
00047 #include "Alignment/CommonAlignment/interface/Alignable.h"
00048 
00049 // includes to make known that they inherit from Alignable:
00050 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00051 #include "Alignment/MuonAlignment/interface/AlignableMuon.h"
00052 #include "Alignment/CommonAlignment/interface/AlignableExtras.h"
00053 
00054 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
00055 
00056 class ApeSettingAlgorithm : public AlignmentAlgorithmBase
00057 {
00058  public:
00060   ApeSettingAlgorithm(const edm::ParameterSet &cfg);
00061 
00063   virtual ~ApeSettingAlgorithm();
00064 
00066   virtual void initialize(const edm::EventSetup &setup, 
00067                           AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras,
00068                           AlignmentParameterStore *store);
00069 
00071   virtual void terminate();
00072 
00074   virtual void run(const edm::EventSetup &setup, const EventInfo &eventInfo);
00075 
00076  private:
00077   edm::ParameterSet         theConfig;
00078   AlignableNavigator       *theAlignableNavigator;
00079   AlignableTracker         *theTracker;
00080   bool                     saveApeToAscii_,readApeFromAscii_,readFullLocalMatrix_;
00081   bool                     readLocalNotGlobal_,saveLocalNotGlobal_;
00082   bool                     setComposites_,saveComposites_;
00083 };
00084 
00085 //____________________________________________________
00086 //____________________________________________________
00087 //____________________________________________________
00088 //____________________________________________________
00089 
00090 
00091 // Constructor ----------------------------------------------------------------
00092 //____________________________________________________
00093 ApeSettingAlgorithm::ApeSettingAlgorithm(const edm::ParameterSet &cfg) :
00094   AlignmentAlgorithmBase(cfg), theConfig(cfg),
00095   theAlignableNavigator(0)
00096 {
00097   edm::LogInfo("Alignment") << "@SUB=ApeSettingAlgorithm" << "Start.";
00098   saveApeToAscii_ = theConfig.getUntrackedParameter<bool>("saveApeToASCII");
00099   saveComposites_ = theConfig.getUntrackedParameter<bool>("saveComposites");
00100   saveLocalNotGlobal_ = theConfig.getUntrackedParameter<bool>("saveLocalNotGlobal");
00101   readApeFromAscii_ = theConfig.getParameter<bool>("readApeFromASCII");
00102   readLocalNotGlobal_ = theConfig.getParameter<bool>("readLocalNotGlobal");
00103   readFullLocalMatrix_ = theConfig.getParameter<bool>("readFullLocalMatrix");
00104   setComposites_ = theConfig.getParameter<bool>("setComposites");
00105   
00106 }
00107 
00108 // Destructor ----------------------------------------------------------------
00109 //____________________________________________________
00110 ApeSettingAlgorithm::~ApeSettingAlgorithm()
00111 {
00112   delete theAlignableNavigator;
00113 }
00114 
00115 // Call at beginning of job ---------------------------------------------------
00116 //____________________________________________________
00117 void ApeSettingAlgorithm::initialize(const edm::EventSetup &setup, 
00118                                      AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras,
00119                                      AlignmentParameterStore *store)
00120 {
00121  theAlignableNavigator = new AlignableNavigator(tracker, muon);
00122  theTracker = tracker;
00123  
00124  if (readApeFromAscii_)
00125    { std::ifstream apeReadFile(theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath().c_str()); //requires <fstream>
00126    if (!apeReadFile.good())
00127      { edm::LogInfo("Alignment") << "@SUB=initialize" <<"Problem opening APE file: skipping"
00128                                  << theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath();
00129      return;
00130      }
00131    std::set<int> apeList; //To avoid duplicates
00132    while (!apeReadFile.eof())
00133      { int apeId=0; double x11,x21,x22,x31,x32,x33;
00134      if (!readLocalNotGlobal_ || readFullLocalMatrix_) 
00135        { apeReadFile>>apeId>>x11>>x21>>x22>>x31>>x32>>x33>>std::ws;}
00136      else
00137        { apeReadFile>>apeId>>x11>>x22>>x33>>std::ws;}
00138      //idr What sanity checks do we need to put here?
00139      if (apeId != 0) //read appears valid?
00140        { if (apeList.find(apeId) == apeList.end()) //Not previously done
00141          {  DetId id(apeId);
00142          AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(id)); //NULL if none
00143          if (alidet)
00144            { if ((alidet->components().size()<1) || setComposites_) //the problem with glued dets...
00145              { GlobalError globErr;
00146              if (readLocalNotGlobal_)
00147                { AlgebraicSymMatrix as(3,0); 
00148                if (readFullLocalMatrix_)
00149                  { as[0][0]=x11; as[1][0]=x21; as[1][1]=x22;
00150                  as[2][0]=x31; as[2][1]=x32; as[2][2]=x33;
00151                  }
00152                else
00153                  { as[0][0]=x11*x11; as[1][1]=x22*x22; as[2][2]=x33*x33;} //local cov.
00154                align::RotationType rt=alidet->globalRotation();
00155                AlgebraicMatrix am(3,3);
00156                am[0][0]=rt.xx(); am[0][1]=rt.xy(); am[0][2]=rt.xz();
00157                am[1][0]=rt.yx(); am[1][1]=rt.yy(); am[1][2]=rt.yz();
00158                am[2][0]=rt.zx(); am[2][1]=rt.zy(); am[2][2]=rt.zz();
00159                as=as.similarityT(am); //symmetric matrix
00160                globErr = GlobalError( as );
00161                }
00162              else
00163                {
00164                  globErr = GlobalError(x11,x21,x22,x31,x32,x33);
00165                }
00166              alidet->setAlignmentPositionError(globErr, false); // do not propagate down!
00167              apeList.insert(apeId); //Flag it's been set
00168              }
00169            else
00170              { edm::LogInfo("Alignment") << "@SUB=initialize" << "Not Setting APE for Composite DetId "<<apeId;
00171              }
00172            }
00173          }
00174        else
00175          { edm::LogInfo("Alignment") << "@SUB=initialize" << "Skipping duplicate APE for DetId "<<apeId;
00176          }
00177        }
00178      }
00179    apeReadFile.close();
00180    edm::LogInfo("Alignment") << "@SUB=initialize" << "Set "<<apeList.size()<<" APE values.";
00181    }
00182 }
00183  
00184 
00185 // Call at end of job ---------------------------------------------------------
00186 //____________________________________________________
00187 void ApeSettingAlgorithm::terminate()
00188 {
00189   if (saveApeToAscii_)
00190     { AlignmentErrors* aliErr=theTracker->alignmentErrors();
00191     int theSize=aliErr->m_alignError.size();
00192     std::ofstream apeSaveFile(theConfig.getUntrackedParameter<std::string>("apeASCIISaveFile").c_str()); //requires <fstream>
00193     for (int i=0; i < theSize; ++i)
00194       { int id= aliErr->m_alignError[i].rawId();
00195       AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(DetId(id))); //NULL if none
00196       if (alidet && ((alidet->components().size()<1) || saveComposites_))
00197         { apeSaveFile<<id;
00198         CLHEP::HepSymMatrix sm = aliErr->m_alignError[i].matrix();
00199         if (saveLocalNotGlobal_)
00200           { align::RotationType rt=alidet->globalRotation();
00201           AlgebraicMatrix am(3,3);
00202           am[0][0]=rt.xx(); am[0][1]=rt.xy(); am[0][2]=rt.xz();
00203           am[1][0]=rt.yx(); am[1][1]=rt.yy(); am[1][2]=rt.yz();
00204           am[2][0]=rt.zx(); am[2][1]=rt.zy(); am[2][2]=rt.zz();
00205           sm=sm.similarity(am); //symmetric matrix
00206           } //transform to local
00207         for (int j=0; j < 3; ++j)
00208           for (int k=0; k <= j; ++k)
00209             apeSaveFile<<"  "<<sm[j][k]; //always write full matrix
00210         
00211         apeSaveFile<<std::endl;
00212         }
00213       }
00214     delete aliErr;
00215     apeSaveFile.close();
00216     }
00217   // clean up at end:  // FIXME: should we delete here or in destructor?
00218   delete theAlignableNavigator;
00219   theAlignableNavigator = 0;
00220 }
00221 
00222 // Run the algorithm on trajectories and tracks -------------------------------
00223 //____________________________________________________
00224 void ApeSettingAlgorithm::run(const edm::EventSetup &setup, const EventInfo &eventInfo)
00225 {
00226   // nothing to do here?
00227 }
00228 
00229 // Plugin definition for the algorithm
00230 DEFINE_EDM_PLUGIN(AlignmentAlgorithmPluginFactory,
00231                    ApeSettingAlgorithm, "ApeSettingAlgorithm");
00232 
00233 
00234