//**********MAIN************************************************************************** int main(int argc, char* argv[]) { if(argc < 2) { cout << argv[0] << " cfg file " << "[run]" << endl; return -1; } //---memory consumption tracking--- float cpu[2]{0}, mem[2]={0}, vsz[2]={0}, rss[2]={0}; //---load options--- CfgManager opts; opts.ParseConfigFile(argv[1]); //-----input setup----- if(argc > 2) { vector<string> run(1, argv[2]); opts.SetOpt("h4reco.run", run); } string outSuffix = opts.GetOpt<string>("h4reco.outNameSuffix"); string run = opts.GetOpt<string>("h4reco.run"); TChain* inTree = new TChain("H4tree"); ReadInputFiles(opts, inTree); H4Tree h4Tree(inTree); //-----output setup----- uint64 index=stoul(run)*1e9; TFile* outROOT = new TFile(outSuffix+TString(run)+".root", "RECREATE"); outROOT->cd(); RecoTree mainTree(&index); //---Get plugin sequence--- PluginLoader<PluginBase>* loader; vector<PluginLoader<PluginBase>* > pluginLoaders; map<string, PluginBase*> pluginMap; vector<PluginBase*> pluginSequence; vector<string> pluginList = opts.GetOpt<vector<string> >("h4reco.pluginList"); //---plugin creation pluginLoaders.reserve(pluginList.size()); for(auto& plugin : pluginList) { cout << ">>> Loading plugin <" << plugin << ">" << endl; //---create loader loader = new PluginLoader<PluginBase>(opts.GetOpt<string>(plugin+".pluginType")); pluginLoaders.push_back(loader); pluginLoaders.back()->Create(); //---get instance and put it in the plugin sequence PluginBase* newPlugin = pluginLoaders.back()->CreateInstance(); if(newPlugin) { pluginSequence.push_back(newPlugin); pluginSequence.back()->SetInstanceName(plugin); pluginMap[plugin] = pluginSequence.back(); } else { cout << ">>> ERROR: plugin type " << opts.GetOpt<string>(plugin+".pluginType") << " is not defined." << endl; return 0; } } //---begin for(auto& plugin : pluginSequence) { //---call Begin() methods and check the return status bool r_status = plugin->Begin(opts, &index); if(!r_status) { cout << ">>> ERROR: plugin returned bad flag from Begin() call: " << plugin->GetInstanceName() << endl; exit(-1); } //---Get plugin shared data for(auto& shared : plugin->GetSharedData("", "TTree", true)) { TTree* tree = (TTree*)shared.obj; tree->SetMaxVirtualSize(10000); tree->SetDirectory(outROOT); } } //---events loop int maxEvents = opts.OptExist("h4reco.maxEvents") ? opts.GetOpt<int>("h4reco.maxEvents") : -1; cout << ">>> Processing H4DAQ run #" << run << " <<<" << endl; while(h4Tree.NextEntry() && (index-stoul(run)*1e9<maxEvents || maxEvents==-1)) { if(index % 1000 == 0) { cout << ">>> Processed events: " << index-stoul(run)*1e9 << "/" << (maxEvents<0 ? h4Tree.GetEntries() : min(h4Tree.GetEntries(), (uint64)maxEvents)) << endl; TrackProcess(cpu, mem, vsz, rss); } //---call ProcessEvent for each plugin and check the return status bool status=true; for(auto& plugin : pluginSequence) if(status) status = plugin->ProcessEvent(h4Tree, pluginMap, opts); //---fill the main tree with info variables and increase event counter if(status) { mainTree.time_stamp = h4Tree.evtTimeStart; mainTree.run = h4Tree.runNumber; mainTree.spill = h4Tree.spillNumber; mainTree.event = h4Tree.evtNumber; mainTree.Fill(); ++index; } } //---end for(auto& plugin : pluginSequence) { //---call endjob for each plugin bool r_status = plugin->End(opts); // if(!r_status) // { // cout << ">>> ERROR: plugin returned bad flag from End() call: " << plugin->GetInstanceName() << endl; // exit(-1); // } //---get permanent data from each plugin and store them in the out file for(auto& shared : plugin->GetSharedData()) { if(shared.obj->IsA()->GetName() == string("TTree")) { TTree* currentTree = (TTree*)shared.obj; outROOT->cd(); currentTree->BuildIndex("index"); currentTree->Write(currentTree->GetName(), TObject::kOverwrite); mainTree.AddFriend(currentTree->GetName()); } else { outROOT->cd(); shared.obj->Write(shared.tag.c_str(), TObject::kOverwrite); } } } //---close mainTree.Write(); opts.Write("cfg"); outROOT->Close(); for(auto& loader : pluginLoaders) loader->Destroy(); //---info TrackProcess(cpu, mem, vsz, rss); exit(0); }
//**********MAIN************************************************************************** int main(int argc, char* argv[]) { if(argc < 2) { cout << argv[0] << " cfg file " << "[run]" << endl; return -1; } gROOT->SetBatch(kTRUE); //---load options--- CfgManager opts; opts.ParseConfigFile(argv[1]); //---data opts string path=opts.GetOpt<string>("global.path2data"); string run=opts.GetOpt<string>("global.run"); string outSuffix=opts.GetOpt<string>("global.outFileSuffix"); if(argc > 2) run = argv[2]; int maxEvents = opts.GetOpt<int>("global.maxEvents"); //---channels opts int nCh = opts.GetOpt<int>("global.nCh"); int nSamples = opts.GetOpt<int>("global.nSamples"); float tUnit = opts.GetOpt<float>("global.tUnit"); string refChannel = opts.GetOpt<string>("global.refChannel"); vector<string> channelsNames = opts.GetOpt<vector<string> >("global.channelsNames"); map<string, vector<float> > timeOpts; timeOpts[refChannel] = opts.GetOpt<vector<float> >(refChannel+".timeOpts"); for(auto& channel : channelsNames) timeOpts[channel] = opts.GetOpt<vector<float> >(channel+".timeOpts"); //---definitions--- int iEvent=0; //-----output setup----- TString outF="ntuples/Templates_"+TString(outSuffix)+"_"+TString(run)+".root"; TFile* outROOT = TFile::Open(outF, "RECREATE"); outROOT->cd(); map<string, TH2F*> templates; for(auto channel : channelsNames) templates[channel] = new TH2F(channel.c_str(), channel.c_str(), 16000, -40, 160, 1200, -0.1, 1.1); //-----input setup----- TChain* inTree = new TChain("H4tree"); string ls_command; if(path.find("/eos/cms") != string::npos) ls_command = string("gfal-ls root://eoscms/"+path+run+" | grep 'root' > tmp/"+run+".list"); else ls_command = string("ls "+path+run+" | grep 'root' > tmp/"+run+".list"); system(ls_command.c_str()); ifstream waveList(string("tmp/"+run+".list").c_str(), ios::in); string file; int nFiles=0; while(waveList >> file && (opts.GetOpt<int>("global.maxFiles")<0 || nFiles<opts.GetOpt<int>("global.maxFiles")) ) { if(path.find("/eos/cms") != string::npos) { std::cout << "+++ Adding file " << ("root://eoscms/"+path+run+"/"+file).c_str() << std::endl; inTree->AddFile(("root://eoscms/"+path+run+"/"+file).c_str()); } else { std::cout << "+++ Adding file " << (path+run+"/"+file).c_str() << std::endl; inTree->AddFile((path+run+"/"+file).c_str()); } ++nFiles; } H4Tree h4Tree(inTree); //---process WFs--- long nentries=h4Tree.GetEntries(); cout << ">>> Processing H4DAQ run #" << run << " events #" << nentries << " <<<" << endl; while(h4Tree.NextEntry() && (iEvent<maxEvents || maxEvents==-1)) { ++iEvent; if (iEvent%1000==0) std::cout << "Processing event " << iEvent << "/" << nentries << std::endl; //---setup output event int outCh=0; bool badEvent=false; //---read the digitizer //---read time raference channel float refTime=0, refAmpl=0; WFClass WF(opts.GetOpt<int>(refChannel+".polarity"), tUnit); int digiGr = opts.GetOpt<int>(refChannel+".digiGroup"); int digiCh = opts.GetOpt<int>(refChannel+".digiChannel"); int offset = digiGr*9*nSamples + digiCh*nSamples; for(int iSample=offset; iSample<offset+nSamples; ++iSample) { //---H4DAQ bug: sometimes ADC value is out of bound. if(h4Tree.digiSampleValue[iSample] > 4096) { badEvent = true; break; } WF.AddSample(h4Tree.digiSampleValue[iSample]); } //---skip bad events if(badEvent) continue; WF.SetBaselineWindow(opts.GetOpt<int>(refChannel+".baselineWin", 0), opts.GetOpt<int>(refChannel+".baselineWin", 1)); WF.SetSignalWindow(opts.GetOpt<int>(refChannel+".signalWin", 0), opts.GetOpt<int>(refChannel+".signalWin", 1)); WFBaseline refBaseline=WF.SubtractBaseline(); refAmpl = WF.GetInterpolatedAmpMax(); refTime = WF.GetTime(opts.GetOpt<string>(refChannel+".timeType"), timeOpts[refChannel]).first; //---you may want to use an offset, for example if you use the trigger time if(opts.OptExist(refChannel+".timeOffset"))refTime -= opts.GetOpt<float>(refChannel+".timeOffset"); //---require reference channel to be good if(refTime/tUnit < opts.GetOpt<int>(refChannel+".signalWin", 0) || refTime/tUnit > opts.GetOpt<int>(refChannel+".signalWin", 1) || refBaseline.rms > opts.GetOpt<float>(refChannel+".noiseThreshold") || refAmpl < opts.GetOpt<int>(refChannel+".amplitudeThreshold")) continue; //---template channels for(auto& channel : channelsNames) { //---read WFs WFClass WF(opts.GetOpt<int>(channel+".polarity"), tUnit); int digiGr = opts.GetOpt<int>(channel+".digiGroup"); int digiCh = opts.GetOpt<int>(channel+".digiChannel"); int offset = digiGr*9*nSamples + digiCh*nSamples; for(int iSample=offset; iSample<offset+nSamples; ++iSample) { //---H4DAQ bug: sometimes ADC value is out of bound. if(h4Tree.digiSampleValue[iSample] > 4096) { badEvent = true; break; } WF.AddSample(h4Tree.digiSampleValue[iSample]); } //---skip bad events if(badEvent) continue; //---compute reco variables float channelTime=0, channelAmpl=0; WF.SetBaselineWindow(opts.GetOpt<int>(channel+".baselineWin", 0), opts.GetOpt<int>(channel+".baselineWin", 1)); WF.SetSignalWindow(opts.GetOpt<int>(channel+".signalWin", 0), opts.GetOpt<int>(channel+".signalWin", 1)); WFBaseline channelBaseline=WF.SubtractBaseline(); channelAmpl = WF.GetInterpolatedAmpMax(-1,-1,opts.GetOpt<int>(channel+".signalWin", 2)); channelTime = WF.GetTime(opts.GetOpt<string>(channel+".timeType"), timeOpts[channel]).first; //---skip bad events or events with no signal if(channelTime/tUnit > opts.GetOpt<int>(channel+".signalWin", 0) && channelTime/tUnit < opts.GetOpt<int>(channel+".signalWin", 1) && channelBaseline.rms < opts.GetOpt<float>(channel+".noiseThreshold") && channelAmpl > opts.GetOpt<int>(channel+".amplitudeThreshold") && channelAmpl < 4000) { vector<float>* analizedWF = WF.GetSamples(); for(int iSample=0; iSample<analizedWF->size(); ++iSample) templates[channel]->Fill(iSample*tUnit-refTime, analizedWF->at(iSample)/channelAmpl); } } } cout << ">>> Writing output " << outF << " <<<" << endl; outROOT->cd(); for(auto& channel : channelsNames) { // pair<TH1F, TH1F> dft = DFT_cut(getMeanProfile(templates[channel]), channel, 4); // dft.first.Write(); // dft.second.Write(); if (templates[channel]->GetEntries()>1024*10) { TH1F* prof=getMeanProfile(templates[channel]); templates[channel]->Write(); prof->Write(); } } opts.Write("cfg"); outROOT->Close(); }