Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

LatticeMaker.cxx

Go to the documentation of this file.
00001 
00002 // $Id: LatticeMaker.cxx,v 1.2 2001/03/02 08:19:06 west Exp $
00003 //
00004 // LatticeMaker
00005 //
00006 // Begin_Html<img src="../../pedestrians.gif" align=center>
00007 // <a href="../source_warning.html">Warning for beginners</a>.<br>
00008 // Also see <a href="../../root_crib/index.html">The ROOT Crib</a> and
00009 // <a href="../index.html">The MINOS Class User Guide</a>End_Html
00010 //
00011 // N. West 02/2001
00012 //
00013 // Purpose:                                                           
00014 // LatticeMaker is a tool to simplify Lattice building for an         
00015 // arbitrary n:m relationship.  The user provides 2 ROOT collections  
00016 // and an association function and the LatticeMaker build and returns 
00017 // a Lattice.                                                         
00018 //                                                                    
00019 // Caution: the LatticeMakeer is not particularly efficient; it loops 
00020 //          over all n*m combinations to fill out the Lattice.  Use   
00021 //          LatticeBuilder for a simple 1:n Lattice.                  
00022 // Purpose:
00023 //
00025 
00026 #include <string>
00027 
00028 #include "Lattice/Lattice.h"
00029 #include "Lattice/LatticeBuilder.h"
00030 #include "Lattice/LatticeMaker.h"
00031 #include "MessageService/MsgService.h"
00032  
00033 ClassImp(LatticeMaker)
00034 
00035 //   Definition of static data members
00036 //   *********************************
00037 
00038 CVSID("$Id: LatticeMaker.cxx,v 1.2 2001/03/02 08:19:06 west Exp $");
00039 
00040 // Definition of member functions (alphabetical order)
00041 // ***************************************************
00042 
00043 
00044 
00045 //.....................................................................
00046 
00047 Lattice* LatticeMaker::CreateLattice( const char* nameLeft,
00048                                       const char* nameRight,
00049                                       const char* type ) {
00050 
00051 //  Purpose:   Create a new empty Lattice.
00052 //  Arguments: 
00053 //     nameLeft  in   Class name of objects on left side of lattice.
00054 //     nameRight in   Class name of objects on left side of lattice.
00055 //     type      in   Lattice type.  One of "1:n", "n:1" or "n:m" 
00056 //                    the default is "n:m"
00057 //            
00058 //  Return:    
00059 //     New empty Lattice.  The caller is responsible for deleting it.
00060 
00061   
00062 //  Program Notes:-
00063 //  =============
00064 //
00065 //  None.
00066 //
00067 //  Contact:   N. West
00068 
00069   string latType = type;
00070 
00071 //  Create new Lattice supporting  1:n relationship
00072   if ( latType == "1:n" 
00073      ) return  new Lattice(nameLeft,
00074                            nameRight,
00075                            LatticeBase::LNodeMulti,
00076                            LatticeBase::RLinkMulti);
00077 
00078 //  Create new Lattice supporting  n:1 relationship
00079   if ( latType == "n:1" 
00080      ) return  new Lattice(nameLeft,
00081                            nameRight,
00082                            LatticeBase::RNodeMulti,
00083                            LatticeBase::LLinkMulti);
00084 
00085 //  Create new Lattice supporting full n:m relationship
00086   return new Lattice(nameLeft,
00087                      nameRight,
00088                      LatticeBase::LNodeMulti,
00089                      LatticeBase::RNodeMulti,
00090                      LatticeBase::LLinkMulti,
00091                      LatticeBase::RLinkMulti);
00092 }                            
00093 //.....................................................................
00094 
00095 Lattice* LatticeMaker::CreateLattice( const TObject* objLeft,
00096                                       const TObject* objRight,
00097                                       const char* type ) {
00098 
00099 //  Purpose:   Create a new empty Lattice.
00100 //  Arguments: 
00101 //     objLeft  in   Example object for left side of lattice.
00102 //     objRight in   Example object for right side of lattice.
00103 //     type     in   Lattice type.  One of "1:n", "n:1" or "n:m" 
00104 //                   the default is "n:m"
00105 //            
00106 //  Return:    
00107 //     New empty Lattice.  The caller is responsible for deleting it.
00108 
00109   
00110 //  Program Notes:-
00111 //  =============
00112 //
00113 //  objLeft and objRight are just used to get the names for the
00114 //  two sides.
00115 //
00116 //  Contact:   N. West
00117 
00118     return CreateLattice(objLeft->ClassName(),
00119                          objRight->ClassName(),
00120                          type);
00121 
00122 }                            
00123 //.....................................................................
00124 
00125 Lattice* LatticeMaker::CreateLattice(const TCollection* setLeft, 
00126                                      const TCollection* setRight, 
00127                                      AssociateFunc* asFunc,
00128                                      const char* type ) {
00129 
00130 //  Purpose:   Create a new Lattice between setLeft and setRight.
00131 //  Arguments: 
00132 //     setLeft  in   Left set.         
00133 //     setRight in   Right set.         
00134 //     asFunc   in   Association function.  Returns kTRUE if entry in 
00135 //                   first set is associated with entry in the second.
00136 //     type     in   Lattice type.  One of "1:n", "n:1" or "n:m" 
00137 //                   the default is "n:m"
00138 //            
00139 //  Return:    
00140 //     New Lattice.  The caller is responsible for deleting it.
00141 //                   Returns 0 if either or both sets empty.
00142 
00143 //  Contact:   N. West
00144 
00145 //  Check .
00146 
00147   TIter itrLeft(setLeft);
00148   TIter itrRight(setRight);
00149   TObject* objLeft = itrLeft();
00150   TObject* objRight = itrRight();
00151 
00152   if ( ! objLeft || ! objRight ) {
00153     MSG("Lat",Msg::kWarning) 
00154       << "Cannot build lattice; one or both sets empty," << endl;
00155     return 0;
00156   }
00157 
00158 //  Create new Lattice.
00159   Lattice* lat = CreateLattice(objLeft->ClassName(),
00160                                objRight->ClassName(),
00161                                type);
00162 
00163   string latType = type;
00164   itrLeft.Reset();
00165   itrRight.Reset();
00166 
00167   if ( latType == "1:n" ) { 
00168     while ( (objRight = itrRight()) ){
00169       itrLeft.Reset();
00170       while ( (objLeft = itrLeft()) ) {
00171         if ( asFunc(objLeft,objRight) ) {
00172           lat->ConnectLR(objLeft,0,objRight);
00173           break;
00174         }
00175       }
00176     }
00177 
00178   }
00179   else  if ( latType == "n:1" ) { 
00180     while ( (objLeft = itrLeft()) ){
00181      itrRight.Reset();
00182      while ( (objRight = itrRight()) ) {
00183        if ( asFunc(objLeft,objRight) ) {
00184          lat->ConnectLR(objLeft,0,objRight);
00185          break;
00186        }
00187      }
00188     }
00189   }
00190 
00191   else { 
00192     while ( (objLeft = itrLeft()) ){
00193      itrRight.Reset();
00194      while ( (objRight = itrRight()) ) {
00195        if ( asFunc(objLeft,objRight) 
00196            ) lat->ConnectLR(objLeft,0,objRight);
00197      }
00198     }
00199   }
00200 
00201      
00202   return lat;
00203 
00204 }                            
00205 //.....................................................................
00206 
00207 Lattice* LatticeMaker::Create1ToNLattice(const TCollection* setLeft, 
00208                                          const TCollection* setRight, 
00209                                          KeyFunc* leftFunc,
00210                                          KeyFunc* rightFunc ) {
00211 
00212 //  Purpose:   Create a new 1:n Lattice between setLeft and setRight.
00213 //  Arguments: 
00214 //     setLeft   in  Left set.  
00215 //                   Each member is associated to 1 or more of right set
00216 //     setRight  in  Right set.       
00217 //                   Each member is associated to 1 of left set
00218 //     LeftFunc  in  Key function for setLeft members.
00219 //                   Returns a unique key.
00220 //     RightFunc in  Key function for rightSet members.
00221 //                   Returns the unique key of the setLeft member
00222 //                   with which this member is associated.
00223 //            
00224 //  Return:    
00225 //     New Lattice.  The caller is responsible for deleting it.
00226 //                   Returns 0 if either or both sets empty.
00227 
00228 //  Contact:   N. West
00229 
00230 //  Check .
00231 
00232   TIter itrLeft(setLeft);
00233   TIter itrRight(setRight);
00234   TObject* objLeft = itrLeft();
00235   TObject* objRight = itrRight();
00236 
00237   if ( ! objLeft || ! objRight ) {
00238     MSG("Lat",Msg::kWarning) 
00239       << "Cannot build lattice; one or both sets empty," << endl;
00240     return 0;
00241   }
00242 
00243 //  Create new 1:n  Lattice.
00244   Lattice* lat = CreateLattice(objLeft->ClassName(),
00245                                objRight->ClassName(),
00246                                "1:n");
00247 
00248   
00249 // Create a LatticeBuilder and dock lattice.
00250   LatticeBuilder builder;
00251   builder.DockLattice(lat, Lattice::kLeft);
00252 
00253 //  Store the primary keys.
00254    itrLeft.Reset();
00255    while ( (objLeft = itrLeft() ) 
00256          )  builder.AddPrimary(leftFunc(objLeft), objLeft);
00257 
00258 //  Connect secondaries to primary.
00259    itrRight.Reset();
00260    while ( (objRight = itrRight() ) 
00261          )  builder.AddSecondary(rightFunc(objRight), objRight);
00262 
00263   return lat;
00264 
00265 }                            

Generated on Mon Feb 15 11:06:50 2010 for loon by  doxygen 1.3.9.1