Commit in slic/src on MAIN
LcioHitsCollectionBuilder.cc+46-91.15 -> 1.16
add support to slic for writing to same hits collection from different subdetectors

slic/src
LcioHitsCollectionBuilder.cc 1.15 -> 1.16
diff -u -r1.15 -r1.16
--- LcioHitsCollectionBuilder.cc	4 Apr 2008 00:56:10 -0000	1.15
+++ LcioHitsCollectionBuilder.cc	14 Oct 2009 20:48:25 -0000	1.16
@@ -1,4 +1,4 @@
-// $Header: /cvs/lcd/slic/src/LcioHitsCollectionBuilder.cc,v 1.15 2008/04/04 00:56:10 jeremy Exp $
+// $Header: /cvs/lcd/slic/src/LcioHitsCollectionBuilder.cc,v 1.16 2009/10/14 20:48:25 jeremy Exp $
 
 #include "LcioHitsCollectionBuilder.hh"
 
@@ -28,6 +28,7 @@
 
 namespace CLHEP {}
 using namespace CLHEP;
+using std::string;
 
 namespace slic
 {
@@ -64,7 +65,7 @@
         // HCID
         G4int hcid;
 
-        LCCollectionVec* collVect = 0;
+        LCCollectionVec* collVec = 0;
 
 
         for (std::vector<int>::const_iterator iter = hcids.begin();
@@ -74,7 +75,9 @@
         
             hcid = *iter;
         
+#ifdef SLIC_LOG
             log().debug(" Creating hits collections for HCID: " + StringUtil::toString( hcid ) );
+#endif
         
             // retrieve Sensitive Detector ptr
             G4SensitiveDetector* SD =	static_cast<G4SensitiveDetector*>( G4SDManager::GetSDMpointer()->FindSensitiveDetector( HCtbl->GetSDname( hcid ) ) );
@@ -88,25 +91,43 @@
 
                     // add a LCCollectionVec if got a HC
                     if ( HC ) {
-
+                                                                        
                         // set LCIO endcap bit in the flag (i.e. CHBIT_BARREL ) according to SDs setting
                         setEndcapFlag( SD );
 
                         // create collection vector based on type of SD
-                        collVect = createCollectionVec( HC, SD->getType() );
-
+                        collVec = createCollectionVec( HC, SD->getType() );
+                        
                         // Store the cellID description into the LCIO::cellIDEncoding parameter in the collection.
                         if ( SD->getIdSpec() ) {
                             std::string id = SD->getIdSpec()->getFieldDescription();
 #if LCIO_VERSION_GE( 1, 7)
-                            collVect->parameters().setValue(LCIO::CellIDEncoding, id) ;
+                            collVec->parameters().setValue(LCIO::CellIDEncoding, id) ;
 #else
-                            collVect->parameters().setValue("CellIDEncoding", id) ;
+                            collVec->parameters().setValue("CellIDEncoding", id) ;
 #endif
                         }
 
-                        // add collection vector to LCEvent
-                        m_currentLCEvent->addCollection( collVect, HC->GetName() );
+                        // Check for existing collection.
+                        if (containsCollection( m_currentLCEvent, HC->GetName() ) )
+                        {
+#ifdef SLIC_LOG
+                            log().debug("Adding hits to existing collection " + HC->GetName() + ".");
+#endif
+                            // Update existing collection.
+                            // TODO: Check for matching id scheme and flags!
+                            LCCollectionVec* collection = (LCCollectionVec*) m_currentLCEvent->getCollection( HC->GetName() );
+                            collection->insert( collection->begin(), collVec->begin(), collVec->end() );
+                        }
+                        // No collection found.
+                        else
+                        {
+#ifdef SLIC_LOG
+                            log().debug("Creating new hit collection collection " + HC->GetName() + ".");
+#endif
+                            // Add new collection vector to LCEvent.
+                            m_currentLCEvent->addCollection( collVec, HC->GetName() );
+                        }
                     }
       
                     else {
@@ -116,6 +137,9 @@
             }
         }
     }
+
+
+
     // create the CollectionVec (decides which overloaded subfunction to call)
     IMPL::LCCollectionVec* LcioHitsCollectionBuilder::createCollectionVec(G4VHitsCollection* g4HC,
                                                                           G4SensitiveDetector::EType SDtype)
@@ -472,4 +496,17 @@
         log().verbose("Set CHBIT_PDG: " + StringUtil::toString( setting ) );
 #endif
     }
+
+    bool LcioHitsCollectionBuilder::containsCollection(EVENT::LCEvent* event, const string& collectionName)
+    {
+        for (std::vector<string>::const_iterator iter = event->getCollectionNames()->begin(); iter != event->getCollectionNames()->end(); iter++)
+        {
+            const string thisName = *iter;
+            if (thisName.compare(collectionName) == 0)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
 }
CVSspam 0.2.8