Repository: dmwm/wmcore
Issue: T0 related bug when creating merge process

**Impact of the bug**
 `merge` creation process  fails with T0 workflows due to a bug in the output module labels

**Describe the bug**
When creating a `merge` process, the output module label is either: `Merged` OR `MergedError`, depending on the use of `--useErrorDataset` in this external script:

https://github.com/cms-sw/cmssw-wm-tools/blob/30b626d030b7b11f83fe4e0385ce303e83d3fcdf/bin/cmssw_wm_create_process.py#L45-L46

However, the output modules listed in cmsswStep have both, so one of them fails and the process aborts.

**How to reproduce it**
T0 workflow with `useErrorDataset=False` on a `merge` process gives:

```
2021-06-09 06:47:49,472:INFO:SetupCMSSWPset:ScramRun command args: edm_pset_tweak.py --input_pkl /srv/job/WMTaskSpace/cmsRun1/PSet.pkl --output_pkl /srv/job/WMTaskSpace/cmsRun1/PSet.pkl --json /srv/job/WMTaskSpace/cmsRun1/PSetTweak.json
2021-06-09 06:47:49,472:INFO:Scram:Creating a subprocess to run the PSet setup.
2021-06-09 06:47:49,472:INFO:Scram:Also recording SCRAM command-line related output.
2021-06-09 06:47:49,481:INFO:Scram:    Invoking command: edm_pset_tweak.py --input_pkl /srv/job/WMTaskSpace/cmsRun1/PSet.pkl --output_pkl /srv/job/WMTaskSpace/cmsRun1/PSet.pkl --json /srv/job/WMTaskSpace/cmsRun1/PSetTweak.json
2021-06-09 06:47:49,884:INFO:Scram:Subprocess stdout was:
Set attribute process.source.fileNames to cms.untracked.vstring('/store/unmerged/data/Tier0_REPLAY_2021/Cosmics/RAW/v2106090600/000/341/607/00000/aa0b3ef3-6a94-4e03-a338-d4779e41f25c.root')
Set attribute process.Merged.logicalFileName to cms.untracked.string('/store/backfill/1/data/Tier0_REPLAY_2021/Cosmics/RAW/v2106090600/000/341/607/00000/Merged.root')
Set attribute process.maxEvents to cms.untracked.PSet(
    input = cms.untracked.int32(-1),
    output = cms.optional.untracked.allowed(cms.int32,cms.PSet)
)
Tweak not applied process.MergedError.logicalFileName /store/backfill/1/data/Tier0_REPLAY_2021/Cosmics-Error/RAW/v2106090600/000/341/607/00000/MergedError.root
```

**Expected behavior**
Tweak should only be created and applied to either `Merged` or `MergedError`, depending on the value of `useErrorDataset`

**Additional context and error message**
 In the past, this error was ignored when creating the data set and the process creation continued.

Changed files:
src/python/PSetTweaks/WMTweak.py, src/python/PSetTweaks/PSetTweak.py, src/python/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py, src/python/WMCore/Configuration.py, test/python/WMCore_t/Configuration_t.py

diff --git a/src/python/PSetTweaks/PSetTweak.py b/src/python/PSetTweaks/PSetTweak.py
index 887af4b84..38565f09d 100644
--- a/src/python/PSetTweaks/PSetTweak.py
+++ b/src/python/PSetTweaks/PSetTweak.py
@@ -421,6 +421,16 @@ class PSetTweak(object):
             for param, value in viewitems(jsoniser.parameters):
                 self.addParameter(param , value)
 
+    def reset(self):
+        """
+        _reset_
+
+        Reset pset holder process
+
+        """
+        del self.process
+        self.process = PSetHolder("process")
+
 
 def makeTweakFromJSON(jsonDictionary):
     """
diff --git a/src/python/PSetTweaks/WMTweak.py b/src/python/PSetTweaks/WMTweak.py
index 4dd3cc586..6ab83bad0 100644
--- a/src/python/PSetTweaks/WMTweak.py
+++ b/src/python/PSetTweaks/WMTweak.py
@@ -516,7 +516,7 @@ def makeOutputTweak(outMod, job, result):
 
     """
     # output filenames
-    modName = str(getattr(outMod, "_internal_name"))
+    modName = outMod.getInternalName()
     logging.info("modName = %s", modName)
     fileName = "%s.root" % modName
 
diff --git a/src/python/WMCore/Configuration.py b/src/python/WMCore/Configuration.py
index 5c4d775c1..d14d2a72f 100644
--- a/src/python/WMCore/Configuration.py
+++ b/src/python/WMCore/Configuration.py
@@ -357,6 +357,15 @@ class ConfigSection(object):
         comps = self._internal_settings
         return list(comps)
 
+    def getInternalName(self):
+        """
+        _getInternalName_
+
+        Return the internal name
+
+        """
+        return self._internal_name
+
 
 class Configuration(object):
     # pylint: disable=protected-access
diff --git a/src/python/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py b/src/python/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py
index f580f14a4..348c4ca4a 100644
--- a/src/python/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py
+++ b/src/python/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py
@@ -218,6 +218,15 @@ class SetupCMSSWPset(ScriptInterface):
         return
 
     def applyPsetTweak(self, psetTweak, skipIfSet=False, allowFailedTweaks=False, name='', cleanupTweak=False):
+        """
+        _applyPsetTweak_
+        Apply a tweak to a pset process.
+        Options:
+          skipIfSet: Do not apply a tweak to a parameter that has a value set already.
+          allowFailedTweaks: If the tweak of a parameter fails, do not abort and continue tweaking the rest.
+          name: Extra string to add to the name of the json file that will be createed.
+          cleanupTweak: Reset pset tweak object after applying all tweaks. Mostly used after using self.tweak
+        """
         procScript = "edm_pset_tweak.py"
         psetTweakJson = os.path.join(self.stepSpace.location, "PSetTweak%s.json" % name)
         psetTweak.persist(psetTweakJson, formatting='simplejson')
@@ -233,8 +242,8 @@ class SetupCMSSWPset(ScriptInterface):
             cmd += " --allow_failed_tweaks"
         self.scramRun(cmd)
 
-        if cleanupTweak is True:
-            psetTweak = PSetTweak()
+        if cleanupTweak:
+            psetTweak.reset()
 
         return
 
@@ -748,8 +757,22 @@ class SetupCMSSWPset(ScriptInterface):
         cmsswStep = self.step.getTypeHelper()
         for om in cmsswStep.listOutputModules():
             mod = cmsswStep.getOutputModule(om)
+            modName = mod.getInternalName()
+
+            if funcName == 'merge':
+                # Do not use both Merged output label unless useErrorDataset is False
+                # Do not use both MergedError output label unless useErrorDataset is True 
+                useErrorDataset = getattr(self.jobBag, "useErrorDataset", False)
+
+                if useErrorDataset and modName != 'MergedError':
+                    continue
+                if not useErrorDataset and modName == 'MergedError':
+                    continue
+
             makeOutputTweak(mod, self.job, self.tweak)
-        self.applyPsetTweak(self.tweak, cleanupTweak=True)
+        # allow failed tweaks in this case, to replicate the previous implementation, where it would ignore 
+        # and continue if it found an output module that  doesn't exist and don't want in the pset like: process.Sqlite
+        self.applyPsetTweak(self.tweak, allowFailedTweaks=True, cleanupTweak=True)
 
         # revlimiter for testing
         if getattr(self.step.data.application.command, "oneEventMode", False):
diff --git a/test/python/WMCore_t/Configuration_t.py b/test/python/WMCore_t/Configuration_t.py
index d917c2337..8d82cca6c 100644
--- a/test/python/WMCore_t/Configuration_t.py
+++ b/test/python/WMCore_t/Configuration_t.py
@@ -327,5 +327,15 @@ class ConfigurationTest(unittest.TestCase):
         self.assertEqual(d["Task1"]["subSection"]["value3"], "MyValue3")
 
 
+    def testI_testGetInternalName(self):
+        """
+        Test we properly retrieve the internal name of the configuration object
+
+        """
+        config = ConfigSection("config")
+        name = config.getInternalName()
+        self.assertEqual(name, "config")
+
+
 if __name__ == '__main__':
     unittest.main()