Newer
Older
#include "QFramework/TQFolder.h"
#include "QFramework/TQIterator.h"
#include "QFramework/TQUtils.h"
#include "QFramework/TQStringUtils.h"
#include "TString.h"
#include "SFramework/TSStatisticsManager.h"
namespace TSBaseActions {
class TransferSystematics : public TSStatisticsManager::Action {
bool matchesAny(TQFolder* f,const std::vector<TString>& filters, const TString&prefix = "") const {
bool matched = false;
for(const auto& x:filters){
if(TQStringUtils::matches(f->GetName(),prefix+x)){
matched = true;
return matched;
}
bool execute(TQFolder * config) const override {
TQFolder* model = models()->getFolder(config->GetName());
if(!model){
manager->error(TString::Format("no such model available: '%s'",config->GetName()));
}
TQFolderIterator transfers = config->getListOfFolders("?");
int nOK = 0;
while(transfers.hasNext()){
TQFolder* transfer = transfers.readNext();
if(!transfer) continue;
nOK += performTransfer(transfer,model);
}
return nOK;
}
bool performTransfer(TQFolder * config,TQFolder* model) const {
TString mode = config->getTagStringDefault("mode","sample");
std::vector<TString> exceptRegions = config->getTagVString("except.Channels");
std::vector<TString> exceptSamples = config->getTagVString("except.Samples");
std::vector<TString> exceptSystematics = config->getTagVString("except.OverallSys");
manager->info(TString::Format("performing transfer '%s'",config->GetName()));
std::vector<TString> targetSamples = config->getTagVString("select.Samples");
if(targetSamples.empty()){
targetSamples.push_back("*");
}
std::vector<TString> targetChannels = config->getTagVString("select.Channels");
if(targetChannels.empty()){
targetChannels.push_back("*");
}
std::vector<TString> targetSystematics = config->getTagVString("select.Systematics");
if(targetSystematics.empty()){
targetSystematics.push_back("*");
}
TString mark;
bool marked = config->getTagString("selectTag.OverallSys",mark);
if (mode.Contains("sample",TString::kIgnoreCase)) {
TString sourceSample;
if(!config->getTagString("source",sourceSample)){
manager->error(TString::Format("missing required 'source' information on transfer '%s'",config->GetName()));
return false;
}
int i=0;
bool verbose = config->getTagBoolDefault("verbose",false);
for(const auto& channelExp:targetChannels){
TQFolderIterator regions(model->getListOfFolders("Channel."+channelExp),true);
while(regions.hasNext()){
TQFolder* region = regions.readNext();
if(!region) continue;
if(matchesAny(region,exceptRegions,"Channel.")) continue;
if(verbose) manager->info(TString::Format("transferring in region '%s'",region->GetName()));
TQFolder* source = region->getFolder("Sample."+sourceSample);
if(!source){
manager->warn(TString::Format("unable to find source '%s' in channel '%s', skipping!",sourceSample.Data(),region->GetName()));
continue;
}
for(const auto& sampleExp:targetSamples){
TQFolderIterator samples(region->getListOfFolders("Sample."+sampleExp),true);
while(samples.hasNext()){
TQFolder* sample = samples.readNext();
if(!sample) continue;
if(matchesAny(sample,exceptSamples,"Sample.")) continue;
if(TQStringUtils::matches(sample->GetName(),"Sample."+sourceSample)) continue;
if(verbose) manager->info(TString::Format("\tfor sample '%s'",sample->GetName()));
sample->deleteObject(".Dropped!");
for (const auto& systType : {TString("OverallSys."),TString("ShapeSys.")}) {
std::vector<TQFolder*> remove;
{
TQFolderIterator oldsystematics(sample->getListOfFolders(systType+"*"),true);
while(oldsystematics.hasNext()){
TQFolder* systematic = oldsystematics.readNext();
if(!systematic) continue;
if(matchesAny(systematic,exceptSystematics,systType) || !matchesAny(systematic,targetSystematics,systType)) continue;
if(marked && !systematic->getTagBoolDefault(mark,false)) continue;
if(verbose) manager->info(TString::Format("\t\tremoving '%s'",systematic->GetName()));
remove.push_back(systematic);
}
}
for(auto systematic:remove){
systematic->detachFromBase();
delete systematic;
}
{
TQFolderIterator newsystematics(source->getListOfFolders(systType+"*"),true);
while(newsystematics.hasNext()){
TQFolder* systematic = newsystematics.readNext();
if(!systematic) continue;
if(matchesAny(systematic,exceptSystematics,systType) || !matchesAny(systematic,targetSystematics,systType)) continue;
if(marked && !systematic->getTagBoolDefault(mark,false)) continue;
if(verbose) manager->info(TString::Format("\t\tcopying '%s'",systematic->GetName()));
TQFolder* copy = systematic->copy();
i++;
sample->addFolder(copy);
copy->setTagString("transferred",source->GetName());
}
}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
}
}
}
return (i>0);
} else if (mode.Contains("channel",TString::kIgnoreCase)) {
TString sourceChannel;
if(!config->getTagString("source",sourceChannel)){
manager->error(TString::Format("missing required 'source' information on transfer '%s'",config->GetName()));
return false;
}
TQFolder* source = model->getFolder("Channel."+sourceChannel);
if(!source){
manager->warn(TString::Format("unable to find source channel '%s' in model '%s', skipping!",sourceChannel.Data(),model->GetName()));
return false;
}
int i=0;
bool verbose = config->getTagBoolDefault("verbose",false);
for(const auto& channelExp:targetChannels){
TQFolderIterator regions(model->getListOfFolders("Channel."+channelExp),true);
while(regions.hasNext()){
TQFolder* region = regions.readNext();
if(!region) continue;
if(region == source) continue; //there is no point in transfering from A to A (it would even delete some stuff!)
if(matchesAny(region,exceptRegions,"Channel.")) continue;
if(verbose) manager->info(TString::Format("transferring in region '%s'",region->GetName()));
for(const auto& sampleExp:targetSamples){
TQFolderIterator samples(region->getListOfFolders("Sample."+sampleExp),true);
while(samples.hasNext()){
TQFolder* sample = samples.readNext();
if(!sample) continue;
if(matchesAny(sample,exceptSamples,"Sample.")) continue;
if(verbose) manager->info(TString::Format("\tfor sample '%s'",sample->GetName()));
sample->deleteObject(".Dropped!");
std::vector<TQFolder*> remove;
{
TQFolderIterator oldsystematics(sample->getListOfFolders("OverallSys.*"),true);
while(oldsystematics.hasNext()){
TQFolder* systematic = oldsystematics.readNext();
if(!systematic) continue;
if(matchesAny(region,exceptSystematics,"OverallSys.")) continue;
if(marked && !systematic->getTagBoolDefault(mark,false)) continue;
if(!matchesAny(systematic,targetSystematics,"OverallSys.")) continue;
if(verbose) manager->info(TString::Format("\t\tremoving '%s'",systematic->GetName()));
remove.push_back(systematic);
}
}
for(auto systematic:remove){
systematic->detachFromBase();
delete systematic;
}
{
TQFolderIterator newsystematics(source->getListOfFolders(TQFolder::concatPaths(sample->getName(),"OverallSys.*")),true);
while(newsystematics.hasNext()){
TQFolder* systematic = newsystematics.readNext();
if(!systematic) continue;
if(matchesAny(region,exceptSystematics,"OverallSys.")) continue;
if(marked && !systematic->getTagBoolDefault(mark,false)) continue;
if(!matchesAny(systematic,targetSystematics,"OverallSys.")) continue;
if(verbose) manager->info(TString::Format("\t\tcopying '%s'",systematic->GetName()));
TQFolder* copy = systematic->copy();
i++;
sample->addFolder(copy);
copy->setTagString("transferred",source->GetName());
}
return (i>0);
//no valid mode was selected
manager->error("Failed to perform, no valid mode was selected!");
return false;