@@ -365,6 +365,9 @@ struct McSGCandProducer {
365365 Produces<aod::UDMcCollsLabels> outputMcCollsLabels;
366366 Produces<aod::UDMcTrackLabels> outputMcTrackLabels;
367367
368+ // save all McTruth, even if the collisions is not reconstructed
369+ Configurable<bool > saveAllMcCollisions{" saveAllMcCollisions" , true , " save all McCollisions" };
370+
368371 using CCs = soa::Join<aod::Collisions, aod::EvSels, aod::McCollisionLabels>;
369372 using BCs = soa::Join<aod::BCsWithTimestamps, aod::BcSels, aod::Run3MatchedToBCSparse>;
370373 using TCs = soa::Join<aod::Tracks, aod::McTrackLabels>;
@@ -539,33 +542,10 @@ struct McSGCandProducer {
539542 }
540543 }
541544
542- void init (InitContext& context)
545+ // updating McTruth data and links to reconstructed data
546+ void procWithSgCand (aod::McCollisions const & mccols, aod::McParticles const & mcparts,
547+ UDCCs const & sgcands, UDTCs const & udtracks)
543548 {
544- // add histograms for the different process functions
545- if (context.mOptions .get <bool >(" processMC" )) {
546- registry.add (" mcTruth/collisions" , " Number of associated collisions" , {HistType::kTH1F , {{11 , -0.5 , 10.5 }}});
547- registry.add (" mcTruth/collType" , " Collision type" , {HistType::kTH1F , {{5 , -0.5 , 4.5 }}});
548- registry.add (" mcTruth/IVMpt" , " Invariant mass versus p_{T}" , {HistType::kTH2F , {{150 , 0.0 , 3.0 }, {150 , 0.0 , 3.0 }}});
549- }
550- }
551-
552- // process function for MC data
553- // save the MC truth of all events of interest and of the DG events
554- void processMC (aod::McCollisions const & mccols, aod::McParticles const & mcparts,
555- UDCCs const & sgcands, UDTCs const & udtracks,
556- CCs const & /* collisions*/ , BCs const & /* bcs*/ , TCs const & /* tracks*/ )
557- {
558- if (verboseInfoMC) {
559- LOGF (info, " Number of McCollisions %d" , mccols.size ());
560- LOGF (info, " Number of SG candidates %d" , sgcands.size ());
561- LOGF (info, " Number of UD tracks %d" , udtracks.size ());
562- }
563- if (sgcands.size () <= 0 ) {
564- if (verboseInfoMC)
565- LOGF (info, " No DG candidates to save!" );
566- return ;
567- }
568-
569549 // use a hash table to keep track of the McCollisions which have been added to the UDMcCollision table
570550 // {McCollisionId : udMcCollisionId}
571551 // similar for the McParticles which have been added to the UDMcParticle table
@@ -575,21 +555,20 @@ struct McSGCandProducer {
575555
576556 // loop over McCollisions and UDCCs simultaneously
577557 auto mccol = mccols.iteratorAt (0 );
578- auto sgcand = sgcands.iteratorAt (0 );
579558 auto lastmccol = mccols.iteratorAt (mccols.size () - 1 );
559+ auto mccolAtEnd = false ;
560+
561+ auto sgcand = sgcands.iteratorAt (0 );
580562 auto lastsgcand = sgcands.iteratorAt (sgcands.size () - 1 );
563+ auto sgcandAtEnd = false ;
581564
582565 // advance dgcand and mccol until both are AtEnd
583566 int64_t mccolId = mccol.globalIndex ();
584567 int64_t mcsgId = -1 ;
585- // int64_t colId = -1;
586- auto sgcandAtEnd = sgcand == lastsgcand;
587- auto mccolAtEnd = mccol == lastmccol;
588- bool goon = !sgcandAtEnd || !mccolAtEnd;
568+
569+ bool goon = true ;
589570 while (goon) {
590- auto bcIter = mccol.bc_as <BCs>();
591- uint64_t globBC = bcIter.globalBC ();
592- // uint64_t globBC = 0;
571+ auto globBC = mccol.bc_as <BCs>().globalBC ();
593572 // check if dgcand has an associated McCollision
594573 if (sgcand.has_collision ()) {
595574 auto sgcandCol = sgcand.collision_as <CCs>();
@@ -600,7 +579,6 @@ struct McSGCandProducer {
600579 mcsgId = -1 ;
601580 }
602581 } else {
603- // colId = -1;
604582 mcsgId = -1 ;
605583 }
606584 if (verboseInfoMC)
@@ -659,7 +637,7 @@ struct McSGCandProducer {
659637 auto mcPart = track.mcParticle ();
660638 auto mcCol = mcPart.mcCollision ();
661639 if (mcColIsSaved.find (mcCol.globalIndex ()) == mcColIsSaved.end ()) {
662- updateUDMcCollisions (mcCol, mcCol. bc_as <BCs>(). globalBC () );
640+ updateUDMcCollisions (mcCol, globBC );
663641 mcColIsSaved[mcCol.globalIndex ()] = outputMcCollisions.lastIndex ();
664642 }
665643 updateUDMcParticle (mcPart, mcColIsSaved[mcCol.globalIndex ()], mcPartIsSaved);
@@ -687,6 +665,7 @@ struct McSGCandProducer {
687665 if (mcColIsSaved.find (mccolId) == mcColIsSaved.end ()) {
688666 if (verboseInfoMC)
689667 LOGF (info, " Saving McCollision %d" , mccolId);
668+
690669 // update UDMcCollisions
691670 updateUDMcCollisions (mccol, globBC);
692671 mcColIsSaved[mccolId] = outputMcCollisions.lastIndex ();
@@ -710,7 +689,71 @@ struct McSGCandProducer {
710689 LOGF (info, " End of loop mcsgId %d mccolId %d" , mcsgId, mccolId);
711690 }
712691 }
692+
693+ // updating McTruth data only
694+ void procWithoutSgCand (aod::McCollisions const & mccols, aod::McParticles const & mcparts)
695+ {
696+ // use a hash table to keep track of the McCollisions which have been added to the UDMcCollision table
697+ // {McCollisionId : udMcCollisionId}
698+ // similar for the McParticles which have been added to the UDMcParticle table
699+ // {McParticleId : udMcParticleId}
700+ std::map<int64_t , int64_t > mcColIsSaved;
701+ std::map<int64_t , int64_t > mcPartIsSaved;
702+
703+ // loop over McCollisions
704+ for (auto const & mccol : mccols) {
705+ int64_t mccolId = mccol.globalIndex ();
706+ uint64_t globBC = mccol.bc_as <BCs>().globalBC ();
707+
708+ // update UDMcCollisions and UDMcParticles
709+ if (mcColIsSaved.find (mccolId) == mcColIsSaved.end ()) {
710+ if (verboseInfoMC)
711+ LOGF (info, " Saving McCollision %d" , mccolId);
712+
713+ // update UDMcCollisions
714+ updateUDMcCollisions (mccol, globBC);
715+ mcColIsSaved[mccolId] = outputMcCollisions.lastIndex ();
716+
717+ // update UDMcParticles
718+ auto mcPartsSlice = mcparts.sliceBy (mcPartsPerMcCollision, mccolId);
719+ updateUDMcParticles (mcPartsSlice, mcColIsSaved[mccolId], mcPartIsSaved);
720+ }
721+ }
722+ }
723+
724+ void init (InitContext& context)
725+ {
726+ // add histograms for the different process functions
727+ if (context.mOptions .get <bool >(" processMC" )) {
728+ registry.add (" mcTruth/collisions" , " Number of associated collisions" , {HistType::kTH1F , {{11 , -0.5 , 10.5 }}});
729+ registry.add (" mcTruth/collType" , " Collision type" , {HistType::kTH1F , {{5 , -0.5 , 4.5 }}});
730+ registry.add (" mcTruth/IVMpt" , " Invariant mass versus p_{T}" , {HistType::kTH2F , {{150 , 0.0 , 3.0 }, {150 , 0.0 , 3.0 }}});
731+ }
732+ }
733+
734+ // process function for MC data
735+ // save the MC truth of all events of interest and of the DG events
736+ void processMC (aod::McCollisions const & mccols, aod::McParticles const & mcparts,
737+ UDCCs const & sgcands, UDTCs const & udtracks,
738+ CCs const & /* collisions*/ , BCs const & /* bcs*/ , TCs const & /* tracks*/ )
739+ {
740+ if (verboseInfoMC) {
741+ LOGF (info, " Number of McCollisions %d" , mccols.size ());
742+ LOGF (info, " Number of SG candidates %d" , sgcands.size ());
743+ LOGF (info, " Number of UD tracks %d" , udtracks.size ());
744+ }
745+ if (mccols.size () > 0 ) {
746+ if (sgcands.size () > 0 ) {
747+ procWithSgCand (mccols, mcparts, sgcands, udtracks);
748+ } else {
749+ if (saveAllMcCollisions) {
750+ procWithoutSgCand (mccols, mcparts);
751+ }
752+ }
753+ }
754+ }
713755 PROCESS_SWITCH (McSGCandProducer, processMC, " Produce MC tables" , false );
756+
714757 void processDummy (aod::Collisions const & /* collisions*/ )
715758 {
716759 // do nothing
0 commit comments