Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:55:59

0001 //   COCOA class implementation file
0002 //Id:  FittedEntriesManager.cc
0003 //CAT: Model
0004 //
0005 //   History: v1.0
0006 //   Pedro Arce
0007 #include <map>
0008 #include <fstream>
0009 #include <iostream>
0010 #include <iomanip>
0011 
0012 #include "Alignment/CocoaFit/interface/FittedEntriesManager.h"
0013 #include "Alignment/CocoaModel/interface/Model.h"
0014 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
0015 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
0016 
0017 FittedEntriesManager* FittedEntriesManager::instance = nullptr;
0018 
0019 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0020 //@@  Gets the only instance of Model
0021 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0022 FittedEntriesManager* FittedEntriesManager::getInstance() {
0023   if (!instance) {
0024     instance = new FittedEntriesManager;
0025   }
0026   return instance;
0027 }
0028 
0029 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0030 //@@ add a new set of fitted entries
0031 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0032 void FittedEntriesManager::AddFittedEntriesSet(FittedEntriesSet* fents) { theFittedEntriesSets.push_back(fents); }
0033 
0034 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0035 //@@ dump data to histograms
0036 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0037 //tvoid FittedEntriesManager::WriteHeader()
0038 void FittedEntriesManager::MakeHistos() {
0039   //----------- Get
0040   //----------- Loop entries
0041   //-  vfescite = theFittedEntriesSets.begin();
0042   //  std::vector< FittedEntry* > vfe = theFittedEntriesSets.begin()->FittedEntries();
0043   //  std::vector< FittedEntry* >::const_iterator vfecite2;
0044   //-- Number of fitted entries (equal for all Fitted Entries Sets )
0045   if (ALIUtils::debug >= 5)
0046     std::cout << "No sets2 " << theFittedEntriesSets.size() << " No ent "
0047               << ((*(theFittedEntriesSets.begin()))->FittedEntries()).size() << std::endl;
0048   std::ofstream fout;
0049   std::ofstream fout2;
0050   fout.open("fittedEntries.out");
0051   fout2.open("longFittedEntries.out");
0052   //---------- Dump dimensions
0053   ALIUtils::dumpDimensions(fout);
0054   ALIUtils::dumpDimensions(fout2);
0055 
0056   AddFittedEntriesSet(new FittedEntriesSet(theFittedEntriesSets));  //add a new set that averages all the others
0057 
0058   //---------- Loop sets of entries
0059   std::vector<FittedEntriesSet*>::const_iterator vfescite;
0060   std::vector<FittedEntry*>::const_iterator vfecite;
0061   ALIint jj = 1;
0062   for (vfescite = theFittedEntriesSets.begin(); vfescite != theFittedEntriesSets.end(); ++vfescite) {
0063     //---------- Loop entries
0064     if (vfescite == theFittedEntriesSets.begin()) {
0065       //----- dump entries names if first set
0066       fout << "  ";
0067       ALIint ii = 0;
0068       for (vfecite = ((*vfescite)->FittedEntries()).begin(); vfecite != ((*vfescite)->FittedEntries()).end();
0069            ++vfecite) {
0070         ALIstring filename = createFileName((*vfecite)->getOptOName(), (*vfecite)->getEntryName());
0071         fout << ii << ": " << std::setw(13) << filename << " ";
0072         ;
0073         if (ALIUtils::debug >= 3)
0074           std::cout << ii << ": " << std::setw(13) << filename << " = " << (*vfecite)->getName() << std::endl;
0075         if (ALIUtils::debug >= 3)
0076           std::cout << filename << " ";
0077         if (ALIUtils::debug >= 3)
0078           std::cout << "OPENING FITTED ENTRIES file " << filename << std::endl;
0079         ii++;
0080       }
0081       //      fout << std::setw(17) << "2:-4:";
0082       fout << std::endl;
0083       if (ALIUtils::debug >= 3)
0084         std::cout << std::endl;
0085     }
0086     //----- Dump entry set number
0087     fout << jj << " ";
0088     fout2 << jj << " ";
0089     //----- Dump measurements date
0090     GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
0091     if (gomgr->GlobalOptions()["DumpDateInFittedEntries"] >= 1) {
0092       fout << (*vfescite)->getDate() << " " << (*vfescite)->getTime() << " ";
0093       fout2 << (*vfescite)->getDate() << " " << (*vfescite)->getTime() << " ";
0094     }
0095 
0096     //ALIint ii = 0;
0097     for (vfecite = ((*vfescite)->FittedEntries()).begin(); vfecite != ((*vfescite)->FittedEntries()).end(); ++vfecite) {
0098       //      std::cout << ii << *vfescite << " FITTEDENTRY: "   << vfecite << " " <<*vfecite << " " << (*vfecite)->Value() << std::endl;
0099       //      if( ii == 2 || ii == 4 ) {
0100       fout << std::setprecision(8) << std::setw(10) << (*vfecite)->getValue() << " " << (*vfecite)->getSigma() << "  ";
0101       //- fout << std::setw(9) << std::setprecision(6) << (*vfecite)->getValue()  << " +- " << (*vfecite)->getSigma() << "  ";
0102       //      }
0103       if (ALIUtils::debug >= 3)
0104         std::cout << " FITTEDENTRY:" << std::setprecision(5) << std::setw(8) << (*vfecite)->getValue() << " +- "
0105                   << (*vfecite)->getSigma() << std::endl;
0106 
0107       ALIstring filename = createFileName((*vfecite)->getOptOName(), (*vfecite)->getEntryName());
0108       fout2 << std::setprecision(8) << std::setw(10) << filename << " " << (*vfecite)->getValue() << " "
0109             << (*vfecite)->getSigma() << "  ";
0110       //ii++;
0111     }
0112     //    dumpEntriesSubstraction( fout, *(*vfescite), 2, 4);
0113     fout << std::endl;
0114     fout2 << std::endl;
0115     if (ALIUtils::debug >= 3)
0116       std::cout << std::endl;
0117     jj++;
0118   }
0119   fout.close();
0120   fout2.close();
0121 
0122   GetDifferentBetweenLasers();
0123 }
0124 
0125 #include "Alignment/CocoaModel/interface/LightRay.h"
0126 #include "Alignment/CocoaModel/interface/OpticalObject.h"
0127 
0128 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0129 void FittedEntriesManager::GetDifferentBetweenLasers() {
0130   std::vector<OpticalObject*> optoList = Model::OptOList();
0131   std::vector<OpticalObject*>::const_iterator ite;
0132   std::map<ALIstring, LightRay*> lrays;
0133 
0134   for (ite = optoList.begin(); ite != optoList.end(); ++ite) {
0135     if ((*ite)->type() == "laser") {
0136       LightRay* lightray = new LightRay;
0137       lightray->startLightRay(*ite);
0138       lrays[(*ite)->parent()->name()] = lightray;
0139     }
0140   }
0141 
0142   std::map<ALIstring, LightRay*>::const_iterator lite1, lite2;
0143   for (lite1 = lrays.begin(); lite1 != lrays.end(); ++lite1) {
0144     lite2 = lite1;
0145     ++lite2;
0146     for (; lite2 != lrays.end(); ++lite2) {
0147       if (lite1 == lite2)
0148         continue;
0149       CLHEP::Hep3Vector dirdiff = ((*lite1).second->direction() - (*lite2).second->direction());
0150       if (ALIUtils::debug >= 0) {
0151         std::cout << "LASER DIFF " << (*lite1).first << " & " << (*lite2).first << " " << dirdiff.mag() * 180. / M_PI
0152                   << "o " << dirdiff.mag() << " rad " << dirdiff << std::endl;
0153 
0154         (*lite1).second->dumpData(ALIstring(" laser ") + (*lite1).first);
0155         (*lite2).second->dumpData(ALIstring(" laser ") + (*lite2).first);
0156       }
0157     }
0158   }
0159 }
0160 
0161 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0162 //@@ create file name to dump fitted entries values taking the last name of optoName and the full entryName with space converted to '.'
0163 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0164 ALIstring FittedEntriesManager::createFileName(const ALIstring& optoName, const ALIstring& entryName) {
0165   //  std::cout << "in createFileName " << optoName << " " << entryName << std::endl;
0166   ALIstring filename;
0167   //-  std::cout << "o" << optoName << " e " << entryName << std::endl;
0168   /*  ALIint last_slash =  optoName.rfind('/');
0169   ALIint size = optoName.length();
0170   //-   std::cout << last_slash << " " << size << "optoname " << optoName << std::endl;
0171 
0172   filename = optoName.substr( last_slash+1, size );
0173   */
0174   //----- substitute '/' by '.' in opto name
0175   filename = optoName.substr(2, optoName.size());  // skip the first 's/'
0176   ALIint slash = -1;
0177   for (;;) {
0178     slash = filename.find('/', slash + 1);
0179     if (slash == -1)
0180       break;
0181     filename[slash] = '.';
0182   }
0183 
0184   //----- Check if there is a ' ' in entry (should not happen now)
0185   ALIint space = entryName.rfind(' ');
0186   filename.append(".");
0187   ALIstring en = entryName;
0188   if (space != -1)
0189     en[space] = '_';
0190 
0191   //----- Merge opto and entry names
0192   // now it is not used as filename   filename.append( en + ".out");
0193   filename.append(en);
0194   if (ALIUtils::debug >= 3)
0195     std::cout << "filename " << filename << std::endl;
0196 
0197   return filename;
0198 }
0199 
0200 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0201 //@@ create file name to dump fitted entries values taking the last name of optoName and the full entryName with space converted to '-'
0202 // entry1/2 are the entries name including OptO name
0203 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0204 void FittedEntriesManager::dumpEntriesSubstraction(std::ofstream& fout,
0205                                                    FittedEntriesSet& fes,
0206                                                    ALIint order1,
0207                                                    ALIint order2) {
0208   //---------- Found order of entry1 and entry2 in FittedEntriesSet fes
0209   // (the order will be the same for every FittedEntriesSet
0210   //std::vector< FittedEntriesSet* >::const_iterator vfescite = theFittedEntriesSets.begin();
0211   /* std::vector< FittedEntry* >::const_iterator vfecite;
0212   ALIint order1, order2;
0213   ALIint jj=0;
0214   for( vfecite = (fes.FittedEntries()).begin(); vfecite != (fes.FittedEntries()).end(); vfecite++) {
0215     ALIstring entryTemp = (*vfecite)->OptOName() + "/" + (*vfecite)->EntryName();
0216     if(entryTemp == entry1) order1 = jj;
0217     if(entryTemp == entry2) order2 = jj;
0218     jj++;
0219   }
0220   */
0221 
0222   FittedEntry* fe1 = *((fes.FittedEntries()).begin() + order1);
0223   FittedEntry* fe2 = *((fes.FittedEntries()).begin() + order2);
0224   //-------- Substract values of entry1 and entry2 (with errors)
0225   ALIdouble val1 = fe1->getValue();
0226   ALIdouble val2 = fe2->getValue();
0227   ALIdouble sig1 = fe1->getSigma();
0228   ALIdouble sig2 = fe2->getSigma();
0229   ALIdouble val = val1 - val2;
0230   ALIdouble sig = sqrt(sig1 * sig1 + sig2 * sig2);
0231   //-  std::cout << "CHECK " << val1 << " "<< val2 << " "<< sig1 << " "<< sig2 << std::endl;
0232   fout << std::setprecision(6) << std::setw(8) << val << " +- " << sig << "  ";
0233   if (ALIUtils::debug >= 3)
0234     std::cout << " FITTEDENTRY:" << std::setprecision(5) << std::setw(8) << val << " +- " << sig << std::endl;
0235 }
0236 
0237 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0238 /*void FittedEntriesManager::MakeHistos1()
0239 {
0240 
0241   std::vector< FittedEntriesSet* >::const_iterator vfescite;
0242   std::vector< FittedEntry* >::const_iterator vfecite;
0243   //----------- Get 
0244   //----------- Loop entries
0245   vfescite = theFittedEntriesSets.begin(); 
0246   //  std::vector< FittedEntry* > vfe = theFittedEntriesSets.begin()->FittedEntries();
0247   //  std::vector< FittedEntry* >::const_iterator vfecite2;
0248   ALIint ii;
0249   //-- Number of fitted entries (equal for all Fitted Entries Sets 
0250   ALIint NoFitEnt = ( ( *(theFittedEntriesSets.begin()) )->FittedEntries() ).size();
0251   if( ALIUtils::debug >= 3) std::cout << "No sets1 " << theFittedEntriesSets.size() << " No ent " << NoFitEnt << std::endl;
0252   std::ofstream fout;
0253   ALIint jj;
0254   for( ii = 0; ii < NoFitEnt; ii++) {    
0255     jj = 1;
0256 
0257     for( vfescite = theFittedEntriesSets.begin(); vfescite != theFittedEntriesSets.end(); vfescite++) {
0258       vfecite = ((*vfescite)->FittedEntries()).begin() + ii;
0259       //----- create file name
0260       if( vfescite == theFittedEntriesSets.begin() ) {
0261     if( ALIUtils::debug >= 3) std::cout << " create filename " << (*vfecite)->OptOName() <<  (*vfecite)->EntryName() << std::endl;
0262         ALIstring filename = createFileName( (*vfecite)->OptOName(), (*vfecite)->EntryName() );  
0263     fout.open( filename.c_str() );
0264     if( ALIUtils::debug >= 3) std::cout << "OPENING FITTED ENTRIES file " << filename << std::endl;
0265       }
0266       //      std::cout << ii << *vfescite << " FITTEDENTRY: "   << vfecite << " " <<*vfecite << " " << (*vfecite)->Value() << std::endl;
0267       fout << jj << " " << (*vfecite)->Value()  << (*vfecite)->Sigma() << std::endl;   
0268       if( ALIUtils::debug >= 3) std::cout << ii << " FITTEDENTRY: " << (*vfecite)->OptOName() << " / " << (*vfecite)->EntryName() << " " << (*vfecite)->Value() << " " << (*vfecite)->Sigma() << std::endl;
0269       jj++;
0270     }
0271     fout.close();
0272   }
0273 
0274 }
0275 */