CDF II
CDF KITS
source navigation ]
diff markup ]
identifier search ]
freetext search ]
file search ]
 
Architecture: i386 ]
Version: 4.10.4 ] [ 4.10.5 ] [ 4.8.4 ] [ 4.8.4l3s ] [ 4.8.5 ] [ 4.9.0 ] [ 4.9.1 ] [ 4.9.1.hpt3 ] [ 4.9.1hpt3 ] [ 4.9.1top1 ] [ 5.0.0 ] [ 5.1.0 ] [ 5.1.0beamonly ] [ 5.1.1 ] [ 5.2.0 ] [ 5.3.0 ] [ 5.3.1 ] [ 5.3.1dsp ] [ 5.3.3 ] [ 5.3.3_nt ] [ 5.3.4 ] [ 6.1.1 ] [ 6.1.1b ] [ 6.1.2 ] [ 6.1.3 ] [ 6.1.4 ] [ 6.1.4int3 ] [ 6.1.4mc ] [ 6.1.4mc_a ] [ 6.1.6 ] [ development ]

001 //*****************************************************************************
002 //       FillMissingCotEntry <runnumber> <dbid_write> <dbid_read>
003 //    this executable will make an DB entry using the beamline entries in the
004 //    same store.
005 //
006 //      06/20/02  Hartmut Stadie  IEKP Karlsruhe
007 //*****************************************************************************
008 //=============================================================================
009 // RCS Current Revision Record
010 //-----------------------------------------------------------------------------
011 // $Source: /cdf/code/cdfcvs/run2/Alignment/test/FillMissingCotEntry.cc,v $
012 // $Revision: 1.8 $
013 // $Date: 2003/02/26 16:57:35 $
014 // $Author: stadie $
015 // $State: Exp $
016 // $Locker:  $
017 //*****************************************************************************
018 
019 #include <cmath>
020 #include <iostream>
021 #include <strstream>
022 #include <vector>
023 
024 #include "CalibDB/RunListKey.hh"      
025 #include "Alignment/CotBeam.hh"
026 #include "AlignmentDB/CotBeamPosition.Defs.hh"
027 #include "AlignmentDB/SiAlignFrame.Defs.hh"
028 #include "CalibDB/Helpers.hh"
029 #include "CalibDB/RunListDefs.hh" 
030 #include "RunConfigDB/BEAM_CONDITIONS.Defs.hh"
031 #include "DBManager/ConControl.hh"
032 #include "DBManager/IoEntry.hh"
033 #include "CLHEP/Matrix/Vector.h"
034 #include "CLHEP/Matrix/SymMatrix.h"
035 
036 //following the example in RunConfigDB/Test/BeamConditions.cc
037 int storeNumber(int run)
038 {
039   static bool initialized = false;
040   static PackageManager m;
041   if( ! initialized)
042     {
043       ConControl::instance()->addIoEntry(new IoEntry("offline","OTL","cdf_reader/reader@cdfofprd"));
044       m.setDefaultID("offline");
045       initialized = true;
046     }
047   // Define the manager & container/iterator for returned values.
048   static BEAM_CONDITIONS_mgr dmgr1("BEAM_CONDITIONS");
049   static BEAM_CONDITIONSContainer_ptr rvals1;
050   static BEAM_CONDITIONSContainer::iterator it1;
051   int store = -1;
052   // Set the key -- just simple run selection + cardname selection 
053   std::ostringstream srt;
054   //srt << run << ends;
055   srt << run;
056   std::string sql_string = "WHERE RC_RUNNUMBER = " ; 
057   sql_string += srt.str();
058   ROKey dbk(sql_string);
059   
060   // Do the fetch from the DB and print out returned rows.
061   Result rc = dmgr1.get(dbk,rvals1);
062   if (rc == Result::success)
063     {
064       //std::cout << "Rows got:" << std::endl;
065       it1 = rvals1->begin();
066       if(it1 != rvals1->end()) store = it1->tevStore();
067     }
068   else std::cout << "could not read RunConfigDB(error = "<< rc << ")" << std::endl;
069   return store;
070 }
071 
072 std::string keyAlgorithm(const std::string& dbid,
073                          const RunListKey& rlk)
074 {
075   RunList_mgr runIom(dbid,"RunList");
076   RunList_var row;
077 
078   if( !runIom.isValid() ) 
079     {
080       std::cerr<< "Error opening RunList database" << std::endl;
081       return rlk.algorithm();
082     }
083   
084   if(( runIom.get(rlk,row)!=Result::success ) ||
085      ( row->size() != 1 ))
086     {
087       std::cerr << "Failure of runIom.get() to retrieve db contents" << std::endl;  
088       return rlk.algorithm();
089     }
090   //std::cout << "new rlk:" << *(row->begin()) << std::endl;
091   return row->begin()->algorithm();
092 }
093 
094 bool meanBeamLine(CotBeamPosition& newcot, std::vector<CotBeamPositionContainer> input)
095 {
096   HepVector x(4,0);
097   HepSymMatrix G(4,0);
098   int ntracks = 0;
099   double sumz = 0;
100   double sumsigmaz = 0;
101   
102   for(std::vector<CotBeamPositionContainer>::const_iterator bpiter = input.begin() ;  bpiter != input.end() ; ++bpiter)
103     {
104       int ntracks2 = bpiter->begin()->Statistics0();
105       HepVector x2(4,0);
106       HepSymMatrix G2(4,0);
107 
108       x2[0] = bpiter->begin()->BeamX();
109       x2[1] = bpiter->begin()->BeamY();
110       x2[2] = bpiter->begin()->SlopeX();
111       x2[3] = bpiter->begin()->SlopeY();
112       
113       G2[0][0] = bpiter->begin()->FitCov00();
114       G2[0][1] = bpiter->begin()->FitCov01();
115       G2[0][2] = bpiter->begin()->FitCov02();
116       G2[0][3] = bpiter->begin()->FitCov03(); 
117       G2[1][1] = bpiter->begin()->FitCov11();
118       G2[1][2] = bpiter->begin()->FitCov12();
119       G2[1][3] = bpiter->begin()->FitCov13();
120       G2[2][2] = bpiter->begin()->FitCov22();
121       G2[2][3] = bpiter->begin()->FitCov23();
122       G2[3][3] = bpiter->begin()->FitCov33();
123  
124       sumz += ntracks2 * bpiter->begin()->BeamZ();
125       sumsigmaz += ntracks2 * bpiter->begin()->WidthZ();     
126       //       std::cout << "input:  n tracks:" << ntracks2 << std::endl;
127 //       std::cout << "x:" << x2;
128 //       std::cout << "G:" << G2;
129       int ierr = 0;
130       ntracks += ntracks2;
131       x += x2 * ntracks2;
132       G += G2.inverse(ierr) * ntracks2;
133      
134       if( ierr != 0 ) 
135         {
136           std::cout << "FillMissingCotEntry: matrix inversion failed." 
137                     << std::endl;
138           return false;
139         }
140     }
141   int ierr = 0;
142   x /= ntracks;
143   G /= ntracks;
144   G.invert(ierr);
145   if( ierr != 0 ) 
146     {
147       std::cout << "FillMissingCotEntry: matrix inversion failed." 
148                     << std::endl;
149       return false;
150     }
151 //   std::cout << "result:    n tracks:" << ntracks << std::endl;
152 //   std::cout << "x:" << x;
153 //   std::cout << "G:" << G;
154   //fill the table
155   std::ostringstream os;
156   
157  os << (long)1 << " ";
158   os << x[0] << " " << x[1] << " " << x[2] << " " 
159      << x[3] << " ";
160   os << (double)0 << " " << (double)0 << " " << (double)0 << " ";
161   os << sumz/ntracks << " " << sumsigmaz/ntracks << " ";
162   os << 0 << " " << 0 << " " << 0 << " " << 0 << " ";
163   os << 0 << " " << 0 << " " << 0 << " ";
164   os << 0 << " " << 0 << " ";
165   os << 0 << " " << 0 << " " << 0 << " " << 0 << " ";
166   os << 0 << " " << 0 << " " << 0 << " " << 0 << " ";
167   //cov
168   os << G[0][0] << " " << G[0][1] << " " << G[0][2] << " " << G[0][3] << " ";
169   os << G[1][1] << " " << G[1][2] << " " << G[1][3] << " ";
170   os << G[2][2] << " " << G[2][3] << " ";
171   os << G[3][3] << " ";
172  
173   //statistics
174   os << ntracks << " " << 0 << " ";
175   //flags
176   std::vector<CotBeamPositionContainer>::const_iterator bpiter = input.begin();
177   
178   long flag0,flag1;
179   long hist = (bpiter->begin()->Flag0() & 0xFFFF);
180   long algo = (((bpiter->begin()->Flag0()>>16) & 0xFFFF) >= 20) ? 15 : 5;
181   long srce = 10;// 5 online, 10 offline
182   flag0 = (algo << 16) + hist;
183   flag1 =  srce;
184   os << flag0 << " " << flag1 << " ";
185   //alignment
186   os << 0 << " " << 0 << " " << 0 << " "<< 0 << " ";
187   os << std::ends;
188   
189   std::istringstream is(os.str());    
190   is >> newcot;
191   return true;
192 }
193 
194 int main(int argc, char* argv[])
195 {
196   if(argc != 6)
197     {
198       std::cout << "USAGE: FillMissingCotEntry <runnumber> <history> <dbid_write> <dbid_read> <algorithm>"
199                 << std::endl;
200       return 1;
201     }
202   int runnumber = atoi(argv[1]);
203   int history = atoi(argv[2]);
204   std::string dbidwrite = argv[3];
205   std::string dbidread  = argv[4];
206   int algorithm = atoi(argv[5]);
207   
208   //get list with all runs that have a COT beamline
209   RunList_mgr runIom(dbidread,"RunList");
210   if( !runIom.isValid() ) 
211     {
212       std::cout << "FillMissingCotEntry: Error opening RunList database" 
213                << std::endl;
214       return 1;
215     }
216   RunListKey rlk("GOOD","CotBeamPosition",Run::undefined,
217                  Version::undefined);
218   RunList_var runlist;
219   if(runIom.get(rlk,runlist)!=Result::success) 
220     {
221       std::cout << "FillMissingCotEntry: Failure of runIom.get() to retrieve db contents" << std::endl;  
222       return 1;
223     }
224 
225   //find neighbors. 
226   int diffdown = 1000000;
227   int diffup = 1000000;
228   int rundown = 0,runup = 0;
229   int store = storeNumber(runnumber); 
230 
231   CotBeam cotbeam(dbidread);
232   cotbeam.setHistoryCode(history);
233   cotbeam.setAlgorithm(algorithm);
234   for(RunList::iterator iter = runlist->begin(); iter != runlist->end();
235       ++iter)
236     {
237       int newrun = iter->run();
238       int diff = runnumber - newrun;
239       
240       //They should be within 50 runs.
241       if(abs(diff) > 50) continue;
242  
243       if( diff == 0 )
244         {  
245           //cotbeam.setAlgorithm(algorithm-5);
246           //if(cotbeam.loadRun(newrun) == 0)
247           //  {
248           //    std::cout << "FillMissingCotEntry: run " << runnumber << " has an entry already." << std::endl;
249           //    return 1;
250           //   }
251           // cotbeam.setAlgorithm(algorithm);
252           if(cotbeam.loadRun(newrun) == 0)
253             {
254               std::cout << "FillMissingCotEntry: run " << runnumber << " has an entry already." << std::endl;
255               return 1;
256             }
257         }
258       else if( diff < 0 )
259         {
260           if(( -diff < diffdown ) && (storeNumber(newrun) == store))
261             {
262               //can we load the entry
263               if( cotbeam.loadRun(newrun) == 0 )
264                 {
265                   diffdown = -diff;
266                   rundown = newrun;
267                 }
268             }
269         }
270       else
271         if(( diff < diffup ) && (storeNumber(newrun) == store))
272           {
273             //can we load the entry
274             if( cotbeam.loadRun(newrun) == 0 )
275               {
276                 diffup = diff;
277                 runup = newrun;
278               }
279           }
280     }
281  
282   if( (runup == 0) && (rundown == 0)) 
283     {
284       std::cout << "FillMissingCotEntry: no other run with COT beam line information found for store " << store << "." << std::endl;
285        return 2;
286      }
287   std::cout << "FillMissingCotEntry: found: store: " << store << " run= " << rundown << "," 
288             << runup << std::endl;
289   
290   //make vector for good entries
291   std::vector<CotBeamPositionContainer> beampositions;
292   RunListKey oldkey;
293  
294   if(runup != 0)
295     {
296       if( cotbeam.loadRun(runup) == 0 )
297         {
298           CotBeamPositionContainer sbpc = cotbeam.getContainer();
299 
300           beampositions.push_back(sbpc);
301           oldkey = cotbeam.getKey();
302         }
303     }
304   if(rundown != 0)
305     {
306       if( cotbeam.loadRun(rundown) == 0 )
307         {
308           CotBeamPositionContainer sbpc = cotbeam.getContainer();
309          
310           beampositions.push_back(sbpc);
311           oldkey = cotbeam.getKey();
312         }
313     }
314   if(beampositions.empty())
315     {
316       std::cout << "FillMissingCotEntry: no good entry for the COT beam line found." << std::endl;
317       return 3;
318     }
319   
320   CotBeamPosition newbp;
321   CotBeamPositionContainer oldcontainer;
322   meanBeamLine(newbp, beampositions);
323 
324   std::cout << "FillMissingCotEntry:new beampos entry:" << newbp << std::endl;
325    
326 //   //fill DB
327   CotBeamPositionContainer contDB;
328   CotBeamPosition_mgr iomCotBeam(dbidwrite,"CotBeamPosition");
329   if(iomCotBeam.isValid()==false)
330     {
331       std::cout <<"FillMissingCotEntry: CotBeamPosition_mgr initialization failure" << std::endl;
332       return  false;
333     }                                                                
334   RunListKey qk(runnumber,Version::generate); //just add a new entry
335   qk.setDataStatus("GOOD");
336   qk.newVersion();
337   qk.setAlgorithm(keyAlgorithm(dbidread,oldkey));
338   contDB.clear();
339   contDB.push_back(newbp);
340   //add parents 
341   for(std::vector<CotBeamPositionContainer>::const_iterator bpiter = beampositions.begin() ;  
342       bpiter != beampositions.end() ; ++bpiter)
343     contDB.addParent(bpiter->key());
344   
345   if(iomCotBeam.put(qk,contDB) != Result::success)
346     {
347       std::cout << "FillMissingCotEntry: Put failed for CotBeamPosition Run = " << runnumber
348                 << " in database " << dbidwrite << "." << std::endl; 
349       return false;
350     }
351   std::cout << "FillMissingCotEntry: making COT DB entry...   done" << std::endl;
352   
353 }
354           
355 
356 

source navigation ] diff markup ] identifier search ] freetext search ] file search ]

This page was automatically generated by the LXR engine.
The LXR team
Valid HTML 4.01!

Send problems or questions to cdfcode@fnal.gov